Function Overloading in C++

Function overloading in C++ allows you to define multiple functions with the same name, but they must have different types of parameters or different numbers of parameters. You cannot overload functions by just changing their return type. The compiler distinguishes overloaded functions based on their parameter lists.

Example 1: Overloading with different types of parameter

Function overloading with different types of parameters means that you can define multiple functions with the same name but with different types of arguments. The C++ compiler will distinguish between the overloaded functions based on the types of arguments provided when the function is called.

Overloading with Different Types of Parameter

C++ Code:

#include <iostream>
using namespace std;

// Function that takes an integer
int multiply(int a, int b) {
return a * b;
}

// Function that takes a double
double multiply(double a, double b) {
return a * b;
}

// Function that takes an integer and a double
double multiply(int a, double b) {
return a * b;
}

int main() {
cout << “Multiply two integers: ” << multiply(3, 4) << endl; // Calls int version
cout << “Multiply two doubles: ” << multiply(2.5, 4.2) << endl; // Calls double version
cout << “Multiply integer and double: ” << multiply(3, 4.5) << endl; // Calls mixed version
return 0;
}

Output

Multiply two integers: 12
Multiply two doubles: 10.5
Multiply integer and double: 13.5

Example 2: Overloading with different Number of Parameters

Function overloading based on a different number of parameters means that you can define multiple functions with the same name but with a different number of arguments. The C++ compiler will differentiate between them based on how many parameters are passed when the function is called.

Overloading with Different Number of Parameter

Code in C++

#include <iostream>
using namespace std;

// Function with 1 integer parameter
void display(int num) {
cout << “Integer: ” << num << endl;
}

// Function with 1 double parameter
void display(double num) {
cout << “Double: ” << num << endl;
}

// Function with 2 parameters
void display(int num1, double num2) {
cout << “Integer: ” << num1 << ” and Double: ” << num2 << endl;
}

int main() {
display(10); // Calls function with 1 integer
display(3.14); // Calls function with 1 double
display(10, 3.14); // Calls function with 2 parameters
return 0;
}

Output:

Integer: 10
Double: 3.14
Integer: 10 and Double: 3.14

 

Important:

In C++, function overloading requires the functions to have different parameter lists (either in terms of the number of parameters or the types of parameters). You cannot overload functions only by changing the return type.

Invalid Overloading (Only Return Type is different)

In your example, you have:

int add(int a, int b) {
// Some code
}

double add(int a, int b) {
// Some code
}

Here, the functions are not overloaded because they have the same parameter list (int a, int b), and the only difference is the return type (one returns int, the other returns void). The C++ compiler cannot distinguish these functions based solely on the return type. This will lead to a compiler error.

Advantages of Function Overloading in C++

  • Improves Code Readability: Using the same function name for similar tasks makes the code more readable and easier to understand.

  • Reduces Code Duplication: You can use a single function name for operations that differ only by the type or number of parameters, reducing the need for multiple similar function names.

  • Simplifies Maintenance: Since you only need to maintain one function name for similar tasks, code maintenance becomes simpler and less error-prone.

  • Increases Flexibility: Function overloading allows you to perform the same operation on different types of data (e.g., int, float, double) without changing the function name.

  • Supports Polymorphism: Overloading allows functions to behave differently depending on the type or number of arguments, which is a key feature of polymorphism in C++.