Polymorphism means many forms (You can respond
same message in different ways). In simple words, it is the ability of a
message to be displayed in more than one form.
Example 1(same function name and different parameter with different order(no of arguments))
Runtime time Polymorphism
Example : same method name and same parameters with same data types,return type and order(number of arguments)
Real life example: A person at a same time can travel to any places in
different ways by car, bus, train, flight and walking .
Types:
§ Compile time Polymorphism
§ Runtime Polymorphism
Compile
time Polymorphism
The
Polymorphism which occurs at compile time is called Compile time Polymorphism
Compile
time polymorphism is called as static polymorphism or static binding or early
binding
It
provides fast execution because occurs at compile time.
Example:
Function overloading (Same function name and different parameter) and Operator
overloading
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
Runtime time Polymorphism
The
Polymorphism which occurs at run time is called Run time Polymorphism
Run
time polymorphism is called as dynamic polymorphism or dynamic method dispatch
or dynamic binding or late binding.
It
provides slow execution because occurs at run time.
Example:
Method overriding (Same method name and same parameter).
Example : same method name and same parameters with same data types,return type and order(number of arguments)
#include<iostream.h>
#include<conio.h>
class
A
{
public:
void
display(int a)
{
cout
<< "value of a :"<<a<<endl;
}
};
class
B:public A
{
public:
void
display(int a)
{
cout
<< "square value of a :"<<a*a;
}
};
void
main()
{
clrscr();
A a1;
a1.display(5);
B
b1;
b1.display(5);
getch();
}
Output:
value of a:5
square value of a:25
Python is an open-source, high level, interpreted programming language which offers great opportunities for object-oriented programming. Choosing a python training program from Red Prism Group, which is best training institute for python in noida.
ReplyDelete