You are on page 1of 4

A virtual function is a member function that is declared within a base class and redefined by a

derived class. To create virtual function, precede the functions declaration in the base class with
the keyword virtual. When a class containing virtual function is inherited, the derived class
redefines the virtual function to suit its own needs.
Base class pointer can point to derived class object. In this case, using base class pointer if we
call some function which is in both classes, then base class function is invoked. But if we want to
invoke derived class function using base class pointer, it can be achieved by defining the
function as virtual in base class, this is how virtual functions support runtime polymorphism.
Consider following program code:
Class A
{
int a;
public:
A()
{
a = 1;
}
virtual void show()
{
cout <<a;
}
};
Class B: public A
{
int b;
public:

B()
{
b = 2;
}
void show()
{
cout <<b;
}
};
int main()
{
A *pA;
B oB;
pA = &oB;
pA->show();
return 0;
}

Output is 2 since pA points to object of B and show() is virtual in base class A.


Describe the virtual function and virtual function table.
A virtual function in C++ is
- A simple member function of a class which is declared with virtual keyword
- It usually performs different functionality in its derived classes.
- The resolving of the function call is done at run-time.
Virtual Table:
A virtual table is a mechanism to perform dynamic polymorphism i.e., run time binding. Virtual
table is used to resolve the function calls at runtime. Every class that uses virtual functions is
provided with its own virtual functions.
Every entry in the virtual table is a pointer that points to the derived function that is accessible by
that class. A hidden pointer is added by a compiler to the base class which in turn calls *_vptr
which is automatically set when an instance of the class is created and it points to the virtual
table for that class..
If the destructor in the base class is not made virtual, then an object that might have been
declared of type base class and instance of child class would simply call the base class destructor
without calling the derived class destructor.

In C++, whats a virtual destructor and when is it needed?

As you may know, in C++ a destructor is generally used to deallocate memory and do some other
cleanup for a class object and its class members whenever an object is destroyed. Destructors
are distinguished by the tilde, the ~ that appears in front of the destructor name. In order to
define a virtual destructor, all you have to do is simply add the keyword virtual before the tilde
symbol.
The need for virtual destructors in C++ is best illustrated by some examples. Lets start by going
through an example that does not use virtual destructors, and then we will go through an
example that does use virtual destructors. Once you see the difference, you will understand why
virtual destructors are needed. Take a look at the code below to start out:

Example without a Virtual Destructor:


class Base
{
public:
Base(){ cout<<"Constructing Base";}

// this is a destructor:
~Base(){ cout<<"Destroying Base";}
};
class Derive: public Base
{
public:
Derive(){ cout<<"Constructing Derive";}
~Derive(){ cout<<"Destroying Derive";}
};
void main()
{
Base *basePtr = new Derive();
}

delete basePtr;

The output after running the code above would be:


Constructing Base
Constructing Derive
Destroying Base

Based on the output above, we can see that the constructors get called in the appropriate order
when we create the Derive class object pointer in the main function.
But there is a major problem with the code above: the destructor for the "Derive" class does not
get called at all when we delete basePtr.
So, how can we fix this problem?
Well, what we can do is make the base class destructor virtual, and that will ensure that the
destructor for any class that derives from Base (in our case, its the "Derive" class) will be called.
Subscribe to our newsletter for more free interview questions.

Example with a Virtual Destructor:


So, the only thing we will need to change is the destructor in the Base class and heres what it
will look like note that we highlighted the part of the code where the virtual keyword has been
added in red:
class Base
{
public:
Base(){ cout<<"Constructing Base";}

};

// this is a virtual destructor:


virtual ~Base(){ cout<<"Destroying Base";}

Now, with that change, the output after running the code above will be:

Constructing Base
Constructing Derive
Destroying Derive
Destroying Base

Note that the derived class destructor will be called before the base class.
So, now youve seen why we need virtual destructors and also how they work.
One important design paradigm of class design is that if a class has one or more virtual functions,
then that class should also have a virtual destructor.

You might also like