10C++ Function Overloading (With Examples)
10C++ Function Overloading (With Examples)
In this tutorial, we will learn about the function overloading in C++ with
examples.
A DV E RT I S E M E N T S
In C++, two functions can have the same name if the number and/or type of
arguments passed is different.
These functions having the same name but different arguments are known as
overloaded functions. For example:
Notice that the return types of all these 4 functions are not the same.
Overloaded functions may or may not have different return types but they must
have different arguments. For example,
// Error code
int test(int a) { }
double test(int b){ }
Here, both functions have the same name, the same type, and the same number
of arguments. Hence, the compiler will throw an error.
#include <iostream>
using namespace std;
int main() {
Output
Absolute value of -5 = 5
Absolute value of 5.5 = 5.5
#include <iostream>
using namespace std;
int main() {
int a = 5;
double b = 5.5;
Output
Integer number: 5
Float number: 5.5
Integer number: 5 and double number: 5.5
A DV E RT I S E M E N T S
Here, the display() function is called three times with different arguments.
Depending on the number and type of arguments passed, the corresponding
display() function is called.
The return type of all these functions is the same but that need not be the case
for function overloading.
Note: In C++, many standard library functions are overloaded. For example,
the sqrt() function can take double , float , int, etc. as parameters.
This is possible because the sqrt() function is overloaded in C++.
A DV E RT I S E M E N T S
Related Tutorials
Tutorials Examples
Join our newsle er for the latest Python 3 Tutorial Python Examples
updates.
JavaScript Tutorial JavaScript Examples
Swi Tutorial
C# Tutorial
DSA Tutorial
Company Apps
Privacy Policy
Contact
Blog
Youtube