Function Overloading in C++

Function overloading in C++ with examples

Function overloading in C++ with examples

 

Function overloading is a C++ programming feature that allows us to have more than one function having same name but different parameter list, when I say parameter list, it means the data type and sequence of the parameters, for example the parameters list of a function myfuncn(int a, float b) is (int, float) which is different from the function myfuncn(float a, int b) parameter list (float, int). Function overloading is a compile-time polymorphism.
Now that we know what is parameter list let’s see the rules of overloading: we can have following functions in the same scope.

sum(int num1, int num2)
sum(int num1, int num2, int num3)
sum(int num1, double num2)

The easiest way to remember this rule is that the parameters should qualify any one or more of the following conditions, they should have different typenumber or sequence of parameters.

For example:
These two functions have different parameter type:

sum(int num1, int num2)
sum(double num1, double num2)

These two have different number of parameters:

sum(int num1, int num2)
sum(int num1, int num2, int num3)

These two have different sequence of parameters:

sum(int num1, double num2)
sum(double num1, int num2)

All of the above three cases are the valid case of overloading. We can have any number of functions, just remember that the parameter list should be different. For example:

int sum(int, int)
double sum(int, int)

This is not allowed as the parameter list is the same. Even though they have different return types, it’s not valid.

Function overloading Example

Let’s take an example to understand function overloading in C++.

#include <iostream>
using namespace std;
class Addition {
public:
    int sum(int num1,int num2) {
        return num1+num2;
    }
    int sum(int num1,int num2, int num3) {
       return num1+num2+num3;
    }
};
int main(void) {
    Addition obj;
    cout<<obj.sum(20, 15)<<endl;
    cout<<obj.sum(81, 100, 10);
    return 0;
}

Output:

35
191

Function overloading Example 2

As I mentioned in the beginning of this guide that functions having different return types and same parameter list cannot be overloaded. However, if the functions have different parameter list then they can have same or different return types to be eligible for overloading. In short, the return type of a function does not play any role in function overloading. All that matters is the parameter list of functions.

#include <iostream>
using namespace std;
class DemoClass {
public:
    int demoFunction(int i) {
        return i;
    }
    double demoFunction(double d) {
        return d;
    }
};
int main(void) {
    DemoClass obj;
    cout<<obj.demoFunction(100)<<endl;
    cout<<obj.demoFunction(5005.516);
   return 0;
}

Output:

100
5006.52

Advantages of Function overloading

The main advantage of function overloading is to improve the code readability and allows code reusability. In example 1, we have seen how we were able to have more than one function for the same task(addition) with different parameters, this allowed us to add two integer numbers as well as three integer numbers if we wanted we could have some more functions with the same name and four or five arguments.
Imagine if we didn’t have function overloading, we either have the limitation to add only two integers or we had to write different name functions for the same task addition, this would reduce the code readability and reusability.

3 thoughts on “Function overloading in C++ with examples

  1. Pingback: Difference between Function Overloading and Function overriding in C++ - CloudNClear

  2. Pingback: Abstraction in C++ with example - CloudNClear

  3. Pingback: Interfaces in C++ with Examples - CloudNClear

Comments are closed.