Constructor and Destructor in C++ (With Order of Execution)
Constructor runs automatically when an object is born, destructor when it dies. Learn syntax (~ for destructor), execution order proof and why destructors matter.
The hotel-room analogy
Syntax — the two identity marks
class Student {
public:
Student() { } // CONSTRUCTOR: same name as class, no return type
~Student() { } // DESTRUCTOR : same name with ~ (tilde), no return
// type, NO parameters, only ONE allowed
};
Watch them run automatically
#include <iostream>
using namespace std;
class Student {
public:
string name;
Student(string n) { // constructor with parameter
name = n;
cout << name << " admitted (constructor)\n";
}
~Student() { // destructor
cout << name << " left school (destructor)\n";
}
};
int main() {
cout << "-- main starts --\n";
Student s1("Aman"); // constructor runs HERE
Student s2("Priya");
cout << "-- main ends --\n";
return 0; // destructors run HERE (scope ends)
}
Why do destructors even matter?
For a simple Student holding a name, the destructor has nothing important to do. But when an object holds a resource — heap memory, an open file, a database connection — someone must release it. The destructor is the guaranteed place:
class ReportFile {
FILE *fp;
public:
ReportFile() { fp = fopen("report.txt", "w"); } // acquire in constructor
~ReportFile() { fclose(fp); } // release in destructor
}; // whoever uses ReportFile can NEVER forget to close the file -
// closing happens automatically when the object dies
This pattern — acquire in constructor, release in destructor — is called RAII, and it is the single most important idea behind professional C++. Mention RAII in an interview and you immediately stand out.
Constructor vs Destructor table
| Point | Constructor | Destructor |
|---|---|---|
| Name | Class name | ~ClassName |
| When it runs | Object created | Object destroyed / scope ends |
| Parameters | Allowed (can overload many) | Never — exactly one destructor |
| Return type | None | None |
| Purpose | Initialize / acquire resources | Cleanup / release resources |
| Call order (multiple objects) | Creation order | Reverse of creation (LIFO) |
Hotel-room analogy
Syntax — दो पहचान के निशान
class Student {
public:
Student() { } // CONSTRUCTOR: class का same नाम, no return type
~Student() { } // DESTRUCTOR : same नाम ~ (tilde) के साथ, no return
// type, कोई parameters NAHI, सिर्फ EK allowed
};
इन्हें automatically चलते देखिए
#include <iostream>
using namespace std;
class Student {
public:
string name;
Student(string n) { // parameter वाला constructor
name = n;
cout << name << " admitted (constructor)\n";
}
~Student() { // destructor
cout << name << " left school (destructor)\n";
}
};
int main() {
cout << "-- main starts --\n";
Student s1("Aman"); // constructor YAHAN चला
Student s2("Priya");
cout << "-- main ends --\n";
return 0; // destructors YAHAN चले (scope खत्म)
}
Destructor आखिर ज़रूरी क्यों है?
Name रखने वाले simple Student के लिए destructor के पास कोई खास काम नहीं. लेकिन जब object कोई resource रखता है — heap memory, खुली file, database connection — तो किसी को उसे release करना ही होगा. Destructor वह guaranteed जगह है:
class ReportFile {
FILE *fp;
public:
ReportFile() { fp = fopen("report.txt", "w"); } // constructor में लो
~ReportFile() { fclose(fp); } // destructor में छोड़ो
}; // ReportFile use करने वाला file close करना कभी भूल ही नहीं सकता -
// object खत्म होते ही close automatically होता है
यह pattern — constructor में acquire, destructor में release — RAII कहलाता है, और professional C++ के पीछे यही सबसे बड़ा idea है. Interview में RAII बोल दिया तो आप तुरंत अलग दिखेंगे.
Constructor vs Destructor table
| Point | Constructor | Destructor |
|---|---|---|
| Name | Class का नाम | ~ClassName |
| कब चलता है | Object बनते ही | Object destroy / scope खत्म होते ही |
| Parameters | Allowed (कई overload हो सकते हैं) | कभी नहीं — exactly एक destructor |
| Return type | नहीं | नहीं |
| Purpose | Initialize / resources लेना | Cleanup / resources छोड़ना |
| Order (कई objects) | Creation order | Creation का उल्टा (LIFO) |
Frequently Asked Questions
What is a destructor in C++?
A destructor is a special member function named ~ClassName that runs automatically when an object is destroyed, used to release resources like memory, files or connections.
In what order are destructors called for multiple objects?
In reverse order of creation (LIFO) — the last object created is the first one destroyed when the scope ends.
Can a destructor be overloaded or take parameters?
No. A class has exactly one destructor with no parameters and no return type — unlike constructors which can be overloaded freely.