An interface class contains pure virtual functions and virtual destructor. A pure virtual function has been described above. A pure virtual function is declared by initializing or assigning it a value 0. This is demonstrated in the following example:

void show () = 0;

A pure virtual function is included in the base class and it should be redefined in the derived class. It is necessary for the virtual function to be redefined in the derived or the child class.

In abstract class we cannot define objects of classes that contain the pure virtual function. That’s why this class is called abstract class. Abstract class is only used for defining the derived classes. Interfaces and abstract classes are similar but having a minor difference that is:

Interfaces have no implemented. Rather a class implements interfaces. But abstract class may contain implementation or data members. Interfaces can be multiple inherited, but abstract classes cannot be.


Virtual destructors

In a simple class program we use destructors to destroy the objects. Destructor has the same name as that of constructor but its name is followed by ( ~ ) e.g.

~circle () ;

The destructor is automatically called by the end of the program. Destructor always destroys the created objects in descending order.

Now we are introducing virtual destructor, these are mostly used in class inheritance (base class and derived class ). If we are using a virtual destructor, then it will destroy the objects of derived class first and then destroys the objects of base class.

And if the derived class destructor is not virtual, then the objects of base class will be deleted only and the objects of derived class will not be deleted.