C++ Programming

Function Overloading in C++

Function Overloading

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: "<<endl;
cin >> x1 >> x2;
cout<<endl;
cout << "Enter two float numbers please:"<<endl;
cin >> f1 >> f2;
cout<<endl;
cout << "Enter two string please:"<<endl;
cin >> a >> b;
cout<<endl;
sum( x1, x2);
// calls the first 'sum' function. sum( x1, x2);
sum( f1, f2);
// calls the 2nd 'sum' function. sum( f1, f2);
sum( a, b);
// calls the 3rd 'sum' function. sum( a, b);
getch();
return 1;
}

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="<<result2<<endl;
}
//end of the 2nd finction

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="<<result3<<endl;
}
//end of the 3rd function

Complete programming example of function overloading


#include <stdio.h>
#include<conio.h>
#include<iostream>
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: "<<endl;
cin >> x1 >> x2;
cout<<endl;
cout << "Enter two float numbers please:"<<endl;
cin >> f1 >> f2;
cout<<endl;
cout << "Enter two string please:"<<endl;
cin >> a >> b;
cout<<endl;
sum( x1, x2); // calls the first 'sum' function. sum( x1, x2);
sum( f1, f2); // calls the 2nd 'sum' function. sum( f1, f2);
sum( a, b); // calls the 3rd 'sum' function. sum( a, b);
return 1;
}
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
void sum(float p,float q) //defining the 2nd function
{
float result2;
result2=p+q;
cout<<"The sum of the two float number is="<<result2<<endl;
} //end of the 2nd finction
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="<<result3<<endl;
} //end of the 3rd function


Watch Videos of C++ programming