Polymorphism in C++

“Polymorphism” is a combination of the words “poly” and “morphs,” which meaning “many forms.” It’s a word from the Greek language. We use three main concepts in object-oriented programming: inheritance, encapsulation, and polymorphism.

Polymorphism in the Real World:

Let’s take a look at a real-world example of polymorphism. In a classroom, a lady acts as a teacher, as a mother or daughter in the home, and as a client in the market. Here, a single person acts in a variety of ways depending on the circumstances.

In C++, there are two types of polymorphism:

types of polymorphism in C
 types of polymorphism in C++:

Compile Time Polymorphism:

Overloaded functions are invoked by matching the type and quantity of parameters at compile time. Because this information is available at compile-time, the compiler selects the proper function at that time. Function overloading and operator overloading, often known as static binding or early binding, are used to achieve this. Consider the instance where the function name and prototype are the 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";  
  }  
};  

The prototype of the display() function is the same in both the base and derived classes in the example above. As a result, static binding can’t be used. It would be fantastic if the right function could be chosen at runtime. Run time polymorphism is the term for this.

Run time polymorphism:

When the object’s method is called at run time rather than compile-time, run time polymorphism is obtained. Method overriding, also known as dynamic binding or late binding, is used to do this.

Join Our Community

Join our WhatsApp Group To know more about Programming Language tips, tricks and knowledge about and how to start learning any programming language.

Difference between compile time and run time

Compile Time PolymorphismRun Time Polymorphism
At compile time, the function to be invoked is known.At runtime, the function to be invoked is known.
Overloading, early binding, and static binding are other terms for the same thing.Overriding, Dynamic binding and late binding are other terms for it.
Overloading is a compile-time polymorphism in which multiple methods with the same name but different numbers or types of parameters are created.Overriding is a sort of run-time polymorphism in which more than one method has the same name, number of parameters, and parameter type.
Function overloading and operator overloading are used to achieve this.Virtual functions and pointers are used to accomplish this.
As it is known at compile-time, it allows quick execution.It delivers delayed execution due to the fact that it is known at run time.
It is less flexible because almost everything happens at compile time.It’s more adaptable because everything happens at the same time.
Difference Between Compile Time And Run Time Polymorphism

Example of Runtime Polymorphism in C++:

Let’s look at a basic run-time polymorphism example in C++.

//Without the virtual term, here’s an example:

#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;    
}    

Output:

Eating bread...

Polymorphism at runtime in C++ Using two derived classes as an example:

Let’s look at another run-time polymorphism example in C++, this time with two derived classes:

// an example with the 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();     
}    

Output:

drawing...
drawing rectangle...
drawing circle...

Data Members and Runtime Polymorphism:

In C++, data members can be used to achieve runtime polymorphism. Let’s look at an example where we’re accessing a field via a reference variable that relates to a derived class instance.

#include <iostream>    
using namespace std;    
class Animal {                                          //  base class declaration.  
    public:    
    string color = "Black";      
};     
class Dog: public Animal                       // inheriting Animal class.  
{      
 public:    
    string color = "Grey";      
};    
int main(void) {    
     Animal d= Dog();      
    cout<<d.color;     
}    

Output:

Black
Default image
Lingaraj Senapati
Hey There! I am Lingaraj Senapati, the Co-founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.
Articles: 217

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply