Function overloading in C++ by R4R Team

Function overloading in C++
Function overloading is a feature in C++ where two or more functions can have the same name but different parameters.

Example-
int sum(int a,int b,int c)
{
}
int sum(int a, int b)
{
}
int sum(double a,double b)
{
}
-Above three function are of same name but have different type of parameter and different number of parameter, so above three function are the example of function overloading.

program:

#include< iostream>

using namespace std;
class r4r
{
public:
int sum(int a,int b, int c)
{
cout<<"Sum is: "<< a+b+c<< endl;
}
int sum(int a,int b)
{
cout<<"Sum is: "<< a+b<< endl;
}
int sum(double a,double b)
{
cout<<"Sum is: "<< a+b<< endl;
}
};

int main()
{
r4r obj;
obj.sum(2,3,4);
obj.sum(1,2);
obj.sum(3.2,4.1);
return 0;
}


output-

Sum is: 9
Sum is: 3
Sum is: 7.3




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!