< Prev
Next >



Virtual Function in C++





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 the last article, we 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






  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 -
    //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





    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 >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
Python

Ad2