← Back to C++ Course | Chapter 8: OOP Core | Lesson 7 of 14

C++ Destructors

A destructor is the clean-up step that runs when an object goes away, like turning off the lights when leaving a room. It releases whatever the object was using.
Syntax
cpp
class ClassName {
public:
  ~ClassName() {
    // cleanup code, runs when the object is destroyed
  }
};

What is a Destructor?

A destructor is a special member function that runs automatically when an object goes out of scope or is explicitly deleted.

It shares the class's name prefixed with a tilde (~), takes no arguments, and has no return type, and its job is to clean up whatever the object was holding onto.

Example: What is a Destructor?

cpp
#include <iostream>

class Box {
public:
	~Box() { std::cout << "Box destroyed" << std::endl; }
};

int main() {
	Box b;
	return 0;
} // destructor runs here

Destructors and Resource Cleanup

Destructors are essential for releasing resources an object owns — most commonly memory allocated with new.

If you don't free that memory inside the destructor, it leaks every time an object of that class is destroyed, since nothing else will ever reclaim it.

Example: Destructors and Resource Cleanup

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Buffer
class Buffer {
	int *data;
// Members below can be accessed from outside the class
public:
	Buffer() { data = new int[10]; }
	~Buffer() { delete[] data; std::cout << "Memory freed" << std::endl; }
};

// Program execution starts in main()
int main() {
	Buffer b;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Implicit vs Explicit Destructors

If you don't write a destructor yourself, the compiler provides an implicit one automatically.

That default is fine for simple classes, but the moment your class manages a raw resource like heap memory or a file handle, you need to write an explicit destructor to release it correctly.

Example: Implicit vs Explicit Destructors

cpp
#include <iostream>

class Simple {
	int value = 5; // no destructor written: compiler generates an implicit one
};

int main() {
	Simple s;
	std::cout << "No custom cleanup needed" << std::endl;
	return 0;
}

Destructor Order of Execution

When multiple objects are destroyed together, such as local variables going out of scope at the end of a function, they're destroyed in the exact reverse order of their creation — the last object created is always the first one destroyed, mirroring how a stack unwinds.

Example: Destructor Order of Execution

cpp
#include <iostream>

class Item {
	int id;
public:
	Item(int i) : id(i) {}
	~Item() { std::cout << "Destroying item " << id << std::endl; }
};

int main() {
	Item a(1);
	Item b(2);
	return 0; // b destroyed first, then a (reverse of creation order)
}

Destructors with Pointers

Objects created on the heap with new are not destroyed automatically when they go out of scope, because a raw pointer going out of scope only destroys the pointer, not what it points to.

You must call delete explicitly to trigger the destructor and free that memory.

Example: Destructors with Pointers

cpp
#include <iostream>

class Box {
public:
	~Box() { std::cout << "Box destroyed" << std::endl; }
};

int main() {
	Box *ptr = new Box();
	delete ptr; // must delete manually -- a raw pointer going out of scope does NOT call the destructor
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Calling a destructor manually with obj.~Class(), which leads to it running twice when the object goes out of scope.
  2. Forgetting to delete memory allocated with new in the constructor, causing a leak.
  3. Deleting a derived object through a base pointer when the base destructor is not virtual.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.