Home >>C++ Tutorial >C++ Polymorphism

C++ Polymorphism

C++ Polymorphism

If we break the word Polymorphism in C++ then it would turn out to be the linked result of two words that are “Poly” and “morphs” that is a Greek word and basically translates to ‘many forms’. Polymorphism is among the three most important concepts in the object oriented programming that are inheritance, encapsulation, and polymorphism.

Real Life Example Of Polymorphism

Here is a real life example of polymorphism: A man generally behaves like teacher in a classroom, father or son in home and as a normal customer in a market. Hence, you can notice that here a single individual id behaving differently as per the situation's needs.

Types of the polymorphism in C++

There are generally two types of the polymorphism in C++:

  • Compile time Polymorphism:
  • Run time Polymorphism:

Compile time Polymorphism:

The overloaded functions are called when the numbers and types of argument/parameter are matched Just by matching the type and number of arguments, overloaded functions can be invoked. As this information is only available at the time of compiling hence, compiler is the one that selects the appropriate functions just at the time of compiling.This situation is generally achieved by function overloading and operator overloading that is also called as static binding or with another name that is early binding.

Here is an example that will make you understand the topic very easily:

#include <iostream>    
using namespace std;    
class Calculator {    
    public:    
static int sum(int x,int y)
{      
        return x + y;      
 }      
static int sum(int x, int y, int z)      
    {      
        return x + y + z;      
    }      
};     
int main(void) 
{    
    Calculator obj;                    
    cout<<"Sum of two number ="<<obj.sum(10, 20)<<endl;      
    cout<<"Sum of three number ="<<obj.sum(12, 20, 23);     
   return 0;    
}     
Output :
Sum of two number =30
Sum of three number =55

Run Time Polymorphism

Whenever the class method is invoked at the run time instead of compiling time then the run time polymorphism is achieved. It is generally achieved by the method overriding that is also called as the dynamic binding or late binding.

Here is the example

#include <iostream>    
using namespace std;    
 class base  //  Here we  declared parent/Base Class.  
  {  
       int x;  
       public:  
       void show()  
       {   
             cout<< "THis is Base Class ";  
        }  
  };  
class child : public base //  Here we declared child class.  
{  
    int y;  
    public:  
   void show()  
  {  
        cout<<"THis is child Class";  
  }  
};  
int main(void) {    
   child obj;      
   obj.show();    
   return 0;    
}    
Output :THis is child Class

Differences b/w compile time and run time polymorphism

Compile time polymorphism Run time polymorphism
At the compile time this function is generally invoked. At the run time this function is generally invoked.
It is known by various names in the C++ programming like overloading, early binding and static binding. It is known by various names in the C++ programming like overriding, Dynamic binding and late binding.

No Sidebar ads