CPP

Virtual Base Class

  • If there are member functions with same name in base class and derived class, 
  • virtual base class gives programmer capability to call member function of different class by a same function call depending upon different context.
  • This feature in C++ programming is known as polymorphism which is one of the important feature of OOP.
  • If a base class and derived class has same function and if you write code to access that function using pointer of base class then, the function in the base class is executed even if, the object of derived class is referenced with that pointer variable.

Example:

#include <iostream>

class B
{
    public:
       void display()
         { cout<<"Content of base class.\n"; }
};

class D : public B
{
    public:
       void display()
         { cout<<"Content of derived class.\n"; }
};

void main()
{
    B *b;
    D d;
    b->display();

    b = &d;    /* Address of object d in pointer variable */
    b->display();
    getch();
}

Output:

Output_of_Program

other_Description_add_here




Subscribe us on Youtube

Share This Page on


Ask Question