Same function name and
different parameters is called function overloading.
It may contain same function
name and same parameters with different data types and order (number of
arguments).
It can be done in base as well as
derived class.
It provides fast execution
than overriding.
It may or may not require
inheritance.
Example:
compile time polymorphism
Example:
void display(int a);
void display(int a,int b);
Example 1(same function name and different parameter with
different order(no of arguments))
#include
<iostream.h>
#include
<conio.h>
void display(int a)
{
cout << "value of a: " << a << endl;
}
void display(int a, int b)
{
cout << "sum of two no :"<<a+b<<endl;
}
void display(int a, int b,float c)
{
cout << "sum of three no :"<<a+b+c;
}
void main()
{
clrscr();
display(23);
display(23,27);
display(23,27,23.67);
getch();
}
Output:
value of a:23
sum of two no :50
sum of three no :73.67
Example 2: same function name and
same parameters with different data types
#include
<iostream.h>
#include
<conio.h>
void display(int a)
{
cout << "value of a: "
<< a << endl;
}
void
display(double b)
{
cout << "value of b:
"<< b << endl;
}
void
display(string str)
{
cout << "value of
str:"<<str;
}
void
main()
{
clrscr();
display(23);
display(23.78);
display("suryosnata");
getch();
}
Output:
value
of a:23
value
of b:23.78
value
of str: suryosnata
No comments:
Post a Comment