Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Constructor and Destructor in Inheritance | Inheritance Overview
C++ OOP

Constructor and Destructor in InheritanceConstructor and Destructor in Inheritance

Base Class Constructor First

In the context of inheritance, constructors play a vital role in initializing derived classes correctly. Understanding the sequence in which constructors are invoked is key to grasping inheritance dynamics. Look at the output of the code snippet below to see the order of constructors calls.

cpp

main.cpp

Remember

Once the Base Class constructor has completed its initialization, the Derived Class constructor is executed.

The superclass is called first because it has to initialize the inherited members of the subclass. This ensures that the subclass starts with a valid state and can rely on the initialized state of its base class.

cpp

main.cpp

In the example we call constructor with parameters in initializer list. You have to explicitly call the superclass constructor within the initializer list of the subclass constructor. If you don't specify a base class constructor in the initializer list, the default constructor of the superclass is called automatically.

Derived Class Destructor First

When an object is destroyed, destructors are invoked in the reverse order of their constructors. This means that destructors are called for the most derived class first and then for each base class in the reverse order of their declaration.

cpp

main.cpp

Remember

Only after the Base Class destructor has finished its cleanup, the Derived Class destructor is invoked.

1. When does the superclass constructor get called in relation to the subclass constructor?
2. How are destructors invoked in inheritance hierarchies?

When does the superclass constructor get called in relation to the subclass constructor?

Select the correct answer

How are destructors invoked in inheritance hierarchies?

Select the correct answer

Everything was clear?

Section 4. Chapter 4

Constructor and Destructor in InheritanceConstructor and Destructor in Inheritance

Base Class Constructor First

In the context of inheritance, constructors play a vital role in initializing derived classes correctly. Understanding the sequence in which constructors are invoked is key to grasping inheritance dynamics. Look at the output of the code snippet below to see the order of constructors calls.

cpp

main.cpp

Remember

Once the Base Class constructor has completed its initialization, the Derived Class constructor is executed.

The superclass is called first because it has to initialize the inherited members of the subclass. This ensures that the subclass starts with a valid state and can rely on the initialized state of its base class.

cpp

main.cpp

In the example we call constructor with parameters in initializer list. You have to explicitly call the superclass constructor within the initializer list of the subclass constructor. If you don't specify a base class constructor in the initializer list, the default constructor of the superclass is called automatically.

Derived Class Destructor First

When an object is destroyed, destructors are invoked in the reverse order of their constructors. This means that destructors are called for the most derived class first and then for each base class in the reverse order of their declaration.

cpp

main.cpp

Remember

Only after the Base Class destructor has finished its cleanup, the Derived Class destructor is invoked.

1. When does the superclass constructor get called in relation to the subclass constructor?
2. How are destructors invoked in inheritance hierarchies?

When does the superclass constructor get called in relation to the subclass constructor?

Select the correct answer

How are destructors invoked in inheritance hierarchies?

Select the correct answer

Everything was clear?

Section 4. Chapter 4
some-alt