When more than one function is defined with the same name but different arguments, then this phenomenon is called function overloading. So, function overloading allows multiple function definitions with the same function name.
The functions in this case are called overloaded functions.
Let’s take an example to understand function overloading:
In the program below we make a three function with same name but both three functions take different argument.
We declare 1st function with name sum which take two integer values and add them and display the result.
void sum(int x, int y); // Function prototype of the first one
The Second functions declares also with name sum but its take two float values and add them and display the result.
void sum(float p, float q); //// Function prototype of the 2nd Function
Now the 3rd function also named sum but its take two string value and then concate these two string.
void sum(string a, string b); //// Function prototype of the 3rd Function
Now in main function we called these three function with same name but passed the three different arguments.
int main() { int x1, x2, s1; float f1, f2, s2; string a,b; cout << "Enter two integer numbers please: "< > x1 >> x2; cout< > f1 >> f2; cout< > a >> b; cout<
Now we define all three functions.
First Function take integer values
void sum(int x, int y) { //defining the 1st 'sum' function // Here two integer numbers are added. int result1; result1 = x+y; cout << "The summation of two integers is: "<< result1<< endl; } //End of the 1st 'sum function
2nd function takes two float values
void sum(float p,float q) //defining the 2nd function { float result2; result2=p+q; cout<<"The sum of the two float number is="<
3rd Function takes two string values
void sum(string a,string b) //defining the 3rd function { string result3; result3=(a.append(b)); cout<<"The sum of the two float number is="<
#include #include #include using namespace std; void sum(int x, int y); void sum(float p, float q); void sum(string a, string b); int main() { int x1, x2, s1; float f1, f2, s2; string a,b; cout << "Enter two integer numbers please: "< > x1 >> x2; cout< > f1 >> f2; cout< > a >> b; cout< Watch Videos of C++ programming
Declaring and Initializing Local Variable in C
Declaring and Initializing Global Variable in C Language
Printf Function In C Language
Sacnf Function In C Language
for Loop in C Language
While Loop in C Language
Do While Loop in C Language
Switch Case Statement IN C
IF -Else Statement IN C
Break Statement IN C /C++
User Define Function IN C /C++
Cout/Cin Function IN C /C++
Array IN C /C++
Two Dimensional Array IN C /C++
String In IN C /C++
Pointer In IN C /C++
Const and volatile keyword IN C /C++
Class and Object in C++
Function Over Loading in C++
Character Array In C++
Constructor in C++
Operator overloading in C++
Function Templates in C++
ATM Sample Project In Simple C Language
Find Area of Shape Using Function In C++