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:

  1. Constructor of Person

  2. Constructor of Student

Inheritance में Constructors का execution Base Class से Derived Class की ओर होता है।

उदाहरण के लिए, यदि Student class, Person class से inherit करती है, तो execution order इस प्रकार होगा:

  1. Person का Constructor

  2. Student का 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 Called
Student Constructor Called

Explanation

  • The Student class inherits from the Person class.

  • When the object s is created, the Person constructor executes first.

  • After that, the Student constructor executes.

  • This confirms that the base class constructor executes before the derived class constructor.

इस program में:

  • Student class, Person class से inherit करती है।

  • जब object s create होता है, तो सबसे पहले 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:

  1. Destructor of Student

  2. Destructor of Person

Destructors का execution, Constructors के execution के reverse order में होता है।

यदि Student class, Person class से inherit करती है, तो Destructor execution का क्रम इस प्रकार होगा:

  1. Student का Destructor

  2. Person का 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 Called
Student Constructor Called
Student Destructor Called
Person Destructor Called

Explanation

  • First, the Person constructor executes.

  • Next, the Student constructor executes.

  • When the object goes out of scope, the Student destructor executes first.

  • Finally, the Person destructor 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:

  1. Person constructor

  2. Student constructor

  3. Result constructor

Multilevel Inheritance में एक class दूसरी Derived Class से inherit करती है। इसमें Constructors का execution सबसे ऊपर वाली Base Class से अंतिम Derived Class की ओर होता है।

उपरोक्त hierarchy में Constructor execution का क्रम होगा:

  1. Person Constructor

  2. Student Constructor

  3. Result Constructor

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 Constructor
Student Constructor
Result Constructor
Result Destructor
Student Destructor
Person Destructor

Explanation

  • Person is the base class of Student.

  • Student is the base class of Result.

  • During object creation, constructors execute in the order Person, Student, and Result.

  • During object destruction, destructors execute in the reverse order: Result, Student, and Person.

इस program में:

  • Person, Student की Base Class है।

  • Student, Result की Base Class है।

  • Object creation के समय Constructors का क्रम Person, Student और Result होता है।

  • Object destruction के समय Destructors reverse order में Result, Student और Person execute होते हैं।

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 Called
Student Constructor Called
Student Details
Name: Rahul
Roll Number: 101

Explanation

  • The Person class has a parameterized constructor that accepts a name.

  • The Student constructor calls the base class constructor using Person(n).

  • The base class constructor executes before the body of the derived class constructor.

  • The Student constructor then initializes its own data member rollNo.

इस program में:

  • Person class में parameterized Constructor है जो name को argument के रूप में लेता है।

  • Student का Constructor, Person(n) के माध्यम से Base Class के Constructor को call करता है।

  • Base Class का Constructor, Derived Class के Constructor की body से पहले execute होता है।

  • इसके बाद Student का Constructor अपने data member rollNo को 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 Constructor
BaseB Constructor
Derived Constructor
Derived Destructor
BaseB Destructor
BaseA Destructor

Explanation

  • Derived inherits from BaseA first and BaseB second.

  • Therefore, the BaseA constructor executes before the BaseB constructor.

  • The Derived constructor executes after both base constructors.

  • During destruction, the Derived destructor executes first.

  • Then BaseB and BaseA destructors execute in reverse construction order.

इस program में:

  • Derived class पहले 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 Constructor
B Constructor
C Constructor
D Constructor
D Destructor
C Destructor
B Destructor
A Destructor

Explanation

  • A is a virtual base class of both B and C.

  • When the object of D is created, the virtual base class A is initialized first.

  • Next, the non-virtual direct base classes are initialized in their declaration order: B and then C.

  • Finally, the D constructor executes.

  • During destruction, the order is reversed.

इस program में:

  • A, B और C दोनों की Virtual Base Class है।

  • जब D का object create होता है, तो सबसे पहले Virtual Base Class A initialize होती है।

  • इसके बाद non-virtual direct Base Classes declaration order में initialize होती हैं: पहले B और फिर C।

  • अंत में D का Constructor execute होता है।

  • Destruction के समय इसका reverse order होता है।

Order of Constructor and Destructor Execution – Summary

Inheritance StructureConstructor Execution OrderDestructor Execution Order
Single InheritanceBase → DerivedDerived → Base
Multilevel InheritanceTop Base → Intermediate → Final DerivedFinal Derived → Intermediate → Top Base
Multiple InheritanceBase classes in declaration order → DerivedDerived → Base classes in reverse declaration order
Virtual InheritanceVirtual bases first, then non-virtual bases, then derived classReverse 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

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

Complete C++ Programming Course - OOP and Modern C++

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)