Polymorphism is one of the crucial features of oop. It is simply means 'one name , multiple forms'. Any operation may exhibit different behaviours in different instances.
Definition:
The ability to take more than one form is known as polymorphism. An operation may exhibit different behaviors in different instances.
Ex: Consider the operation of addition , for two numbers , the operation will generate a sum. If two operands are strings then the operation would produce a third string by concatenation.
Types of polymorphism:
1. Compiler time polymorphism or Early Binding or Static BInding
2. Run time polymorphism or Late Binding or dynamic Binding
Compile Time Polymorphism
1. Selecting a function in normal way during compilation time is known as compile time polymorphism or static binding.
2. During compilation time , the cpp compiler determines which function is used , based on parameters passed to the function.
3. Static binding is achieved by using function overloading and operator overloading.
4. Overloading refers to the same thing for different purpose , in function overloading. We can use the same function name to perform variety of task.
#include<iostream.h>
#include<conio.h>
double volume (double l, double b, double h )
{
return(l*b*h);
}
double volume(double r, double h)
{
return(3.14*r*r*h);
}
void main( )
{
cout<<"\n\tVolume of the box is→"<<volume(1.2,2.2,3.4);
cout<<"\n\tVolume of the cylinder is→"<<volume(1.2,4.2);
getch( );
}
Output:-
Volume of the box is→8.976
Volume of the cylinder is→18.99072
Run Time Polymorphism
. In some situation, where the function name and the prototype is the same in both the base and derived classes, since the prototype of function is the same in both the places, the function is not overloaded and therefore static binding does not apply.
2. In such cases , the appropriate member function is selected while program is running and this is known as run time polymorphism.
3. Virtual function is used to achieve run time polymorphism.
4. At run time , when it known what class objects are under consideration the appropriate function is done dynamically at run time.
5. Dynamic binding requires the use of pointers to objects.
Definition:
The ability to take more than one form is known as polymorphism. An operation may exhibit different behaviors in different instances.
Ex: Consider the operation of addition , for two numbers , the operation will generate a sum. If two operands are strings then the operation would produce a third string by concatenation.
Types of polymorphism:
1. Compiler time polymorphism or Early Binding or Static BInding
2. Run time polymorphism or Late Binding or dynamic Binding
Compile Time Polymorphism
1. Selecting a function in normal way during compilation time is known as compile time polymorphism or static binding.
2. During compilation time , the cpp compiler determines which function is used , based on parameters passed to the function.
3. Static binding is achieved by using function overloading and operator overloading.
4. Overloading refers to the same thing for different purpose , in function overloading. We can use the same function name to perform variety of task.
#include<iostream.h>
#include<conio.h>
double volume (double l, double b, double h )
{
return(l*b*h);
}
double volume(double r, double h)
{
return(3.14*r*r*h);
}
void main( )
{
cout<<"\n\tVolume of the box is→"<<volume(1.2,2.2,3.4);
cout<<"\n\tVolume of the cylinder is→"<<volume(1.2,4.2);
getch( );
}
Output:-
Volume of the box is→8.976
Volume of the cylinder is→18.99072
Run Time Polymorphism
. In some situation, where the function name and the prototype is the same in both the base and derived classes, since the prototype of function is the same in both the places, the function is not overloaded and therefore static binding does not apply.
2. In such cases , the appropriate member function is selected while program is running and this is known as run time polymorphism.
3. Virtual function is used to achieve run time polymorphism.
4. At run time , when it known what class objects are under consideration the appropriate function is done dynamically at run time.
5. Dynamic binding requires the use of pointers to objects.
0 Comments:
Post a Comment