What is a destructor in C++?

In C++, a destructor is a special member function of a class that is automatically invoked when an object of that class is destroyed. The primary purpose of a destructor is to release any resources that the object may have acquired during its lifetime, such as memory or file handles. Destructors help in cleaning up resources to prevent memory leaks and ensure the proper functioning of dynamic memory management.

Key Features of Destructors:

  1. Same Name as Class: A destructor has the same name as the class but is preceded by a tilde (~).
  2. No Return Type: Destructors do not have a return type, not even void.
  3. No Parameters: Destructors cannot take any parameters, and they cannot be overloaded.
  4. Automatic Invocation: A destructor is called automatically when an object goes out of scope or is explicitly deleted.
  5. One Destructor per Class: There can only be one destructor per class.

Syntax of Destructor:

class MyClass { public: ~MyClass() { // Cleanup code, such as releasing memory } };

Example:

#include <iostream> using namespace std; class Car { public: Car() { cout << "Car created" << endl; } ~Car() { cout << "Car destroyed" << endl; } }; int main() { Car myCar; // Constructor is called here // Destructor will be called automatically when the object goes out of scope return 0; }

Output:

Car created
Car destroyed

Use Cases of Destructors:

  • Memory Deallocation: If dynamic memory is allocated in the constructor using new, the destructor is used to free the memory with delete.
  • Closing Files or Database Connections: Destructors can be used to close files, network connections, or databases that were opened in the lifetime of the object.

Destructor and Memory Management:

In programs that use dynamic memory allocation (e.g., using new to allocate memory on the heap), destructors play a critical role in ensuring that this memory is released when the object is no longer needed.

Example of memory management with destructors:

class MyClass { private: int* ptr; public: MyClass(int val) { ptr = new int(val); // Dynamically allocate memory } ~MyClass() { delete ptr; // Free dynamically allocated memory } };

Summary:

  • Destructors are essential for resource management in C++, ensuring that dynamically allocated memory or other system resources are released when an object is no longer in use.
  • They follow the RAII (Resource Acquisition Is Initialization) principle, where resources are acquired in the constructor and released in the destructor.

Sources:

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
How to prepare for a PM role?
What is the difference between NeetCode and Leetcode?
How to answer tell me about yourself?
Which is the most important topic in DSA?
What is the golden rule of code review?
Is coding interview worth it?
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
Discounted price for Your Region

$72

Grokking Data Structures & Algorithms for Coding Interviews course cover
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
Discounted price for Your Region

$78

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.