What are virtual constructors and virtual destructors?
* There are no virtual constructors in C++. Virtual destructors are required to avoid memory leaks while deleting the base class pointer when derived class object is created using base class pointers. * The order of execution of destructor in an inherited class during a clean up is like this. 1. Derived class destructor 2. Base class destructor #include “iostream.h” class Base { public: Base(){ cout Try executing this code; you’ll see the difference. To our observation, the constructors are getting called in the proper order. But to the dread of a programmer of a large project, the destructor of the derived class was not called at all. This is where the virtual mechanism comes into our rescue. By making the Base class Destructor virtual, both the destructors will be called in order. The following is the corrected sample.