C++ Polymorphism

    Polymorphism is one of the most important properties of Object-Oriented Programming, the word polymorphism is made by the combination of two Greek words Poly + morph, poly means “many” and morph means forms and its name resemblance its definition. Polymorphism concept state that there could be more than one function that shares the same name but have different functionalities and the concepts like function, & operator overloading and virtual functions are examples of Polymorphism. Real-Life Example of Polymorphism: A woman name Sofia is in a store and she could be mother, wife, and an employee at the same time, but what would be her real identity at a specific position defined by her working, for example, if she is working in the store she will be considered as the employee but if she at her home with her child then she will be treated like a mother, the same concept polymorphism follows, and here the compiler decides the identity or functionality of the object.

    Types of Polymorphism

    In C++ we have two types of Polymorphism.

    • Compile-time Polymorphism
    • Runtime Polymorphism.

    1. Compile-time Polymorphism

    It is also known as static polymorphism and Function and Operator Overloading are examples of Compile time Polymorphism. In this, the compiler selects the appropriate function at compile-time and all the memory allocation to the member function also occurs at the compile time.

    2. Runtime Polymorphism

    It is also known as dynamic binding and here allocation of memory to the function occurs at runtime. Here the object invokes the member function at runtime instead of compile-time, here the object binds the member function at the runtime that’s why it is also known as Late binding. To achieve the runtime polymorphism, we use the Function Overriding and Virtual functions.

    2.1. Function Overriding:

    If the Derived class has the same member function as the base class but with a different definition then it is known as Function overriding.

    2.2 Virtual Functions:

    Virtual Functions are those functions which have a keyword virtual before their return data type. A virtual keyword is used to tell the compiler that allocates this function memory at runtime instead of compile-time and to create a dynamic binding we use the virtual keyword.

    Compile-time Binding Example Runtime Binding Example
    #include<iostream>
    using namespace std;
     class Base                                   
      { 
       public: 
          void show() 
           {  
                 cout<< "It's Base Class show() function "; 
            } 
      }; 
    class Derived : public Base                        
    { 
        public: 
        void show()  //method Overriding
      { 
            cout<<"It's Derived Class show() function"; 
      } 
    };
    
    int main()
    {             
      Base *ptr;
      Derived D;
      ptr =& D;
      ptr->show();   // this will invoke the derived class show() member function
    }
    #include<iostream>
    using namespace std;
     class Base                                   
      {    
      public: 
        virtual void show() 
           {  
            cout<< "It's Base Class show() function "; 
            } 
      }; 
    class Derived : public Base                        
    { 
     public: 
       virtual void show()  //Method overriding
        { 
           cout<<"It's Derived Class show() function"; 
        } 
    };
    int main()
    {             
      Base *ptr;
      Derived D;
      ptr =& D;
      ptr->show();   // this will invoke the derived class show() member function
    
    }
    Output:
    It's Base Class show() function
    Output:
    It's Derived Class show() function
    Behind this Code: In this example when we use the ptr->show(); statement then it invoked the Base class show() member function because of the early or compile time binding concept here the ptr=&D; statement has no significance on the pointer ptr because the address of the object D would be allocated at the runtime so here ptr always been an associate of the base class that’s why it invoked the Base class show() member function. Behind the code: Here when we use the ptr->show(); statement then it invoked the Derived class member function because of the late binding. Here we have used the virtual keyword before the show() member function which tells the compiler not to allocate the memory to these function till the run time. So, here all the function get memory allocation at runtime instead of compile time that why when the ptr =& D; run the ptr hold the address of D object, then only it was capable of invoking the Derived class show() member function.

    Difference between Runtime and Compile time polymorphism:

    Compile Time Polymorphism Runtime Polymorphism
    Functions allocated memory at compile time Functions allocated memory at Runtime
    It is also known as Early and static binding. It is also known as Late and dynamic binding.
    It can be achieved by function overloading, function overriding and Operator Overloading It can be achieved by a virtual Function.
    It is fast It is slow
    Less Flexible More Flexible

    Polymorphism Quick Summary:

    • Polymorphism state that an object can have different definitions for different situations.
    • There are two types of Polymorphisms, compile, and runtime.
    • In Compile type polymorphism the compiler binds the object with function at compile time
    • In Runtime polymorphism, the object binds with functions at runtime.

    People are also reading: