0% found this document useful (0 votes)
303 views6 pages

C++ Polymorphism: Real Life Example of Polymorphism

Compile time polymorphism is resolved at compile time based on function signatures, allowing for function overloading. Run time polymorphism is resolved at run time based on the object type using virtual functions and dynamic binding, allowing for overriding in derived classes. The key differences are that compile time polymorphism is faster but less flexible while run time polymorphism is slower but more flexible. Virtual functions allow for run time polymorphism in C++ by ensuring the correct overriding function is called based on the object's actual type.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
303 views6 pages

C++ Polymorphism: Real Life Example of Polymorphism

Compile time polymorphism is resolved at compile time based on function signatures, allowing for function overloading. Run time polymorphism is resolved at run time based on the object type using virtual functions and dynamic binding, allowing for overriding in derived classes. The key differences are that compile time polymorphism is faster but less flexible while run time polymorphism is slower but more flexible. Virtual functions allow for run time polymorphism in C++ by ensuring the correct overriding function is called based on the object's actual type.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

C++ Polymorphism

The term "Polymorphism" is the combination of "poly" + "morphs" which means many forms. It is a
greek word

Real Life Example Of Polymorphism


Let's consider a real-life example of polymorphism. A lady behaves like a teacher in a classroom, mother
or daughter in a home and customer in a market. Here, a single person is behaving differently according
to the situations.

There are two types of polymorphism in C++:

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.

class A // base class declaration.


{
int a;
public:
void display()
{
cout<< "Class A ";
}
};
class B : public A // derived class declaration.
{
int b;
public:
void display()
{
cout<<"Class B";
}
};
In the above case, the prototype of display() function is the same in both the base and derived class.
Therefore, the static binding cannot be applied. It would be great if the appropriate function is selected
at the run time. This is known as run time polymorphism.

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.

Let's see a simple example of run time polymorphism in C++.

// an example without the virtual keyword.

#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.

// an example with virtual keyword.

#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.

Compile time polymorphism Run time polymorphism

The function to be invoked is known at The function to be invoked is known


the compile time. at the run time.

It is also known as overloading, early It is also known as overriding,


binding and static binding. Dynamic binding and late binding.

Overloading is a compile time Overriding is a run time


polymorphism where more than one polymorphism where more than one
method is having the same name but method is having the same name,
with the different number of parameters number of parameters and the type
or the type of the parameters. of the parameters.

It is achieved by function overloading and It is achieved by virtual functions and


operator overloading. pointers.

It provides fast execution as it is known It provides slow execution as it is


at the compile time. known at the run time.

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.

C++ Virtual Function


A virtual function is a member function in base class that you expect to redefine in derived classes.

Before going into detail, let's build an intuition on why virtual functions are needed in the first place.

An Example to Begin With


Let us assume, we are working on a game (Cricket specifically).

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:

Loading Cricket Features


Loading Batsman
Loading Bowler
Example: Using Virtual Function to Solve the Problem
#include <iostream> int main()
using namespace std; {
class Cricket Loader *l = new Loader;
{ Cricket *c;
public: Batsman b;
virtual void features() Bowler g;
{ cout << "Loading cricket features.\n"; } c = &b;
}; l->loadFeatures(w);
class Batsman : public Cricket c = &g;
{ l->loadFeatures(w);
public: return 0;
void features() }
{ this->Cricket::features();
cout << "Loading Batsman features.\n";
}
};
class Bowler : public Cricket
{
public:
void features()
{
this->Cricket::features();
cout << "Loading Bowler features.\n";
}
};
class Loader
{
public:
void loadFeatures(Cricket *cricket)
{
cricket->features();
}
};

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.

You might also like