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;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s