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

C++ Destructors

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 <iostream>

class Buffer {
	int *data;
public:
	Buffer() { data = new int[10]; }
	~Buffer() { delete[] data; std::cout << "Memory freed" << std::endl; }
};

int main() {
	Buffer b;
	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;
}

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.