Function overloading in C++ and OOP with examples
In this tutorial, we will learn about the followings;
What is function overloading?
Function overloading is a technique that allows to define and use more than one functions with the same scope and same name. But each function has a unique, which can be derived from the followings;
- argument type
- function name
- Total number of arguments
- Sequence of arguments
- Name of arguments
- return type.
Example of Function overloading in C++
#include <iostream>
using namespace std;
void Sum_T4Tutorials(int a, int b)
{
cout << "sum = " << (a + b);
}
void Sum_T4Tutorials(double a, double b)
{
cout << endl << "sum = " << (a + b);
}
// Driver code
int main()
{
Sum_T4Tutorials(4, 6);
Sum_T4Tutorials(8.1, 7.4);
return 0;
}
Output
sum = 10
sum = 15.5