C++ Functions Overloading

In C++ Functions Overloading, two or more than two functions can have same name with different number and/or type of parameters. For example, the names of the functions below are same but the number and type of arguments is different. This phenomenon is called function overloading.

In the above example, the two functions having the name newFunction have different number and sequence of parameter. It is a valid example where the first function has one argument while the second function has two arguments. Here the type of parameters is same. Note that overloading functions allow to have same function return type.

Let us look at another example where type of parameters is different.

In the above example, the two functions having the name newFunction have different number and sequence of parameter. It is a valid example where the first function has one argument while the second function has two arguments. Here the type of parameters is same. Note that overloading functions allow to have same function return type.

C++ Overloading Functions can have a combination of different number and type of parameters with same name. For example:

The above functions are valid overloading functions having different number and type of arguments with same name.

In the code below programmer creates three overloading functions with different types of parameters. Note that the output depends on the type of parameter. If the programmer enters integer value, the compiler executes the first functions. Similarly if the compiler finds double value in the parameters, it executes the second function. In the same way if paramter type is string then the compiler executes the third function.

#include <iostream>
#include <string>
using namespace std;
void dispFunc(int x) {
    cout << "Displaying int value: " << x << endl;
    }
void dispFunc(double  y) {
    cout << "Displaying float value: " << y << endl;
    }
void dispFunc(string z) {
    cout << "Displaying string: " << z << endl;
    }
int main() 
{
   dispFunc(33.2);
   dispFunc(62.223);
   dispFunc("C++ Overloading Functions");
   return 0;
}

The output of above code after compilation is:

Another example shows overloading functions with different number of parameters. There are two functions that add the parameters together. If the programmer calls the function with two parameters then the compiler executes the first function. And if the compiler finds three parameters, it executes the second function.

#include <iostream>
using namespace std;
int addition(int x,int y) {
    int sum;
    sum = x + y;
    cout << "Sum of two numbers is: " << sum << endl;
    return 0;
}
int addition(int x,int y, int z) {
    int sum;
    sum = x + y + z;
    cout << "Sum of three numbers is: " << sum << endl;
    return 0;
}
int main() 
{
    addition(10, 20);
    addition(10, 20, 30);
    return 0;
}

The output of above code after compilation is:

Here is an example that shows two functions. One function takes two integer type arguments and the other function takes three double type arguments. Both functions display the sum of input parameters. Is user add two parameters, then compiler executes the first function. And for three parameters, compiler executes the second function.

#include <iostream>
using namespace std;
int addition(int x,int y) {
    int sum;
    sum = x + y;
    cout << "Sum of two numbers is: " << sum << endl;
    return 0;
}
double addition(double x,double y, double z) {
    double sum;
    sum = x + y + z;
    cout << "Sum of three float numbers is: " << sum << endl;
    return 0;
}
int main() 
{
    addition(10, 20);
    addition(10.2, 20.2, 30.2);
    return 0;
}

The output of the above code after compilation is:

Advantages of C++ Functions Overloading

C++ functions overloading is a feature of compile-time polymorphism that provide simplicity to the programmer to call the same function with different list of parameters. At the same time, the programmer can achieve code reusability and can enhance code readability. Using overloading functions, programmer does not have to limit the functions to a specific number or to a specific type of input parameters.

Comments are closed.