Advertisement



< Prev
Next >



C++ Polymorphism





Polymorphism means having many forms. In C++, polymorphism allows us to access an object in multiple ways, such as - An object accessed in such multiple ways is said to be accessed polymorphically or polymorphic in nature.




Accessing an object polymorphically


  1. A pointer variable can refer to any object of its own class and also any object of its subclass(in inheritance).


  2. //Base class
    class A
    {
    }
    
    
    //Derived class class B is subclass of class A
    class B extends A	
    {
    }
    
    
    int main()
    {
    A ob1;
    B ob2;
    
    //Base class pointer
    A *aptr;
    
    //Base class pointer pointing to the base class object.
    aptr = &ob1;
    
    //Base class pointer pointing to the derived class object.
    aptr = &ob2;
    }


    Program Analysis


      As you may see in the code above, an object of B class is accessed polymorphically by -
    • Pointer variable of its own class B and,
    • Pointer variable of its superclass A.




      Two types of polymorphism


    • Compile-time polymorphism/Stating Binding.
    • Runtime polymorphism/Dynamic Binding.




    • Compile-time polymorphism/Static Binding.

    • Compile-time polymorphism takes place in the fulfilment of all of these conditions -
      • When a class extends another class by inheritance.
      • Function of a class is overridden in its subclass.
      • Object of a subclass is accessed polymorphically(using pointer of its superclass), to call on the overridden function.


      #include<iostream>
      
      using namespace std;
      
      
      //Superclass	
      class A				
      {
      public:
      void message();
      };
      
      void A :: message()
      {
      cout<<"A";
      }
      
      
      
      //Subclass or derived class of class B
      class B : public A	
      {
      public:
      void message();     //overridden message() function
      };
      
      void B :: message()
      {
      cout<<"B";
      }
      
      
      
      int main()
      {
      A* aptr;
      B ob; 	
      
      //Pointer variable aptr is declared of type A but it is pointing to object of its subclass, B
      aptr = &ob;
      
      aptr->message();
      }
      


      Output -


      A


      Program Analysis


      • Class A is extended by a class B and its function message() is overridden in class B.
      • The pointer variable aptr is declared of type A but it is pointing to object of its subclass, B.
      • Using this pointer variable, the overridden function message() of B class is called.
      • Which message() function is executed, is based on the type of pointer variable used to call the message() function, at the compile time.
      • Hence, as aptr is a pointer variable of type A, so the function message() of class A is called.



      Advertisement




      Note:


      In compile-time polymorphism, which overridden version of a function will be executed is decided at compile time, based on the type of pointer variable used to call the overridden function. Hence, the compile time polymorphism is also known as stating binding.


      We are going to discuss the runtime polymorphism using the concept of virtual function and which overridden version of a function will be executed is decided at runtime, will be based on the type of object and not at the type of pointer variable declared at compile time.



      Please share this article -






      Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement