Introduction to Destructor in C++
As we all know, C++ is an Object-Oriented programming language and is widely used in IT industries for the development of software, drivers, embedded firmware and client-server applications. It is a middle-level language encapsulating features of both high level and low-level language. It has a concept of classes and objects. In C++, the constructors play an important role in creating an object and initializing the instance variables, but what happens to these objects and resources after they are no longer in use or the program ends? This responsibility is taken care of by Destructors in C++. Destructors are used to destroy the objects created by the Constructors when they are not needed anymore to release the memory. They are special member functions and called automatically by C++. Compiler to free up the memory when there is no user-defined destructor in the program. Like Constructors, destructors also have the same name as class preceded by a tilde (~) sign. But destructor does not accept any arguments/parameters and does not return anything, i.e. they do not have any return type.
Destructors can never be overloaded like constructors in a class. Below given are some of the situations where the destructor is called automatically by the compiler:
- When a delete operator is called.
- When the program execution gets finished.
- When the block/scope of local variables ends.
A programmer can define a destructor known as a user-designed constructor. A destructor can be declared as Virtual or pure virtual but cannot be declared const, volatile, const volatile or static.
How does Destructor work in C++?
For working of destructor the below-mentioned points need to be kept in mind:
- Destructors of class objects are called first before calling the destructor of members and bases. Destructors of the non-virtual base class are called before the destructors of the virtual base class.
- Before the base classes, the destructor of non-static members is called. Destructors of both virtual and non-virtual base classes are called in the reverse order of their declaration.
- Implicit destructors are called automatically by the C++ compiler when an object goes out of scope, or the program execution terminates for external and static objects in a program. The destructors are called to destroy the objects created by a new keyword.
- In the case of both implicit and user-defined destructors, the compiler first executes the destructor body and then calls destructors of all non-static non-variant class and then calls the destructors of non-virtual and virtual base classes in the reverse order of their construction.
Syntax of Destructor
Destructors in C++ are preceded by the tilde(~) sign. Below mentioned is the basic syntax of destructor:
Syntax:
class class_name()
{
…
…
public:
class_name(); // Constructor
~class_name(); // destructor
}
}
In the syntax, ‘class_name’ is the class name, and in the main method, both the constructor and destructor with the same name of the class are defined. Destructor neither has any parameters nor return type.
Examples of Destructor in C++
Below mention is the example of Destructor in C++:
Example #1: With User-Defined Destructor
Code:
#include <iostream>
using namespace std;
class Example1{ //class
public:
Example1(){ // constructor cout << "Hello I am inside a constructor" << endl;
}
~Example1(){ //destructor
cout << "Hello I am inside a destructor" << endl;
}
void display()
{
cout << "Hello this is a display method" << endl
}
};
int main()
{
Example1 ex; //object created
ex.display(); // display method called return 0;
}
Output:
In the above example, ‘Example1’ is the name of the class and ‘ex’ is the object created of that class. First, the constructor is called, so the text written in the constructor is printed, then the display method is called ana, at last, the destructor is called after the whole execution of the program is done.
Example #2: With Virtual Destructor
Code:
#include<iostream>
using namespace std;
class b1 { //base class
public:
b1() // constructor of base class
{
cout << "Hello this is base constructor" << endl;
}
~virtual b1() // destructor of base class
{
cout << "Hello this is base destructor" << endl;
}
};
class b2: public b1{ // derived class of base class (b1) public:
b2() //constructor of derived cass
{
cout << "Hello this is derived constructor" << endl;
}
~ b2() // destructor of derived class
{
cout << "Hello this is derived destructor" << endl;
}
};
int main(void) //main method
{
b2 *der = new b2();
b1 *bas = der;
delete der;
getchar();
return 0;
}
Output:
It is a good practice to make the base class’s destructor as virtual as this ensures that the object of the derived class is destroyed properly. Whenever a virtual class is used, a virtual destructor should be added immediately to prevent any future unexpected results.
Advantages of Destructor in C++
- It gives a final chance to clean up the resources that are not in use to release the memory occupied by unused objects like deleting dynamic objects, closing of the system handles, used files.
- Because of a lot of resources occupying space and not getting used in the computer, the destructor always comes with a good image to reduce the chances of memory leak by destroying those unused things.
- Though C++ does have the mechanism of Garbage Collection but calling the destructor automatically, whether the programmer calls it or not, to free up space prevents the user from many worst situations in the future.
Points to Summaries about Destructor
- Destructors are used to destroy the unused resources of a class.
- Destructors have the same name as the class name preceding with (~) sign.
- Unlike Constructors, there can be no parameter of the destructor.
- There is no return type of destructor.
- If the user does not define a destructor by itself in a program, the compiler automatically constructs one destructor for it.
- There cannot be more than one destructor in a single class.
Conclusion
The above description of destructors clearly defines the use and implementation of destructor in the C++ program. Though the concept of the destructor is not complex yet it is very important to understand before implementation in the program as improper use of the destructor can lead to some unexpected results.
Recommended Article
This is a guide to Destructor in C++. Here we discuss Introduction to Destructor in C++ and Working of Destructor along with Advantages. You can also go through our other suggested articles to learn more–
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses