Polymorphism
Polymorphism
Polymorphism
Polymorphism means implementing one
thing in many forms(“Poly” means many
and “morphs” means forms)
In C++ we have two types:
1)Compile time polymorphism(or early
binding or static binding), Examples:
Function overloading and Operator
overloading.
2) Run time polymorphism( or late binding
or Dynamic binding), achieved with the help
of virtual functions/pure virtual
functions/abstract classes
Compile Time Polymorphism
(or Early binding or Static
binding)
The concept of binding refers to the linking of
function call to the code of the function to be
executed in response to the function call.
•In compile time polymorphism ,static binding is
performed.
•In this compiler makes the decision regarding
selection of appropriate function to be called in
response to function call at compile time.
•During early binding, compiler considers the type of
pointer only, and a particular function call is decided
there only, no concern is there for what type of
object’s address is assigned to base pointer
•This is because all the address information requires
to call a function is known at compile time.
• It is also known as early binding as
decision of binding is made by the
compiler at the earliest possible moment.
• Since all the information needed to call a
function is available at the compile time,
so early binding results in faster
execution of a program
• The disadvantage is lack of flexibility.
• The compile time polymorphism is
implemented using function overloading
and operator overloading
Run time Polymorphism
(Late binding or Dynamic
binding)
• In this dynamic binding is performed
• In dynamic binding the decision regarding the
selection of appropriate function to be called is made
by compiler at run time.
• In this type of polymorphism, compiler determines
the type of object at runtime(or address of what type
of object is being assigned to base class pointer),
and then binds the function call.
• This is because the information pertaining to the
selection of appropriate function definition
corresponding to a function call is known only at the
run time.
• It is also called the late binding as the compiler
delays the binding decision until run time.
• Run time polymorphism offers flexibility as
compared to compile time polymorphism
as all decisions are taken at run time
• A function call is not resolved until runtime,
hence this kind of polymorphism results in
somewhat slower execution of code
• Run time polymorphism can be achieved
through virtual functions
Compile time Polymorphism(or Early
binding)Example-1
Function overloading
#include<iostream> int main()
#include<conio.h> {
using namespace std; overloading obj1;
class overloading int square,rectangle;
{ float circle;
public: square=obj1.area(5);
int area(int side) cout<<"\n Area of square
{ is:"<<square;
return (side*side); rectangle=obj1.area(3,4);
} cout<<"\n Area of rectangle
int area(int length, int breadth) is:"<<rectangle;
{ circle=obj1.area(3.4f);
return (length*breadth); cout<<"\n Area of circle
} is:"<<circle;
float area(float radius) return 0;
{ }
return (3.14*radius*radius);
}
};
Compile time Polymorphism(or Early
binding)Example-2
Operator overloading
#include<iostream> void show_data()
using namespace std; {
class complex1 cout<<x<<" +i"<<y<<"\n";
{ }
float x,y; };
public: int main()
complex1() {
{ complex1
x=0.0; o1(2.7,3.6),o2(4.1,5.7),o3;
y=0.0; o3=o1+o2;
} //o3=o1.operator+(o2);
complex1(float real,float imag) o3.show_data();
{ }
x=real;
y=imag;
}
complex1 operator+(complex1
obj1)
{
complex1 temp;
temp.x=x+obj1.x;
temp.y=y+obj1.y;
return temp;
Pointer to Base and Pointer to derived
• We can have a pointer to base(Base class pointer) and pointer to derived
(Derived class pointer)
• Base pointer can point towards base class as well as derived class(i.e. We can
assign the address of base class object / or we can assign the address of derived
class object to base class pointer)
• Although base class pointer can point towards derived class, but it can access
only those features of derived class, which are common in both classes(i.e.
Inherited features from base), hence base class pointer cannot access the
specific features of derived class directly.
• If we want to access the specific features of derived class with the help of base
class pointer, then we need to typecast it with the derived class pointer
• Derived class pointer can point towards derived class only(i.e. We can assign
address of only derived class object to derived class pointer), if we try to assign
address of base class object to derived class pointer, then error will arise(So, we
can say derived class pointer cannot point towards base class)
Program example-Pointer to Base and Pointer to derived
#include<iostream> class DC:public BC
using namespace std; {
class BC public:
{ void printDC()
public: {
void printBC() cout<<"\nPrinting
{ message in derived
cout<<"\nPrinting class"<<endl;
}
message in base
void show()
class"<<endl;
{
} cout<<"\nshow() of
void show() derived class"<<endl;
{ }
cout<<"\nshow() of };
base class"<<endl;
} //Continued to next
}; slide………….
Program example-Pointer to Base and Pointer to derived….Continued
All the address information requires to call Information pertaining to the selection of
a function is known at compile time. appropriate function definition
corresponding to a function call is known
only at the run time.
If derived class do not redefine virtual function of If derived class do not redefine virtual function of
base class, then it does not affect compilation. base class, then compilation error occurs.
All derived class may or may not redefine virtual All derived class must redefine pure virtual function
function of base class. of base class[ if they are not redefining, then derived
class will also become abstract in nature]