A destructor is a special function that cleans up when an object is no longer needed. It helps free resources like memory or files automatically.
Destructor role in C++
class ClassName { public: ~ClassName() { // cleanup code here } };
The destructor has the same name as the class but starts with a tilde (~).
It has no return type and no parameters.
class MyClass { public: ~MyClass() { // called automatically when object is destroyed } };
#include <fstream> class FileHandler { public: ~FileHandler() { file.close(); // close file when object is destroyed } private: std::fstream file; };
class Buffer { public: Buffer() { data = new int[10]; } ~Buffer() { delete[] data; } private: int* data; };
This program shows when the destructor runs. The object d is created inside a block and destroyed when the block ends.
#include <iostream> class Demo { public: Demo() { std::cout << "Object created\n"; } ~Demo() { std::cout << "Destructor called: Object destroyed\n"; } }; int main() { { Demo d; std::cout << "Inside block\n"; } // d goes out of scope here std::cout << "Outside block\n"; return 0; }
Destructor runs automatically when an object goes out of scope or is deleted.
You cannot call a destructor directly; it is called by the system.
If you don't define a destructor, the compiler creates a default one that does nothing.
Destructor cleans up resources when an object is destroyed.
It has the same name as the class with a ~ prefix and no parameters.
Destructor runs automatically when an object goes out of scope or is deleted.
Practice
Solution
Step 1: Understand the purpose of a destructor
A destructor is a special function that runs when an object is destroyed to free resources like memory or files.Step 2: Compare with other options
Creating objects, initializing variables, and overloading operators are not roles of destructors.Final Answer:
To clean up resources when an object is destroyed -> Option AQuick Check:
Destructor role = cleanup [OK]
- Confusing destructor with constructor
- Thinking destructor creates objects
- Believing destructor initializes variables
Car?Solution
Step 1: Recall destructor syntax
A destructor has the same name as the class with a tilde (~) prefix and no return type.Step 2: Check each option
~Car() {}matches the correct syntax.void ~Car() {}wrongly includes 'void'.Car~() {}has wrong order.void Car() {}lacks tilde and looks like a constructor.Final Answer:
~Car() {} -> Option CQuick Check:
Destructor syntax = ~ClassName() [OK]
- Adding return type to destructor
- Placing tilde after class name
- Confusing destructor with constructor syntax
class Box {
public:
Box() { std::cout << "Created\n"; }
~Box() { std::cout << "Destroyed\n"; }
};
int main() {
Box b;
return 0;
}Solution
Step 1: Trace object creation
Whenbis created, constructor prints "Created".Step 2: Trace object destruction
At end ofmain(),bis destroyed, destructor prints "Destroyed".Final Answer:
Created\nDestroyed\n -> Option BQuick Check:
Constructor then destructor output [OK]
- Assuming destructor runs before constructor
- Expecting no output
- Confusing order of prints
class Sample {
public:
~Sample(int x) { }
};Solution
Step 1: Check destructor parameters
Destructors cannot have parameters; they must have empty parentheses.Step 2: Verify other rules
Destructor has no return type, must have ~ prefix, and cannot be static.Final Answer:
Destructor cannot have parameters -> Option AQuick Check:
Destructor params = none [OK]
- Adding parameters to destructor
- Adding return type
- Removing ~ from destructor name
FileHandler that opens a file in its constructor. Which is the best way to ensure the file is closed properly when the object is no longer needed?Solution
Step 1: Understand resource management
Files opened by an object should be closed when the object is destroyed to avoid resource leaks.Step 2: Use destructor for cleanup
Destructor runs automatically when object ends, so closing the file there ensures proper cleanup.Final Answer:
Write a destructor that closes the file -> Option DQuick Check:
Destructor cleans resources automatically [OK]
- Forgetting to close files causing leaks
- Relying on manual close calls
- Assuming OS always cleans up immediately
Ready to prove it?
Pass the test and this pattern moves unknown
