Function overloading in C++ Programming

Overloading refers to the use of the same thing for a different purpose. When the same  function name is used for different tasks this is known as function overloading. Function overloading is one of the important features of C++ and any other OO languages.

When an overloaded function is called the function with matching arguments and return type is invoked. e.g.
void border(); //function with no arguments
void border(int ); // function with one int argument
void border(float); //function with one float argument
void border(int, float);// function with one int and one float arguments

 For overloading a function prototype for each and the definition for each function that share same name is compulsory.

//function overloading
//multiple function with same name

#include<iostream.h>
#include<conio.h>
int max(int ,int);
long max(long, long);
float max(float,float);
char max(char,char);
void main()
{  
return(i1>i2?i1:i2);
}


int i1=15,i2=20;    


cout<<"Greater is "<<max(i1,i2) <<endl;   

long l1=40000, l2=38000;   

cout<<"Greater is "<<max(l1,l2) <<endl;    


float f1=55.05, f2=67.777;    


cout<<"Greater is "<<max(f1,f2) <<endl;    


char c1='a',c2='A';    


cout<<"Greater is "<<max(c1,c2) <<endl;    


getch();


}


int max(int i1, int i2)


{  


long max(long l1, long l2)


{  


return(l1>l2?l1:l2);


}


float max(float f1, float f2)


{  


return(f1>f2?f1:f2);


}


char max(char c1, char c2)


{  


return(c1>c2?c1:c2);


}

Share To:

Arogya Thapa Magar

Post A Comment:

0 comments so far,add yours