Destructors
A destructor is a special method in a C++ class that is automatically called when an instance of that class is destroyed. Destructors are used to free resources that were allocated during the object’s lifetime, such as dynamic memory or file handles. Unlike constructors, destructors cannot take arguments and do not return a value.
Syntax
A destructor method is defined using the tilde (~
) character followed by the class name. It has no parameters and no return type.
class ClassName {
public:
~ClassName() {
// Destructor code
}
};
Examples
Basic Destructor
A basic destructor is defined to perform cleanup tasks, such as releasing dynamically allocated memory or closing file streams.
#include <iostream>class MyClass {public:// ConstructorMyClass() {std::cout << "The constructor was executed!\n";}// Destructor~MyClass() {std::cout << "The destructor was executed!\n";}};int main() {MyClass myObject; // Constructor is called here// Destructor will be called automatically when myObject goes out of scopereturn 0;}
The output of the above code will be:
The constructor was executed!The destructor was executed!
Destructor with Dynamic Memory Cleanup
Destructors are especially useful when a class allocates memory dynamically, ensuring proper cleanup to avoid memory leaks.
#include <iostream>class DynamicArray {private:int* data;int size;public:// ConstructorDynamicArray(int s) {size = s;data = new int[s]; // Allocate memory dynamicallystd::cout << "Array of size " << size << " created.\n";}// Destructor~DynamicArray() {delete[] data; // Free allocated memorystd::cout << "Array of size " << size << " destroyed.\n";}};int main() {DynamicArray arr(5); // Constructor is called// Destructor will be called automatically when arr goes out of scopereturn 0;}
The output of the above code will be:
Array of size 5 created.Array of size 5 destroyed.
Destructor Definition Outside the Class
Like constructors, destructors can also be declared in the class and defined outside the class using the scope resolution operator.
#include <iostream>class Resource {private:int* value;public:// ConstructorResource(int v);// Destructor~Resource();};// Define constructor outside the classResource::Resource(int v) {value = new int(v);std::cout << "Resource allocated with value " << *value << "\n";}// Define destructor outside the classResource::~Resource() {delete value;std::cout << "Resource deallocated.\n";}int main() {Resource res(10); // Constructor is called// Destructor will be called automatically when res goes out of scopereturn 0;}
The output of the above code will be:
Resource allocated with value 10Resource deallocated.
Codebyte Example
The following example demonstrates a simple class that dynamically allocates memory for an integer and ensures proper cleanup using a destructor.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C++
Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.Beginner Friendly11 hours