Order of Constructor and Destructor Execution in C++ - Examples
In C++, constructors and destructors are automatically executed during the creation and destruction of objects. However, when a program contains multiple objects, inheritance, arrays or dynamically allocated objects, their execution follows specific rules. Understanding the order of constructor and destructor execution is important for learning object lifetime, inheritance and memory management in C++.
C++ में Constructors और Destructors objects के creation और destruction के दौरान automatically execute होते हैं। लेकिन जब program में multiple objects, inheritance या arrays होते हैं, तो इनके execution का एक निश्चित क्रम होता है।
1. Order of Constructor and Destructor Execution
The basic rule for ordinary local objects in the same scope is:
Constructors are called in the order in which objects are initialized.
Destructors are called in the reverse order when their lifetimes end.
Constructor execution: First In, First Out (FIFO)
Destructor execution: Last In, First Out (LIFO)
एक ही scope में ordinary local objects के constructors initialization order में execute होते हैं, जबकि destructors reverse order में execute होते हैं।
2. Order of Constructor and Destructor Execution for Multiple Objects
When multiple objects of the same class are created, their constructors are called in the order of initialization. Their destructors are called in reverse order.
C++ Program
#include <iostream>using namespace std;class Demo {int number;public:Demo(int n) {number = n;cout << "Constructor: " << number << endl;}~Demo() {cout << "Destructor: " << number << endl;}};int main() {Demo obj1(10);Demo obj2(20);Demo obj3(30);cout << "Inside Main Function" << endl;return 0;}
Output
Constructor: 10Constructor: 20Constructor: 30Inside Main FunctionDestructor: 30Destructor: 20Destructor: 10
Explanation
Three objects are created in the following order:
obj1obj2obj3
Therefore, their constructors execute in the same order.
When execution leaves main(), the objects are destroyed in reverse order:
obj3obj2obj1
इस program में तीन objects क्रमशः create किए गए हैं। इसलिए constructors भी उसी क्रम में call होते हैं। जब main() function समाप्त होता है, तब सबसे बाद में बनाया गया object सबसे पहले destroy होता है।
3. Order of Constructor and Destructor Execution in Nested Blocks
When objects are created in different nested blocks, each object's lifetime is associated with its block.
An object is destroyed when execution leaves the block in which it was created. Objects in an inner block are destroyed before objects in an enclosing block.
Nested blocks में object का lifetime उसके संबंधित block तक रहता है। Inner block समाप्त होने पर उसके objects पहले destroy होते हैं।
C++ Program
#include <iostream>using namespace std;class Demo {int number;public:Demo(int n) {number = n;cout << "Constructor: " << number << endl;}~Demo() {cout << "Destructor: " << number << endl;}};int main() {Demo obj1(10);{Demo obj2(20);Demo obj3(30);cout << "Inside Inner Block" << endl;}cout << "Outside Inner Block" << endl;return 0;}
Output
Constructor: 10Constructor: 20Constructor: 30Inside Inner BlockDestructor: 30Destructor: 20Outside Inner BlockDestructor: 10
Explanation
obj1is created in the outer block.obj2andobj3are created in the inner block.When the inner block ends,
obj3andobj2are destroyed in reverse order.When
main()ends,obj1is destroyed.
यहाँ obj2 और obj3 inner block में बनाए गए हैं। इसलिए inner block समाप्त होते ही ये दोनों objects destroy हो जाते हैं। obj1 outer block में है, इसलिए वह main() के अंत में destroy होता है।
4. Order of Constructor and Destructor Execution in Arrays of Objects
When an array of objects is created, its elements are initialized in increasing index order.
During destruction, the elements are destroyed in reverse index order.
Array of Objects में elements का construction index order में होता है और destruction reverse index order में होता है।
C++ Program
#include <iostream>using namespace std;class Demo {int number;public:Demo() {number = 0;cout << "Constructor Called" << endl;}~Demo() {cout << "Destructor Called" << endl;}};int main() {Demo arr[3];cout << "Array Created" << endl;return 0;}
Output
Constructor CalledConstructor CalledConstructor CalledArray CreatedDestructor CalledDestructor CalledDestructor Called
Explanation
The array contains three objects:
arr[0]arr[1]arr[2]
Their constructors are called in increasing index order.
When the array is destroyed, its elements are destroyed in reverse order:
arr[2]arr[1]arr[0]
इस program में तीन objects का array बनाया गया है। पहले arr[0], फिर arr[1] और अंत में arr[2] initialize होता है। Destruction के समय इसका क्रम उल्टा होता है।
5. Order of Constructor and Destructor Execution in Inheritance
In inheritance, the base class constructor is called before the derived class constructor.
During destruction, the derived class destructor is called before the base class destructor.
Construction order:
Base Class → Derived Class
Destruction order:
Derived Class → Base Class
Inheritance में Derived Class का object create होने पर पहले Base Class का constructor call होता है। Object destroy होने पर पहले Derived Class का destructor और फिर Base Class का destructor call होता है।
C++ Program
#include <iostream>using namespace std;class Base {public:Base() {cout << "Base Constructor" << endl;}~Base() {cout << "Base Destructor" << endl;}};class Derived : public Base {public:Derived() {cout << "Derived Constructor" << endl;}~Derived() {cout << "Derived Destructor" << endl;}};int main() {Derived obj;cout << "Inside Main Function" << endl;return 0;}
Output
Base ConstructorDerived ConstructorInside Main FunctionDerived DestructorBase Destructor
Explanation
When object obj of the Derived class is created:
The
Baseconstructor is called.The
Derivedconstructor is called.
When obj is destroyed:
The
Deriveddestructor is called.The
Basedestructor is called.
यहाँ derived object के निर्माण के समय base constructor पहले execute होता है। Destruction के समय derived destructor पहले execute होता है।
6. Order of Constructor and Destructor Execution in Multilevel Inheritance
In multilevel inheritance, the constructors execute from the topmost base class towards the most derived class.
Destructors execute in exactly the reverse order.
Multilevel Inheritance में सबसे ऊपर की Base Class का constructor सबसे पहले execute होता है। Destruction के समय सबसे derived class का destructor सबसे पहले execute होता है।
C++ Program
#include <iostream>using namespace std;class A {public:A() {cout << "Constructor of A" << endl;}~A() {cout << "Destructor of A" << endl;}};class B : public A {public:B() {cout << "Constructor of B" << endl;}~B() {cout << "Destructor of B" << endl;}};class C : public B {public:C() {cout << "Constructor of C" << endl;}~C() {cout << "Destructor of C" << endl;}};int main() {C obj;cout << "Inside Main Function" << endl;return 0;}
Output
Constructor of AConstructor of BConstructor of CInside Main FunctionDestructor of CDestructor of BDestructor of A
Explanation
The inheritance relationship is:
A → B → C
During object creation:
Constructor of
Aexecutes.Constructor of
Bexecutes.Constructor of
Cexecutes.
During object destruction:
Destructor of
Cexecutes.Destructor of
Bexecutes.Destructor of
Aexecutes.
Multilevel inheritance में constructor execution top-to-bottom और destructor execution bottom-to-top होता है।
7. Order of Constructor and Destructor Execution for Dynamically Allocated Objects
When an object is created using the new operator, its constructor is called automatically.
When the corresponding delete expression is executed, its destructor is called.
For multiple dynamically allocated objects, the order depends on the sequence of allocation and deletion expressions, not simply on the reverse order of allocation.
new से dynamically allocated object का constructor automatically call होता है। जब corresponding delete execute किया जाता है, तब उसका destructor call होता है।
C++ Program
#include <iostream>using namespace std;class Demo {int number;public:Demo(int n) {number = n;cout << "Constructor: " << number << endl;}~Demo() {cout << "Destructor: " << number << endl;}};int main() {Demo* p1 = new Demo(10);Demo* p2 = new Demo(20);cout << "Deleting First Object" << endl;delete p1;cout << "Deleting Second Object" << endl;delete p2;return 0;}
Output
Constructor: 10Constructor: 20Deleting First ObjectDestructor: 10Deleting Second ObjectDestructor: 20
Explanation
new Demo(10)creates the first object.new Demo(20)creates the second object.delete p1destroys the first object.delete p2destroys the second object.
Notice that the destruction order follows the explicit delete statements. It is not automatically reversed as with ordinary local objects.
इस उदाहरण में dynamically allocated objects को जिस क्रम में delete किया गया है, उसी क्रम में उनके destructors call होते हैं। इसलिए dynamic objects के destruction का क्रम programmer द्वारा किए गए deletion operations पर निर्भर करता है।
8. Order of Constructor and Destructor Execution in Multiple Inheritance
In multiple inheritance, base class constructors are called in the order in which the base classes are listed in the derived class declaration, regardless of their order in the constructor's member initializer list.
Destructors execute in the reverse order.
Multiple Inheritance में base class constructors का execution order derived class की inheritance declaration में दिए गए क्रम पर निर्भर करता है। Destructor इसका reverse order follow करते हैं।
C++ Program
#include <iostream>using namespace std;class A {public:A() {cout << "Constructor of A" << endl;}~A() {cout << "Destructor of A" << endl;}};class B {public:B() {cout << "Constructor of B" << endl;}~B() {cout << "Destructor of B" << endl;}};class C : public A, public B {public:C() {cout << "Constructor of C" << endl;}~C() {cout << "Destructor of C" << endl;}};int main() {C obj;cout << "Inside Main Function" << endl;return 0;}
Output
Constructor of AConstructor of BConstructor of CInside Main FunctionDestructor of CDestructor of BDestructor of A
Explanation
The class C inherits from classes A and B:
class C : public A, public BTherefore:
Constructor of
Aexecutes first.Constructor of
Bexecutes second.Constructor of
Cexecutes last.
During destruction, the sequence is reversed.
Multiple Inheritance में base classes का initialization order inheritance declaration से तय होता है। Destruction के समय पहले derived class और फिर base classes reverse order में destroy होती हैं।
9. Summary of Constructor and Destructor Execution Order
The following table summarizes the common execution rules.
| Situation | Constructor Order | Destructor Order |
|---|---|---|
| Multiple local objects | Initialization order | Reverse initialization order |
| Nested blocks | Creation order | Inner block objects first |
| Array of objects | Increasing index order | Decreasing index order |
| Single inheritance | Base → Derived | Derived → Base |
| Multilevel inheritance | Topmost base → Derived | Derived → Topmost base |
| Multiple inheritance | Base declaration order → Derived | Derived → Reverse base declaration order |
| Dynamic objects | Allocation and initialization order | Explicit deletion order |
10. Frequently Asked Questions (FAQs)
Q1. In which order are constructors called for multiple objects?
Constructors are called in the order in which the objects are initialized within the relevant scope.
Multiple local objects के constructors initialization order में call होते हैं।
Q2. In which order are destructors called for multiple local objects?
Destructors are called in reverse order of initialization when the objects' lifetimes end.
Local objects के destructors reverse initialization order में call होते हैं।
Q3. Which constructor is called first in inheritance?
The base class constructor is called before the derived class constructor.
Inheritance में Base Class का constructor पहले call होता है।
Q4. Which destructor is called first in inheritance?
The derived class destructor is called before the base class destructor.
Inheritance में Derived Class का destructor पहले call होता है।
Q5. What is the order of constructors in multiple inheritance?
Base class constructors are called in the order in which the base classes appear in the derived class declaration.
Multiple Inheritance में base constructors inheritance declaration के क्रम में call होते हैं।
Q6. Are dynamically allocated objects destroyed in reverse allocation order?
Not necessarily. Their destructors are called when the corresponding deletion operations occur. The programmer controls the deletion sequence.
नहीं, Dynamic Objects का destruction corresponding delete operations के क्रम पर निर्भर करता है।
Q7. What is the order of constructor and destructor execution in an array of objects?
Array elements are constructed in increasing index order and destroyed in reverse index order.
Array के objects का construction increasing index order और destruction reverse index order में होता है।
The order of constructor and destructor execution is an important concept in C++. Constructors initialize objects in a defined order, while destructors generally execute in the reverse order of construction for objects whose lifetimes are nested. These rules are especially important in multiple objects, arrays, inheritance, nested blocks and dynamic memory management.
C++ में Constructor और Destructor का execution order object lifetime को समझने के लिए बहुत महत्वपूर्ण है। Constructors initialization के निर्धारित क्रम को follow करते हैं, जबकि nested object lifetimes में destructors सामान्यतः reverse order में execute होते हैं।
Comments
Post a Comment