In this lesson, we will learn about function overloading in C++, with examples to better understand the topic.
In C++, two functions with different numbers and or types of arguments can share the same name. Overloaded functions are those that have the same name but different arguments.
For instance:
// same name different arguments // same name different arguments int funOverloading() {} int funOverloading(int a) {} float funOverloading(double a) {} int funOverloading(int a, double b) {}
Here, each of the four functions is overloaded.
You’ll see that none of these 4 functions has the same return types. Different return types may or may not be included in overloaded functions, but different arguments are a requirement.
For instance,
int funOverloading(int a) { } float funOverloading(int a) { }
These functions are identical in terms of name, datatype and the number of parameters. The compiler will therefore produce an error.
Example: C++ Overloading using different types of arguments
#include <iostream> using namespace std; // C++ function with float type parameter bool isPositive(double number) { if (number >= 0.0) cout << number << " is a positive double" << endl; else cout << number << " is a negative double" << endl; return true; } // C++ function with int type parameter bool isPositive(int number) { if (number >= 0) cout << number << " is a positive number" << endl; else cout << number << " is a negative number" << endl; return true; } int main() { //C++ call function with int type parameter isPositive(2); isPositive(-12); //C++ call function with double type parameter isPositive(2.234); isPositive(-2.89); return 0; }
Output
2 is a positive number -12 is a negative number 2.234 is a positive double -2.89 is a negative double
The isPositive()
function in this code is overloaded. The function call makes the appropriate call to the related function based on the kind of parameter given. Like isPositive(2.234);
calls bool isPositive(double number)
and isPositive(2)
calls bool isPositive(int number)
.
Example: C++ Overloading using a different number of arguments
#include <iostream> using namespace std; // C++ function with 3 int type parameters int numberSum(int number1, int number2, int number3) { return (number1 + number2 + number3); } // C++ function with 2 int type parameters int numberSum(int number1, int number2) { return (number1 + number2); } int main() { //C++ call function with 2 int type parameters cout << "Two number sum = " << numberSum(10, 10) << endl; //C++ call function with 3 int type parameters cout << "Three number sum = " << numberSum(10, 10, 10) << endl; return 0; }
Output
Two number sum = 20 Three number sum = 30
In this case, two calls to the numberSum()
function are made with various arguments. The appropriate numberSum()
function is called based on the number and type of arguments given. Like numberSum(10,10)
calls int numberSum(int number1,int number2)
function with two arguments and numberSum(10,10,10)
calls int numberSum(int number1,int number2,int number3)
function with three arguments.
This concludes the C++ Function Overloading lesson. In The next lesson, you will learn about recursion in C++ and its usage.