C++

C++ Function Overloading – Detailed Instructions

A C++ programming feature called function overloading enables us to have multiple functions with the same name but different parameter lists. By parameter list, I mean the data type and order of the parameters. For instance, the parameter list of the function myfuncn(int a, float b) is different from the parameter list of myfuncn(float a, int b) (float, int). A polymorphism created at compile time is function overloading.
We can have the following functions in the same scope now that we understand what a parameter list is.

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

The simplest way to remember this rule is to focus on the fact that the parameters must satisfy one or more of the following requirements: they must have distinct types, numbers, or sequences 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 three of the aforementioned situations qualify as overloaded. We can have as many functions as we want, but always keep in mind that the parameter list should vary. For instance:

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

Since the parameter list is the same, this is not permitted. It is invalid despite the fact that they have distinct return types.

Example of function overloading

To further understand function overloading in C++, let’s look at an example.

#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

Example 2 of function overloading

As I indicated at the beginning of this book, functions with the same parameter list but different return types cannot be overloaded. To qualify for overloading, the functions must have the same or distinct return types if their parameter lists differ. In other words, function overloading has nothing to do with a function’s return type. The function’s parameter list is all that matters.

#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

Function overloading’s benefits

The primary benefit of function overloading is that it makes code easier to read and reuse. We saw in Example 1 how having multiple functions for the same task—addition—with various parameters allowed us to add both two and three integer numbers. If we so desired, we could have other functions with the same name and four or five arguments.
Imagine that without function overloading, we would have to either write different name functions for the same task addition or be limited to adding only two numbers. This would make the code harder to read and reuse.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button