C++ Polymorphism

C++ Polymorphism: Polymorphism is derived from the Greek words. Poly means “many” and “morphism” means “forms” i.e many forms. Polymorphism means more than one form. The operation depends upon the types of data used in the operation. And polymorphism is extensively used in implementing inheritance.



C++ Polymorphism with Examples

Real-Life Example Of Polymorphism

Let us 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.

Example:

#include<iostream.h>
#include<conio.h>
class Base
{
 public:
 void show()
 {
  cout<<"Base class";
 }
};
class Derived:public Base
{
 public:
 void show()
 {
  cout<<"Derived Class";
 }
}

int mian()
{
 Base b;//Base class object
 Derived d;//Derived class object
 b.show();//Early Binding Ocuurs
 d.show();   
getch();
}

Output:

Base class
Derived Class

Different Types of Polymorphism

C++ polymorphism

Compile-time polymorphism

The overloaded functions are invoked by matching the type and number of arguments. It is achieved by function overloading and operator overloading which is also known as static binding or early binding.

Example:

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

Run time polymorphism

Run time polymorphism is achieved when the object’s method is invoked at the run time instead of compile time. And also it is achieved by method overriding which is also known as dynamic binding or late binding.

Example:

#include<iostream>    
using namespace std;    
class Animal {    
    public:    
void eat(){      
cout<<"Eating...";      
    }        
};     
class Dog: public Animal      
{      
 public:    
 void eat()      
 {   
   cout<<"Eating snacks...";      
  }      
};    
int main(void) {    
   Dog d = Dog();      
   d.eat();    
   return 0;    
}

Output:

Eating snacks…

Example: Run time polymorphism by using two derived class

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

Example: Run time polymorphism with data members.

#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