In this C++ tutorial, you will learn about Function Overloading, how to define multiple functions in a class with same name but different number or type of arguments, and how can we access each of these functions based on arguments passed, with examples.

C++ Function Overloading

In C++, Function Overloading is having more than one function with the same name, in the same scope, but different set of parameters for each of these functions. The parameters may differ by their datatype or by the number or parameters.

When one of these overloaded functions is called, the compiler selects one of these functions based on the arguments passed to parameters in the function call.

Quick Example

The following is quick code snippet to demonstrate the function overloading with two functions named with the same name sum. They differ by the number of parameters declared in its definition.

class Calculator {
public:
    int sum(int a, int b) {
        return a + b;
    }
    int sum(int a, int b, int c) {
        return a + b + c;
    }
};

Function Overloading is a form of Polymorphism, because we have more than one function with the same name but different behaviour. Polymorphism means many forms, and here we have a function that occurs in many forms.

ADVERTISEMENT

Examples

In the following examples, we understand different types of overloading based on the number of parameters and the datatype of parameters.

1. Different number of parameters in Function Overloading

In the following example, we define a class Calculator with two functions. These two functions have same name sum. The first sum() function accepts two integers as parameters, and the second sum() function accepts three integers as parameters.

C++ Program

#include <iostream>
using namespace std;

class Calculator {
public:
    int sum(int a, int b) {
        return a + b;
    }
    int sum(int a, int b, int c) {
        return a + b + c;
    }
};

int main() {
    Calculator calc = Calculator();
    cout << "sum(2, 5) : " << calc.sum(2, 5) << endl;
    cout << "sum(2, 5, 1) : " << calc.sum(2, 5, 1) << endl;
}

Output

sum(2, 5) : 7
sum(2, 5, 1) : 8
Program ended with exit code: 0

2. Different datatypes for parameters in Function Overloading

In the following example, the two functions have same name sum and the same number of parameters 2. But they differ based on the datatype of parameters. The first sum() function accepts two integers for its parameters, and the second sum() function accepts two double values for its parameters.

Based on the datatype of values passed in the function call, compiler resolves which function to be called during the compile stage.

C++ Program

#include <iostream>
using namespace std;

class Calculator {
public:
    int sum(int a, int b) {
        return a + b;
    }
    double sum(double a, double b) {
        return a + b;
    }
};

int main() {
    Calculator calc = Calculator();
    cout << "sum(2, 5) : " << calc.sum(2, 5) << endl;
    cout << "sum(1.1, 3.2) : " << calc.sum(1.1, 3.2) << endl;
}

Output

sum(2, 5) : 7
sum(1.1, 3.2) : 4.3
Program ended with exit code: 0

Conclusion

In this C++ Tutorial, we learned what Function Overloading is, and how to use Function Overloading to define multiple functions with same name but different set of parameters, with the help of examples.