Function overloading allows us to create multiple functions with the same name but with different parameter lists. C++ decides which function to call based on the number, type, or order of the arguments.
Function overloading means defining multiple functions with the same name but different parameters.
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
Both functions are named add, but their parameter types
are different.
Function overloading allows related operations to use one common function name.
#include <iostream>
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int main() {
std::cout << add(10, 20) << std::endl;
std::cout << add(10.5, 20.5);
return 0;
}
C++ selects the appropriate add() function based on the
arguments.
Functions can be overloaded by changing the number of parameters.
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
The first function accepts two arguments, while the second accepts three.
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
int main() {
std::cout << add(10, 20) << std::endl;
std::cout << add(10, 20, 30);
return 0;
}
The number of arguments determines which overloaded function is called.
Functions can also be overloaded by changing the parameter data type.
int square(int number) {
return number * number;
}
double square(double number) {
return number * number;
}
One function accepts an int, while the other accepts a
double.
int square(int number) {
return number * number;
}
double square(double number) {
return number * number;
}
int main() {
std::cout << square(5) << std::endl;
std::cout << square(5.5);
return 0;
}
The integer argument selects the integer version, while the decimal argument selects the double version.
void display(int number) {
std::cout << "Integer: "
<< number;
}
void display(double number) {
std::cout << "Double: "
<< number;
}
void display(char letter) {
std::cout << "Character: "
<< letter;
}
All three functions have the same name but accept different data types.
The order of different parameter types can also be changed.
void display(int number, double value) {
std::cout << number << " "
<< value;
}
void display(double value, int number) {
std::cout << value << " "
<< number;
}
The parameter order makes the function signatures different.
For overloading, the parameter list is important. It includes the number, types, and order of parameters.
void show(int number) {
}
void show(double number) {
}
These functions have different parameter types, so they can be overloaded.
You cannot overload functions only by changing their return type.
The following is invalid:
int getValue() {
return 10;
}
double getValue() {
return 10.5;
}
Both functions have the same name and parameter list. Changing only the return type is not sufficient for overloading.
int multiply(int a, int b) {
return a * b;
}
int multiply(int a, int b, int c) {
return a * b * c;
}
int main() {
std::cout << multiply(2, 3) << std::endl;
std::cout << multiply(2, 3, 4);
return 0;
}
The correct function is selected according to the number of arguments.
void print(int value) {
std::cout << "Integer: "
<< value;
}
void print(double value) {
std::cout << "Decimal: "
<< value;
}
int main() {
print(100);
std::cout << std::endl;
print(25.5);
return 0;
}
#include <iostream>
#include <string>
void show(std::string name) {
std::cout << "Name: "
<< name;
}
void show(int age) {
std::cout << "Age: "
<< age;
}
int main() {
show("Rahul");
std::cout << std::endl;
show(20);
return 0;
}
The appropriate function is selected based on the argument type.
int calculate(int a, int b) {
return a + b;
}
int calculate(int a, int b, int c) {
return a + b + c;
}
int main() {
std::cout << calculate(10, 20)
<< std::endl;
std::cout << calculate(10, 20, 30);
return 0;
}
Function overloading can make calculator-style programs easier to organize.
int area(int side) {
return side * side;
}
int area(int length, int width) {
return length * width;
}
int main() {
std::cout << "Square Area = "
<< area(5) << std::endl;
std::cout << "Rectangle Area = "
<< area(5, 10);
return 0;
}
The same function name can represent related operations with different parameter lists.
Care is needed when combining function overloading with default arguments because calls can become ambiguous.
void show(int a) {
std::cout << a;
}
void show(int a, int b = 10) {
std::cout << a << " " << b;
}
Calling show(5) can create ambiguity because both functions
can accept one argument.
C++ can sometimes convert an argument to match an overloaded function. This can affect which function is selected.
void show(int value) {
std::cout << "Integer";
}
void show(double value) {
std::cout << "Double";
}
int main() {
show(10);
return 0;
}
The exact matching int version is selected for the integer
argument.
void display(int value) {
std::cout << "Integer";
}
void display(double value) {
std::cout << "Double";
}
void display(std::string value) {
std::cout << "String";
}
A function name can have multiple overloaded versions as long as their parameter lists are different.
Function overloading is an example of compile-time polymorphism. The compiler determines which overloaded function should be called during compilation.
void show(int value) {
std::cout << "Integer";
}
void show(double value) {
std::cout << "Double";
}
void print(char value) {
std::cout << "Character: "
<< value;
}
void print(int value) {
std::cout << "Integer: "
<< value;
}
int main() {
print('A');
std::cout << std::endl;
print(65);
return 0;
}
The character argument selects the character version, while the integer argument selects the integer version.
#include <iostream>
#include <string>
void student(std::string name) {
std::cout << "Name: "
<< name;
}
void student(std::string name, int age) {
std::cout << "Name: "
<< name << std::endl;
std::cout << "Age: "
<< age;
}
int main() {
student("Amit");
std::cout << std::endl;
student("Amit", 20);
return 0;
}
The same function name provides different levels of student information.
int maximum(int a, int b) {
return (a > b) ? a : b;
}
int maximum(int a, int b, int c) {
int max = a;
if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
return max;
}
int main() {
std::cout << maximum(10, 20)
<< std::endl;
std::cout << maximum(10, 20, 30);
return 0;
}
#include <iostream>
double area(double radius) {
return 3.14159 * radius * radius;
}
double area(double length, double width) {
return length * width;
}
int main() {
std::cout << "Circle Area = "
<< area(5.0)
<< std::endl;
std::cout << "Rectangle Area = "
<< area(5.0, 10.0);
return 0;
}
Without overloading, related operations might require different names:
int addTwo(int a, int b) {
return a + b;
}
int addThree(int a, int b, int c) {
return a + b + c;
}
With overloading, one meaningful name can be used:
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
The general syntax for function overloading is:
returnType functionName(type parameter1) {
// code
}
returnType functionName(type parameter1, type parameter2) {
// code
}
The functions must have different parameter lists.
| Concept | Meaning |
|---|---|
| Function Overloading | Using the same function name with different parameter lists. |
| Parameter Count | Functions can have different numbers of parameters. |
| Parameter Type | Functions can use different parameter data types. |
| Parameter Order | Different parameter orders can create different overloads. |
| Return Type | Cannot be used alone to overload a function. |
| Compile-Time Polymorphism | Function overloading is resolved by the compiler. |
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
Question: Which of the following is required for function overloading in C++?