Order of Execution of Constructors and Destructors in C++ Inheritance
Constructors and Destructors are important features of Object-Oriented Programming in C++. When inheritance is used, constructors and destructors of both base and derived classes are involved in object creation and destruction. A constructor initializes an object, while a destructor performs cleanup when an object is destroyed. In inheritance, the base class constructor executes before the derived class constructor, whereas the derived class destructor executes before the base class destructor.
C++ में Constructors और Destructors, Object-Oriented Programming के महत्वपूर्ण features हैं। जब Inheritance का उपयोग किया जाता है, तब object के creation और destruction के दौरान Base Class तथा Derived Class दोनों के constructors और destructors की भूमिका होती है। Constructor object को initialize करता है, जबकि Destructor object destroy होने पर cleanup का कार्य करता है। Inheritance में Base Class का Constructor, Derived Class के Constructor से पहले execute होता है, जबकि Derived Class का Destructor, Base Class के Destructor से पहले execute होता है।
Constructor in C++ Inheritance
A constructor is a special member function of a class that is automatically called when an object is created. It has the same name as the class and does not have a return type.
When an object of a derived class is created, the constructor of its base class is executed first. After the base class constructor completes, the derived class constructor is executed.
Constructor एक special member function है जो object create होने पर automatically call होता है। इसका नाम class के नाम के समान होता है और इसका कोई return type नहीं होता।
जब Derived Class का object create होता है, तो सबसे पहले उसकी Base Class का Constructor execute होता है। Base Class का Constructor complete होने के बाद Derived Class का Constructor execute होता है।
Destructor in C++ Inheritance
A destructor is a special member function that is automatically called when an object is destroyed. It has the same name as the class, preceded by a tilde (~).
When a derived class object is destroyed, the derived class destructor executes first. After that, the base class destructor executes.
Destructor एक special member function है जो object destroy होने पर automatically call होता है। इसका नाम class के नाम के समान होता है, लेकिन इसके पहले tilde (~) symbol लगाया जाता है।
जब Derived Class का object destroy होता है, तो सबसे पहले Derived Class का Destructor execute होता है। इसके बाद Base Class का Destructor execute होता है।
Order of Constructor Execution in Inheritance
In inheritance, constructors execute from the base class towards the derived class.
For example, if Student inherits from Person, the execution order is:
Constructor of
PersonConstructor of
Student
Inheritance में Constructors का execution Base Class से Derived Class की ओर होता है।
उदाहरण के लिए, यदि Student class, Person class से inherit करती है, तो execution order इस प्रकार होगा:
Personका ConstructorStudentका Constructor
Program: Constructor Execution in Single Inheritance
#include <iostream>using namespace std;class Person {public:Person() {cout << "Person Constructor Called" << endl;}};class Student : public Person {public:Student() {cout << "Student Constructor Called" << endl;}};int main() {Student s;return 0;}
Output
Person Constructor CalledStudent Constructor Called
Explanation
The
Studentclass inherits from thePersonclass.When the object
sis created, thePersonconstructor executes first.After that, the
Studentconstructor executes.This confirms that the base class constructor executes before the derived class constructor.
इस program में:
Studentclass,Personclass से inherit करती है।जब object
screate होता है, तो सबसे पहलेPersonका Constructor execute होता है।इसके बाद
Studentका Constructor execute होता है।इससे स्पष्ट होता है कि Base Class का Constructor, Derived Class के Constructor से पहले execute होता है।
Order of Destructor Execution in Inheritance
Destructors execute in the reverse order of constructor execution.
If Student inherits from Person, the destructor execution order is:
Destructor of
StudentDestructor of
Person
Destructors का execution, Constructors के execution के reverse order में होता है।
यदि Student class, Person class से inherit करती है, तो Destructor execution का क्रम इस प्रकार होगा:
Studentका DestructorPersonका Destructor
Program: Destructor Execution in Single Inheritance
#include <iostream>using namespace std;class Person {public:Person() {cout << "Person Constructor Called" << endl;}~Person() {cout << "Person Destructor Called" << endl;}};class Student : public Person {public:Student() {cout << "Student Constructor Called" << endl;}~Student() {cout << "Student Destructor Called" << endl;}};int main() {Student s;return 0;}
Output
Person Constructor CalledStudent Constructor CalledStudent Destructor CalledPerson Destructor Called
Explanation
First, the
Personconstructor executes.Next, the
Studentconstructor executes.When the object goes out of scope, the
Studentdestructor executes first.Finally, the
Persondestructor executes.
इस program में:
सबसे पहले
Personका Constructor execute होता है।इसके बाद
Studentका Constructor execute होता है।जब object का scope समाप्त होता है, तो सबसे पहले
Studentका Destructor execute होता है।अंत में
Personका Destructor execute होता है।
Constructor Execution in Multilevel Inheritance
In Multilevel Inheritance, a class is derived from another derived class. Constructors execute from the topmost base class to the final derived class.
Consider the following hierarchy:
Person||Student||Result
The constructor execution order is:
PersonconstructorStudentconstructorResultconstructor
Multilevel Inheritance में एक class दूसरी Derived Class से inherit करती है। इसमें Constructors का execution सबसे ऊपर वाली Base Class से अंतिम Derived Class की ओर होता है।
उपरोक्त hierarchy में Constructor execution का क्रम होगा:
PersonConstructorStudentConstructorResultConstructor
Program: Constructor and Destructor in Multilevel Inheritance
#include <iostream>using namespace std;class Person {public:Person() {cout << "Person Constructor" << endl;}~Person() {cout << "Person Destructor" << endl;}};class Student : public Person {public:Student() {cout << "Student Constructor" << endl;}~Student() {cout << "Student Destructor" << endl;}};class Result : public Student {public:Result() {cout << "Result Constructor" << endl;}~Result() {cout << "Result Destructor" << endl;}};int main() {Result r;return 0;}
Output
Person ConstructorStudent ConstructorResult ConstructorResult DestructorStudent DestructorPerson Destructor
Explanation
Personis the base class ofStudent.Studentis the base class ofResult.During object creation, constructors execute in the order
Person,Student, andResult.During object destruction, destructors execute in the reverse order:
Result,Student, andPerson.
इस program में:
Person,Studentकी Base Class है।Student,Resultकी Base Class है।Object creation के समय Constructors का क्रम
Person,StudentऔरResultहोता है।Object destruction के समय Destructors reverse order में
Result,StudentऔरPersonexecute होते हैं।
Calling Parameterized Base Class Constructor
A derived class can call a parameterized constructor of its base class using the member initializer list.
The base class constructor is specified after a colon (:) in the derived class constructor definition.
Derived Class, Member Initializer List की सहायता से Base Class के parameterized constructor को call कर सकती है।
Derived Class के Constructor में colon (:) के बाद Base Class के Constructor को specify किया जाता है।
Syntax
DerivedClass(arguments) : BaseClass(baseArguments) {// Constructor body}
Program: Parameterized Constructor in Inheritance
#include <iostream>#include <string>using namespace std;class Person {protected:string name;public:Person(string n) {name = n;cout << "Person Constructor Called" << endl;}};class Student : public Person {private:int rollNo;public:Student(string n, int r) : Person(n) {rollNo = r;cout << "Student Constructor Called" << endl;}void display() {cout << "Name: " << name << endl;cout << "Roll Number: " << rollNo << endl;}};int main() {Student s("Rahul", 101);cout << "\nStudent Details\n";s.display();return 0;}
Output
Person Constructor CalledStudent Constructor CalledStudent DetailsName: RahulRoll Number: 101
Explanation
The
Personclass has a parameterized constructor that accepts a name.The
Studentconstructor calls the base class constructor usingPerson(n).The base class constructor executes before the body of the derived class constructor.
The
Studentconstructor then initializes its own data memberrollNo.
इस program में:
Personclass में parameterized Constructor है जो name को argument के रूप में लेता है।Studentका Constructor,Person(n)के माध्यम से Base Class के Constructor को call करता है।Base Class का Constructor, Derived Class के Constructor की body से पहले execute होता है।
इसके बाद
Studentका Constructor अपने data memberrollNoको initialize करता है।
Constructor and Destructor Execution in Multiple Inheritance
In Multiple Inheritance, the base class constructors execute in the order in which the base classes are listed in the derived class declaration.
The destructors execute in the reverse order of base-class construction, after the derived class destructor body has executed.
Multiple Inheritance में Base Class के Constructors उसी क्रम में execute होते हैं जिस क्रम में Base Classes को Derived Class की declaration में लिखा जाता है।
Derived Class के Destructor की body execute होने के बाद Base Class के Destructors, construction के reverse order में execute होते हैं।
Program: Constructor and Destructor in Multiple Inheritance
#include <iostream>using namespace std;class BaseA {public:BaseA() {cout << "BaseA Constructor" << endl;}~BaseA() {cout << "BaseA Destructor" << endl;}};class BaseB {public:BaseB() {cout << "BaseB Constructor" << endl;}~BaseB() {cout << "BaseB Destructor" << endl;}};class Derived : public BaseA, public BaseB {public:Derived() {cout << "Derived Constructor" << endl;}~Derived() {cout << "Derived Destructor" << endl;}};int main() {Derived obj;return 0;}
Output
BaseA ConstructorBaseB ConstructorDerived ConstructorDerived DestructorBaseB DestructorBaseA Destructor
Explanation
Derivedinherits fromBaseAfirst andBaseBsecond.Therefore, the
BaseAconstructor executes before theBaseBconstructor.The
Derivedconstructor executes after both base constructors.During destruction, the
Deriveddestructor executes first.Then
BaseBandBaseAdestructors execute in reverse construction order.
इस program में:
Derivedclass पहलेBaseAऔर उसके बादBaseBसे inherit करती है।इसलिए
BaseAका Constructor,BaseBके Constructor से पहले execute होता है।दोनों Base Constructors के बाद
Derivedका Constructor execute होता है।Destruction के समय सबसे पहले
Derivedका Destructor execute होता है।इसके बाद
BaseBऔरBaseAके Destructors reverse construction order में execute होते हैं।
Constructor and Destructor Execution in Virtual Inheritance
When virtual inheritance is used, virtual base classes are initialized before non-virtual base classes and the derived class.
The most-derived class is responsible for initializing virtual base classes. If it does not explicitly initialize a virtual base class, that virtual base class's default constructor is used if available.
Virtual Inheritance में Virtual Base Classes का initialization, non-virtual Base Classes और Derived Class से पहले होता है।
Most-derived class, Virtual Base Classes को initialize करने के लिए responsible होती है। यदि वह किसी Virtual Base Class को explicitly initialize नहीं करती, तो उपलब्ध होने पर उसका default constructor उपयोग किया जाता है।
Program: Constructor Execution with Virtual Base Class
#include <iostream>using namespace std;class A {public:A() {cout << "A Constructor" << endl;}~A() {cout << "A Destructor" << endl;}};class B : virtual public A {public:B() {cout << "B Constructor" << endl;}~B() {cout << "B Destructor" << endl;}};class C : virtual public A {public:C() {cout << "C Constructor" << endl;}~C() {cout << "C Destructor" << endl;}};class D : public B, public C {public:D() {cout << "D Constructor" << endl;}~D() {cout << "D Destructor" << endl;}};int main() {D obj;return 0;}
Output
A ConstructorB ConstructorC ConstructorD ConstructorD DestructorC DestructorB DestructorA Destructor
Explanation
Ais a virtual base class of bothBandC.When the object of
Dis created, the virtual base classAis initialized first.Next, the non-virtual direct base classes are initialized in their declaration order:
Band thenC.Finally, the
Dconstructor executes.During destruction, the order is reversed.
इस program में:
A,BऔरCदोनों की Virtual Base Class है।जब
Dका object create होता है, तो सबसे पहले Virtual Base ClassAinitialize होती है।इसके बाद non-virtual direct Base Classes declaration order में initialize होती हैं: पहले
Bऔर फिरC।अंत में
Dका Constructor execute होता है।Destruction के समय इसका reverse order होता है।
Order of Constructor and Destructor Execution – Summary
| Inheritance Structure | Constructor Execution Order | Destructor Execution Order |
|---|---|---|
| Single Inheritance | Base → Derived | Derived → Base |
| Multilevel Inheritance | Top Base → Intermediate → Final Derived | Final Derived → Intermediate → Top Base |
| Multiple Inheritance | Base classes in declaration order → Derived | Derived → Base classes in reverse declaration order |
| Virtual Inheritance | Virtual bases first, then non-virtual bases, then derived class | Reverse of construction order |
Frequently Asked Questions (FAQs)
Which constructor is called first in inheritance?
The base class constructor is called first, followed by the derived class constructor.
Inheritance में सबसे पहले Base Class का Constructor call होता है, उसके बाद Derived Class का Constructor call होता है।
Which destructor is called first in inheritance?
The derived class destructor is called first, followed by the base class destructor.
Inheritance में सबसे पहले Derived Class का Destructor call होता है, उसके बाद Base Class का Destructor call होता है।
Can a derived class call a parameterized constructor of its base class?
Yes. A derived class can call a parameterized base class constructor using a member initializer list.
हाँ। Derived Class, Member Initializer List की सहायता से Base Class के parameterized Constructor को call कर सकती है।
In which order are constructors called in multiple inheritance?
Base class constructors execute in the order in which the base classes are listed in the derived class declaration. The derived class constructor executes after them.
Multiple Inheritance में Base Class के Constructors उसी क्रम में execute होते हैं जिस क्रम में उन्हें Derived Class की declaration में लिखा जाता है। इसके बाद Derived Class का Constructor execute होता है।
In which order are destructors called in multilevel inheritance?
Destructors execute from the final derived class towards the topmost base class.
Multilevel Inheritance में Destructors का execution अंतिम Derived Class से सबसे ऊपर वाली Base Class की ओर होता है।
What is the role of a virtual base class in constructor execution?
A virtual base class is initialized only once in the most-derived object. Its initialization is controlled by the most-derived class.
Virtual Base Class, most-derived object में केवल एक बार initialize होती है। इसका initialization most-derived class द्वारा नियंत्रित होता है।
Are constructors inherited in C++?
Constructors are not inherited automatically in the ordinary sense. However, C++ supports inheriting constructors using a using Base::Base; declaration. The derived class can also define its own constructors and explicitly call base class constructors.
सामान्य रूप से Constructors automatically inherit नहीं होते। हालांकि, C++ में using Base::Base; declaration की सहायता से constructors inherit किए जा सकते हैं। Derived Class अपने Constructors भी define कर सकती है और Base Class के Constructors को explicitly call कर सकती है।
Constructors and Destructors play an important role in object initialization and destruction in C++ inheritance. Base class constructors execute before derived class constructors, while derived class destructors execute before base class destructors. Understanding their execution order is essential for managing object lifetime, initialization and resource cleanup in inheritance-based programs.
C++ Inheritance में object initialization और destruction के लिए Constructors तथा Destructors महत्वपूर्ण होते हैं। Base Class के Constructors, Derived Class के Constructors से पहले execute होते हैं, जबकि Derived Class के Destructors, Base Class के Destructors से पहले execute होते हैं। Inheritance-based programs में object lifetime, initialization और resource cleanup को सही ढंग से समझने के लिए इनके execution order का ज्ञान आवश्यक है।
Comments
Post a Comment