Function Overloading in C++

Function Overloading is an example of polymorphism feature. It is a feature in C++ where two or more functions can have the same name but different parameters.

void test1(){ }

int test1(int a) { }

float test1(int a,float b){ }

All three functions are overloaded functions because argument(s) passed to these functions are different. Overloaded functions may or may not have different return type but it should have different parameters.

Three ways to overload the functions

There are two ways to overload the method in C++ :

  • By changing number of arguments or parameters

void sum()
{
      int a, b;
      cin>>a>>b;
      cout<<a+b;
}
void sum(int a, int b)
{
      cout<<a+b;
}
void sum(int a, int b, int c)
{
      cout<<a+b+c;
}

  • By changing the data type

void sum(int a, int b)
{
           cout<<a+b;
}
void sum(float a, float b)
{
            cout<<a+b+c;
}

  • Sequence of arguments

void Area(float l,int b)
{
cout<<“\n\tArea of Rectangle is : “<<l*b;
}
void Area(int l,float b)
{
cout<<“\n\tArea of Rectangle is : “<<l*b;
}

Leave a comment