Advertisement



< Prev
Next >



C++ Virtual Function





Polymorphism means having many forms i.e. to be able to access objects of different classes through a single type of pointer variable(using inheritance).

In one of our previous articles, we have discussed the concept of compile polymorphism in which, when a class A(base class) was inherited by class B(derived class) and the function of base class A was overridden in the derived class B, which overridden version of a function will be executed was decided at compile time, based on the type of pointer variable used to call the overridden function.

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




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.



    Advertisement




  3. Runtime polymorphism/Dynamic Binding.

  4. Runtime polymorphism is also known as dynamic function dispatch during runtime.

    Runtime polymorphism takes place in fulfilment of these conditions -
    • When a class extends another class and the function of base lass is overridden in subclass.
    • Object of subclass is accessed polymorphically(by the pointer variable of base class), to call the overridden function.
    • We have declared the function in the base class(which is going to be overridden in the derived class) with the keyword virtual.


    //Runtime polymorphism or dynamic function dispatch, during function overriding 
    
    #include<iostream>
    
    using namespace std;
    
    
    //Base class
    class A
    {
    public:
    int a;
    virtual void show();	//Declaration of base class function - virtual, to peform runtime polymorphism
    };
    
    void A :: show()
    {
    	cout<<"The show()function of Base class A \n";
    	cout<<"a is : "<< a << "\n";
    }
    
    
    //Derived class
    class B : public A	//class B is subclass/child class of class A
    {
    public:
    int b;
    void show();
    };
    
    void B :: show()
    {
    	cout<<"The show()function of derived  class B \n";
    	cout<<"a is : " << a << "\n";
    	cout<<"b is : " << b << "\n";
    }
    
    
    int main()
    {
    A ob1;
    ob1.a = 10;
    
    
    B ob2;	
    ob2.a = 20;
    ob2.b = 30;
    
    A *aptr;
    
    //Base class pointer pointing to the base class object.
    aptr = &ob1;
    
    //Base class pointer pointing to the base class function.
    aptr->show();
    
    //Base class pointer pointing to the derived class object.
    aptr = &ob2;
    
    //Base class pointer pointing to the base class function.
    aptr->show();	
    }

    Output -


    The show()method of Base class A
    a is : 10
    The show()method of derived  class B
    a is : 20
    b is : 30


    Program Analysis


    • A class A is extended by a class B and its function show()(declared with virtual keyword) is overridden in B.
    • The pointer variable of type A, is pointing to an object of class B and the overridden function show() is called, but which show() function is executed, is based on type of the object used to call the show() at the runtime and not on the pointer type used to call show() at compile time.



    Once again :


    In runtime polymorphism, which overridden version of a function will be executed is decided at runtime, based on the type of object(used to call the overridden function) and not at the type of pointer declared at compile time.



    Please share this article -




    < Prev
    Next >
    < Friend Funtion
    C-Style Characters Strings >



    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

C#, PYTHON and DJANGO


Advertisement