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

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

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

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.