C++ Polymorphism: Real Life Example of Polymorphism
C++ Polymorphism: Real Life Example of Polymorphism
The term "Polymorphism" is the combination of "poly" + "morphs" which means many forms. It is a
greek word
Compile time polymorphism: The overloaded functions are invoked by matching the type and
number of arguments. This information is available at the compile time and, therefore, compiler selects
the appropriate function at the compile time. It is achieved by function overloading and operator
overloading which is also known as static binding or early binding. Now, let's consider the case where
function name and prototype is same.
Run time polymorphism: Run time polymorphism is achieved when the object's method is invoked
at the run time instead of compile time. It is achieved by method overriding which is also known as
dynamic binding or late binding.
#include <iostream>
using namespace std;
class Animal {
public:
void eat(){
cout<<"Eating...";
}
};
class Dog: public Animal
{
public:
void eat()
{ cout<<"Eating bread...";
}
};
int main(void) {
Dog d = Dog();
d.eat();
return 0;
}
C++ Run time Polymorphism Example: By using two derived class
Let's see another example of run time polymorphism in C++ where we are having two derived classes.
#include <iostream>
using namespace std;
class Shape { // base class
public:
virtual void draw(){ // virtual function
cout<<"drawing..."<<endl;
}
};
class Rectangle: public Shape // inheriting Shape class.
{
public:
void draw()
{
cout<<"drawing rectangle..."<<endl;
}
};
class Circle: public Shape // inheriting Shape class.
{
public:
void draw()
{
cout<<"drawing circle..."<<endl;
}
};
int main(void) {
Shape *s; // base class pointer.
Shape sh; // base class object.
Rectangle rec;
Circle cir;
s=&sh;
s->draw();
s=&rec;
s->draw();
s=?
s->draw();
}
Differences between compile time and run time polymorphism.
It is less flexible as mainly all the things It is more flexible as all the things
execute at the compile time. execute at the run time.
Before going into detail, let's build an intuition on why virtual functions are needed in the first place.
We created the Cricket class and derived two classes Batsman and Bowler to load features of respective
Cricket.
#include <iostream>
using namespace std;
class Cricket
{
public:
void loadFeatures()
{ cout << "Loading cricket features.\n"; }
};
class Batsman : public Cricket
{
public:
void loadFeatures()
{ cout << "Loading Batsman.”<<endl; }
};
class Bowler : public Cricket
{
public:
void loadFeatures()
{ cout << "Loading Bowler "<<endl; }
};
int main()
{
Cricket *C = new Cricket;
Batsman *b = new Batsman;
Bowler *g = new Bowler;
w->loadFeatures();
b->loadFeatures();
g->loadFeatures();
return 0;
}
Output:
Also, notice that, the l->loadFeatures(c) function calls the function of different classes depending upon
what l object is pointing.
Using virtual function made our code not only clearer but flexible too.
In the above program, cricket features is printed twice. I encourage you to add additional code on the
above program to load cricket features only once.