Polymorphism is a mechanism that allows a developer to have more than one function with same name but different signature. In function overloading, one can make use of more than one function with different signature as well. Hence polymorphism is implemented using function overloading, where one can have more than one functions possess same name but different functionality and behavior.
Example:
#include<iostream.h>
#include<conio.h>
int add(int ar[]);
float add(float arr[]);
void main()
{
int a[10],i,isum;
float b[5],fsum;
clrscr();
cout<<"\nEnter 10 numbers ";
for(i=0;i<10;i++)
{
cin>>a[i];
}
cout<<"\nEnter 5 Float numbers ";
for(i=0;i<5;i++)
{
cin>>b[i];
}
isum=add(a);
fsum=add(b);
cout<<"\nThe addition of 10 integer numbers is "<<isum;
cout<<"\nThe addition of 05 Float numbers is "<<fsum;
getch();
}
int add(int x[])
{
int sum = 0, i;
for(i=0;i<10;i++)
{
sum = sum+x[i];
}
return sum;
}
float add(float x[])
{
float sum = 0, i;
for(i=0;i<5;i++)
{
sum = sum+x[i];
}
return sum;
}
As shown in above example there are two different functions add, having same name but argument and return type differs and performs different tasks. Based on the argument appropriate function will be called. Function selection will be done at compile time itself.