Wednesday, 19 November 2014

Virtual destructor in C++ with examples

Virtual destructor in C++:

First we need to know about the destructor of the class: the destructor is the member function that is used to destroy the memory of the object, when it is goes out of scope. That means the heap memory allocated by the member variables of the class can gets cleanup in the destructor of the class.

The order of execution of the destructors are in the below fashion.

First the derived class memory gets cleaned and then the base class destructor gets called and deletes the base class objects memory.
The order of execution of destructor in an inherited class during a cleanup is like this.



The difference between a destructor and other member functions is that, if a regular member function has a body at the derived class, only the version at Derived class gets executed. Whereas in case of destructors, both derived as well as base class versions get executed.

See the below scenario to know the need of virtual destructors:
/*This is my base class */
class MyBaseClass
{
       public:
          MyBaseClass(){ cout << "Constructor of MyBaseClass" << endl;}
          ~MyBaseClass(){ cout << "Destructor of MyBaseClass" << endl;}
};
/*This is my derived class which is derived from the base*/
class MyDerivedClass: public MyBaseClass
{
     //Doing a lot of jobs by extending the functionality
       public:
           MyDerivedClass(){ cout << "Constructor of MyDerivedClass" << endl; }
           ~MyDerivedClass(){ cout << "Destructor of MyDerivedClass" << endl; }
};
int _tmain (int argc, _TCHAR* argv[])
{
    MyBaseClass *Var = new MyDerivedClass();
    delete Var;
    getchar ();
    return 0;
}

After executing the above code we will get the output as
Constructor of MyBaseClass
Constructor of MyDerivedClass
Destructor of MyBaseClass

Here the base class destructor is called, till now it is ok. What about the derived class destructor, it will not call. So resolve the problem the virtual destructors comes into the picture.to call all the desructors in the order (derived to base) we need to make the base calss destructor as virtual in our code

I am rewriting the above class and trying to compile it, let us watch the results
using namespace std;
/*This is my base class */
class MyBaseClass
{
       public:
          MyBaseClass(){ cout << "Constructor of MyBaseClass" << endl;}
          virtual ~MyBaseClass(){ cout << "Destructor of MyBaseClass" << endl;}
};
/*This is my derived class which is derived from the base*/
class MyDerivedClass: public MyBaseClass
{
     //Doing a lot of jobs by extending the functionality
       public:
           MyDerivedClass(){ cout << "Constructor of MyDerivedClass" << endl; }
           ~MyDerivedClass(){ cout << "Destructor of MyDerivedClass" << endl; }
};
int _tmain (int argc, _TCHAR* argv[])
{
    MyBaseClass *Var = new MyDerivedClass();
    delete Var;
    getchar ();
    return 0;
}
The output of the above program is
Constructor of MyBaseClass
Constructor of MyDerivedClass
Destructor of MyDerivedClass
Destructor of MyBaseClass


Awesome, see the output, the destructor of the derived class gets called when I call the
    delete Var;


1 comment:

Engr. Ahsan Arif said...

Its really nice initiative from your corner. You are requested to visit for more coding experience at http://www.cprogramming-bd.com