What is a destructor in C++?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

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
What are 5 interesting facts about Microsoft?
What IDE is used at Meta?
What is the best language for algorithm interviews?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Image
Grokking Data Structures & Algorithms for Coding Interviews
Image
Grokking Advanced Coding Patterns for Interviews
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.