Search Your Topic

config
Saturday, 17 June 2017 07:24

C++ Polymorphism

Written by
Rate this item
(0 votes)

The term "Polymorphism" is the combination of "poly" + "morphs" which means many forms. It is a greek word. In object-oriented programming, we use 3 main concepts: inheritance, encapsulation and polymorphism.

There are two types of polymorphism in C++:

  • Compile time polymorphism: It is achieved by function overloading and operator overloading which is also known as static binding or early binding.
  • Runtime polymorphism: It is achieved by method overriding which is also known as dynamic binding or late binding.

C++ Runtime Polymorphism Example

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

 
  1. #include <iostream>  
  2. using namespace std;  
  3. class Animal {  
  4.     public:  
  5. void eat(){    
  6. cout<<"Eating...";    
  7.     }      
  8. };   
  9. class Dog: public Animal    
  10. {    
  11.  public:  
  12.  void eat()    
  13.     {    
  14.        cout<<"Eating bread...";    
  15.     }    
  16. };  
  17. int main(void) {  
  18.    Dog d = Dog();    
  19.    d.eat();  
  20.    return 0;  
  21. }  

Output:

Eating bread...

C++ Runtime Polymorphism Example: By using two derived class

Let's see another example of runtime polymorphism in C++ where we are having two derived classes.

 
  1. #include <iostream>  
  2. using namespace std;  
  3. class Shape {  
  4.     public:  
  5. virtual void draw(){    
  6. cout<<"drawing..."<<endl;    
  7.     }      
  8. };   
  9. class Rectangle: public Shape    
  10. {    
  11.  public:  
  12.  void draw()    
  13.     {    
  14.        cout<<"drawing rectangle..."<<endl;    
  15.     }    
  16. };  
  17. class Circle: public Shape    
  18. {    
  19.  public:  
  20.  void draw()    
  21.     {    
  22.        cout<<"drawing circle..."<<endl;    
  23.     }    
  24. };  
  25. int main(void) {  
  26.     Shape *s;  
  27.     Shape sh;  
  28.         Rectangle rec;  
  29.         Circle cir;  
  30.         s=&sh;  
  31.      s->draw();   
  32.         s=&rec;  
  33.      s->draw();    
  34.     s=○  
  35.      s->draw();   
  36. }  

Output:

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

Runtime Polymorphism with Data Members

Runtime Polymorphism can be achieved by data members in C++. Let's see an example where we are accessing the field by reference variable which refers to the instance of derived class.

 
  1. #include <iostream>  
  2. using namespace std;  
  3. class Animal {  
  4.     public:  
  5.     string color = "Black";    
  6. };   
  7. class Dog: public Animal   
  8. {    
  9.  public:  
  10.     string color = "Grey";    
  11. };  
  12. int main(void) {  
  13.      Animal d= Dog();    
  14.     cout<<d.color;   
  15. }  

Output:

Black
Read 83 times
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

toshiyas.in

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.