Constructor and Destructor Order in Inheritance

Constructor and destructor order in inheritance is the fixed sequence in which they run for a derived class object: the base class constructor runs first, then the derived class constructor. Destruction happens in exactly the reverse order, so the derived destructor runs before the base destructor, because each derived part depends on its base part being intact.

3 min read

When you instantiate an object of a derived class, more than one constructor actually runs, the base class's constructor runs first, then the derived class's constructor body executes. Destruction reverses this: the derived class's destructor runs first, then the base class's.

How it works, step by step

  1. Memory is allocated for the full object (base class members plus derived class members).
  2. The base class constructor runs, initializing everything the base class is responsible for.
  3. The derived class constructor body runs, initializing the derived class's own members and doing any additional setup.
  4. The object is now fully constructed and ready to use.

Destruction happens in exactly the opposite order:

  1. The derived class's destructor runs first, cleaning up whatever it's responsible for.
  2. The base class's destructor runs next, cleaning up its own resources.
  3. Memory for the object is freed.

This "construct base-to-derived, destroy derived-to-base" order makes sense once you think about dependencies: a derived class's constructor often assumes the base part of the object is already fully set up before it runs, and a derived class's destructor often needs the base part to still be intact while it cleans up its own state.

A worked example

#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 d;
    cout << "-- object in use --" << endl;
    return 0;
}

Output:

Base constructor
Derived constructor
-- object in use --
Derived destructor
Base destructor

The base constructor always finishes before the derived constructor begins, and the derived destructor always finishes before the base destructor begins.

Where this matters in real systems

This ordering is exactly why base class destructors need to be marked virtual when a class is meant to be used polymorphically. If you delete a derived object through a base class pointer and the base destructor isn't virtual, the C++ standard says the behavior is undefined. In practice the usual result is that only the base destructor runs and the derived class's own cleanup (closing files, freeing memory it allocated, releasing a database connection) is skipped, a common and hard-to-spot source of leaks in C++ codebases.

What interviewers actually ask about constructor and destructor order

  • "In what order do constructors run in an inheritance chain?" Base to derived: the topmost base class first, then down the chain to the most derived class.
  • "In what order do destructors run?" The reverse: most derived class first, then up the chain to the base class.
  • "What happens if you don't explicitly call a base class constructor?" The base class's default (no-argument) constructor is called automatically, if the base class has no default constructor and you don't explicitly call one with arguments, it's a compile error.
  • "Why does a polymorphic base class need a virtual destructor?" Without it, deleting a derived object through a base pointer is undefined behavior; typically only the base class's destructor runs and the derived class's cleanup is skipped, a real source of resource leaks.

Key takeaways

  • Base class constructors run before derived class constructors.
  • Destructors run in the exact opposite order: derived class first, then base class.
  • This ordering ensures a derived class's setup and teardown code can safely assume the base part of the object is already valid.
  • A missing virtual destructor on a polymorphic base class is a classic, real source of incomplete cleanup and memory leaks.
All 72 OOP interview questions

Log in to save which topics you've read across every subject.

Frequently asked questions

In what order do constructors run in an inheritance chain?

Base to derived: the topmost base class first, then down the chain to the most derived class.

In what order do destructors run?

The reverse: most derived class first, then up the chain to the base class.

What happens if you don't explicitly call a base class constructor?

The base class's default (no-argument) constructor is called automatically, if the base class has no default constructor and you don't explicitly call one with arguments, it's a compile error.

Why does a polymorphic base class need a virtual destructor?

Without it, deleting a derived object through a base pointer is undefined behavior; typically only the base class's destructor runs and the derived class's cleanup is skipped, a real source of resource leaks.