Javatpoint Logo
Javatpoint Logo

Virtual Destructor in C++

A destructor in C++ is a member function of a class used to free the space occupied by or delete an object of the class that goes out of scope. A destructor has the same name as the name of the constructor function in a class, but the destructor uses a tilde (~) sign before its function name.

Virtual Destructor

A virtual destructor is used to free up the memory space allocated by the derived class object or instance while deleting instances of the derived class using a base class pointer object. A base or parent class destructor use the virtual keyword that ensures both base class and the derived class destructor will be called at run time, but it called the derived class first and then base class to release the space occupied by both destructors.

Why we use virtual destructor in C++?

When an object in the class goes out of scope or the execution of the main() function is about to end, a destructor is automatically called into the program to free up the space occupied by the class' destructor function. When a pointer object of the base class is deleted that points to the derived class, only the parent class destructor is called due to the early bind by the compiler. In this way, it skips calling the derived class' destructor, which leads to memory leaks issue in the program. And when we use virtual keyword preceded by the destructor tilde (~) sign inside the base class, it guarantees that first the derived class' destructor is called. Then the base class' destructor is called to release the space occupied by both destructors in the inheritance class.

Write a program to display a class destructor's undefined behaviour without using a virtual destructor in C ++.


Virtual Destructor in C++

As we can see in the above output, when the compiler compiles the code, it calls a pointer object in the main function that refers to the base class. So, it executes the base class' constructor() function and then moves to the derived class' constructor() function. After that, it deletes the pointer object occupied by the base class' destructor and the derived class' destructor. The base class pointer only removes the base class's destructor without calling the derived class' destructor in the program. Hence, it leaks the memory in the program.

Note: If the base class destructor does not use a virtual keyword, only the base class destructor will be called or deleted its occupied space because the pointer object is pointing to the base class. So it does not call the derived class destructor to free the memory used by the derived class, which leads to memory leak for the derived class.

Write a program to display a class destructor's behavior using a virtual destructor in C ++.


Virtual Destructor in C++

In the above program, we have used a virtual destructor inside the base class that calls the derived class' destructor before calling the base class' destructor and release the spaces or resolve the memory leak issue in the program.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA