+92 332 4229 857 99ProjectIdeas@Gmail.com

Function overloading (C++)


Function overloading
Function overloading means that same name is given to more one than one function.
A same name to a function is given only if the function differs in any one of the following:
1.  The number of parameters
2.  The return type of those parameters
3.  The order of parameters in which they appear

Example
Let’s take an example of add function which has different prototypes listed below:
int add( int , int );
double add( double , double );
char add( char , char );
int add( int , int , int );
double add( double , double , double );
char add( char , char , char );
double add( int  , double );
double add( double , int );


1.  The number of parameters
First prototype of add function has 2 parameters
int add( int , int );
Second prototype of add function has 3 parameters
int add( int , int , int );

2.  The return type of those parameters
First prototype of add function has return type of integer
int add( int , int );
Second prototype of add function has return type of double
double add( double , double );

3.  The order of parameters in which they appear
Both the prototypes of add function has same number of parameters and same data type of both parameters but the order in which the parameters appear is different.
double add( int  , double );
double add( double , int );

Source Code
#include "stdafx.h"
#include "iostream"
#include "conio.h"

using namespace std;

int add(int x,int y)
{
       return x+y;
}

double add(double x,double y)
{
       return x+y;
}

char add(char x,char y)
{
       return x+y;
}

int add(int x,int y,int z)
{
       return x+y+z;
}

double add(double x,double y,double z)
{
       return x+y+z;
}

char add(char x,char y,char z)
{
       return x+y+z;
}

double add(int x,double y)
{
       return x+y;
}

double add(double x,int y)
{
       return x+y;
}

int main()
{
       cout<<"5 + 5                          = "<<add(5,5)<<endl;
       cout<<"5.6 + 5.7                      = "<<add(5.6,5.7)<<endl;
       cout<<"'A'+ '0'                       = "<<add('A','0')<<endl;
       cout<<"int('A') + int('0')            = "<<add(int('A'),int('0'))<<endl;
       cout<<"char(47) + char(50)            = "<<add(char(47),char(50))<<endl;
       cout<<"5 + 5 + 5                      = "<<add(5,5,5)<<endl;
       cout<<"5.6 + 5.7 + 5.8                = "<<add(5.6,5.7,5.8)<<endl;
       cout<<"'A'+ '0' + '1'                 = "<<add('A','0','1')<<endl;
       cout<<"int('A') + int('0') + int('1') = "<<add(int('A'),int('0'),int('1'))<<endl;
       cout<<"char(47) + char(50) + char(1)  = "<<add(char(47),char(50),char(1))<<endl;
       cout<<"5 + 5.5                        = "<<add(5,5.5)<<endl;
       cout<<"5.5 + 5                        = "<<add(5.5,5)<<endl;
   
       _getche();
       return 0;
}

Output
5 + 5                          = 10
5.6 + 5.7                      = 11.3
'A'+ '0'                       = q
int('A') + int('0')            = 113
char(47) + char(50)            = a
5 + 5 + 5                      = 15
5.6 + 5.7 + 5.8                = 17.1
'A'+ '0' + '1'                 = ó
int('A') + int('0') + int('1') = 162
char(47) + char(50) + char(1)  = b
5 + 5.5                        = 10.5
5.5 + 5                        = 10.5


Related articles

0 comments: