Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
Free tutorials in Hindi & English Daily computer, mobile and IT guides Beginner friendly learning
Blog · C++ · 04 Jul 2026 · Hindi + English

C++ में Constructor और Destructor (Execution Order के साथ)

Constructor object के जन्म पर automatically चलता है, destructor उसके खत्म होने पर. Syntax (~ destructor के लिए), execution order proof और destructor क्यों ज़रूरी है.

The hotel-room analogy

Think of an object's life like a hotel stay. Constructor = check-in: the moment you enter, the room is automatically prepared — bed made, AC on, towels placed. Destructor = check-out: the moment you leave, the room is automatically cleaned and keys taken back. You never call housekeeping yourself — it happens automatically at birth and death of the stay. Same with objects: constructor runs automatically when the object is created, destructor when it is destroyed.

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)
}
-- main starts -- Aman admitted (constructor) Priya admitted (constructor) -- main ends -- Priya left school (destructor) Aman left school (destructor)
Look at the destructor order: Priya first, then Aman — reverse of creation! Objects are destroyed LIFO (last created, first destroyed), like plates stacked and removed. This exact output-order question is asked in written tests again and again.

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

PointConstructorDestructor
NameClass name~ClassName
When it runsObject createdObject destroyed / scope ends
ParametersAllowed (can overload many)Never — exactly one destructor
Return typeNoneNone
PurposeInitialize / acquire resourcesCleanup / release resources
Call order (multiple objects)Creation orderReverse of creation (LIFO)

Hotel-room analogy

Object की ज़िंदगी को hotel stay की तरह सोचिए. Constructor = check-in: घुसते ही कमरा automatically तैयार मिलता है — bed लगा, AC on, towels रखे. Destructor = check-out: निकलते ही कमरा automatically साफ होता है और चाबियां वापस. आप housekeeping को खुद कभी नहीं बुलाते — यह stay के जन्म और अंत पर अपने आप होता है. Objects के साथ भी वही: constructor object बनते ही automatically चलता है, destructor उसके खत्म होते ही.

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 खत्म)
}
-- main starts -- Aman admitted (constructor) Priya admitted (constructor) -- main ends -- Priya left school (destructor) Aman left school (destructor)
Destructor order देखिए: पहले Priya, फिर Aman — creation का उल्टा! Objects LIFO destroy होते हैं (last created, first destroyed), जैसे plates रखी और उठाई जाती हैं. यही output-order question written tests में बार-बार आता है.

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

PointConstructorDestructor
NameClass का नाम~ClassName
कब चलता हैObject बनते हीObject destroy / scope खत्म होते ही
ParametersAllowed (कई overload हो सकते हैं)कभी नहीं — exactly एक destructor
Return typeनहींनहीं
PurposeInitialize / resources लेनाCleanup / resources छोड़ना
Order (कई objects)Creation orderCreation का उल्टा (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 होते हैं.