C++ में Constructor और Destructor (Execution Order के साथ)
Constructor object के जन्म पर automatically चलता है, destructor उसके खत्म होने पर. Syntax (~ destructor के लिए), execution order proof और destructor क्यों ज़रूरी है.
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
C++ में destructor क्या है?
Destructor ~ClassName नाम का special member function है जो object destroy होते ही automatically चलता है, और memory, files या connections जैसे resources release करने के लिए use होता है.
कई objects के destructors किस order में चलते हैं?
Creation के उल्टे order में (LIFO) — scope खत्म होने पर सबसे बाद में बना object सबसे पहले destroy होता है.
क्या destructor overload हो सकता है या parameters ले सकता है?
नहीं. Class का exactly एक destructor होता है, बिना parameters और बिना return type — constructors के उलट जो आज़ादी से overload होते हैं.