in , ,

C++ Function Overloading

C++ Function Overloading
C++ Function Overloading

C++ Function Overloading

In this tutorial, we will find out about the function overloading in C++ with examples.

In C++, two functions can have a similar name if the number, and/ or sort of arguments passed, is different.

These functions having similar names yet various contentions are known as overloaded functions. For instance:

// same number different arguments
int test() { }
int test(int a) { }
float test(double a) { }
int test(int a, double b) { }

Here, each of the 4 functions are overloaded functions.

Notice that the return type of all these 4 functions are not the equivalent. Overloaded functions could conceivably have distinctive return types yet they should have various contentions. For instance,

// Error code
int test(int a) { }
double test(int b){ }

Here, the two functions have a similar name, a similar sort, and a similar number of contentions. Subsequently, the compiler will throw an error.


Function Overloading using Different Types of Parameter

// Program to compute absolute value
// Works for both int and float

#include <iostream>
using namespace std;

// function with float type parameter
float absolute(float var){
    if (var < 0.0)
        var = -var;
    return var;
}

// function with int type parameter
int absolute(int var) {
     if (var < 0)
         var = -var;
    return var;
}

int main() {
    
    // call function with int type parameter
    cout << "Absolute value of -5 = " << absolute(-5) << endl;

    // call function with float type parameter
    cout << "Absolute value of 5.5 = " << absolute(5.5f) << endl;
    return 0;
}

Output

Absolute value of -5 = 5
Absolute value of 5.5 = 5.5

In this program, we overload the absolute() function. Based on the type of parameter passed during the function call, the corresponding function is called.


Function Overloading using Different Number of Parameters

#include <iostream>
using namespace std;

// function with 2 parameters
void display(int var1, double var2) {
    cout << "Integer number: " << var1;
    cout << " and double number: " << var2 << endl;
}

// function with double type single parameter
void display(double var) {
    cout << "Double number: " << var << endl;
}

// function with int type single parameter
void display(int var) {
    cout << "Integer number: " << var << endl;
}

int main() {

    int a = 5;
    double b = 5.5;

    // call function with int type parameter
    display(a);

    // call function with double type parameter
    display(b);

    // call function with 2 parameters
    display(a, b);

    return 0;
}

Output

Integer number: 5
Float number: 5.5
Integer number: 5 and double number: 5.5

Here, the display() work is called multiple times with various contentions. Contingent upon the number and type of arguments passed, the relating display() work is called.

The return sort of every one of these functions is the equivalent however that need not be the case for function overloading.


Note: In C++, many standard library functions are overloaded. For example, the sqrt() function can take double, float, int, etc. as parameters. This is possible because the sqrt() function is overloaded in C++.


Please feel free to give your comment if you face any difficulty here.

salman khan

Written by worldofitech

Leave a Reply

How to Create a High-Definition, Abstract Wallpaper In Photoshop

How to Create a High-Definition, Abstract Wallpaper In Photoshop

How to Batch-Process Files in Photoshop

How to Batch-Process Files in Photoshop