Do you wear the same clothes for a party and at the time you go to sleep?
No Right?
For different occasions you dress differently.
You are the same person which changes behaviour or appearance according to the situation.
The literal meaning of polymorphism is having different forms or shapes.
In Object-Oriented Programming, polymorphism is a way to perform a single action in different ways.
In C ++, polymorphism can be defined as the ability of member functions to perform the various tasks on which it is acting upon.
But that’s what we learnt in function overloading and function overriding.
Right?
Function overloading falls into polymorphism and so does function overriding, as it allows us to use the same function in different manners.
*Polymorphism is the capability of member functions to do different things based on the object that it is acting upon.
Types of polymorphism
Polymorphism is of two types –
- Compile time Polymorphism
- Runtime Polymorphism
Compile time Polymorphism
Compile time polymorphism is achieved through function overloading.
At compile time, C++ knows which function to call by checking the function signatures.
Let’s see an example for the same.
class Hello{
public:
void hello(){
cout<<“Hello“;
}
void hello(string name){
cout<<“Hello “<<name;
}
};
int main() {
Hello h1;
h1.hello();
h1.hello(“Rahul“);
}
Every function overloading operation ultimately is a polymorphism, just like the one given above.
Can you guess the output of the above snippet?
The output will be
Hello Hello Rahul
First, we are calling the member function hello() without any parameters and the second time we are passing the name as a parameter.
This is what compile time polymorphism is all about.
Runtime Polymorphism
Run time polymorphism is achieved through function overriding.
In case of function overriding the function to call is determined at runtime, hence it is known as run time polymorphism.
class Hello{
public:
virtual void hello(){
cout<<“Hello“;
}
};
class HelloAgain: public Hello{
public:
void hello(){
cout<<“Hello Again.“;
}
};
int main() {
Hello *h1; //pointer object of type Hello
HelloAgain h2; //object of type HelloAgain
h1 = &h2;
h1 -> hello(); //calling function hello()
}
This snippet in which the HelloAgain extends Hello class and hello() is overridden is an example of run time polymorphism.
The output of the previous snippet will be
Hello Again.
The function hello() in class HelloAgain derived from Hello class overrides the hello() function present in Hello class.
But wait, did you notice any difference in the hello() function from the base class?
virtual void hello()
What is virtual doing here?
Actually, this is a virtual function.
A virtual function is the one written with the keyword virtual and defined in the base class which is further overridden in the derived class.
It is impossible to achieve runtime polymorphism without using pointers and virtual function.
Virtual functions ensure that the correct function is called.
To summarize
- Virtual functions are written using the keyword “virtual”, these functions are used to achieve runtime polymorphism and they ensure that the correct function is called during overriding.
- Polymorphism is a way to perform a single action in different ways.
- Run-time polymorphism is achieved through function overriding
- Polymorphism is a way to perform a single action in different ways.
- Polymorphism is of two types – Compile time Polymorphism and Runtime Polymorphism.
- Compile-time polymorphism is achieved through function overloading.
- Runtime polymorphism is achieved through function overriding.
- Runtime polymorphism cannot be achieved without using pointers and virtual functions.