Search

11.3 — Order of construction of derived classes

In the previous lesson on basic inheritance in C++, you learned that classes can inherit members and functions from other classes. In this lesson, we’re going to take a closer look at the order of construction that happens when a derived class is instantiated.

First, let’s introduce some new classes that will help us illustrate some important points.

In this example, class Derived is derived from class Base.

Because Derived inherits functions and variables from Base, you may assume that the members of Base are copied into Derived. However, this is not true. Instead, we can consider Derived as a two part class: one part Derived, and one part Base.

You’ve already seen plenty examples of what happens when we instantiate a normal (non-derived) class:

Base is a non-derived class because it does not inherit from any other classes. C++ allocates memory for Base, then calls Base’s default constructor to do the initialization.

Now let’s take a look at what happens when we instantiate a derived class:

If you were to try this yourself, you wouldn’t notice any difference from the previous example where we instantiate non-derived class Base. But behind the scenes, things happen slightly different. As mentioned above, Derived is really two parts: a Base part, and a Derived part. When C++ constructs derived objects, it does so in phases. First, the most-base class (at the top of the inheritance tree) is constructed first. Then each child class is constructed in order, until the most-child class (at the bottom of the inheritance tree) is constructed last.

So when we instantiate an instance of Derived, first the Base portion of Derived is constructed (using the Base default constructor). Once the Base portion is finished, the Derived portion is constructed (using the Derived default constructor). At this point, there are no more derived classes, so we are done.

This process is actually easy to illustrate.

This program produces the following result:

Instantiating Base
Base
Instantiating Derived
Base
Derived

As you can see, when we constructed Derived, the Base portion of Derived got constructed first. This makes sense: logically, a child can not exist without a parent. It’s also the safe way to do things: the child class often uses variables and functions from the parent, but the parent class knows nothing about the child. Instantiating the parent class first ensures those variables are already initialized by the time the derived class is created and ready to use them.

Order of construction for inheritance chains

It is sometimes the case that classes are derived from other classes, which are themselves derived from other classes. For example:

Remember that C++ always constructs the “first” or “most base” class first. It then walks through the inheritance tree in order and constructs each successive derived class.

Here’s a short program that illustrates the order of creation all along the inheritance chain.

This code prints the following:

Constructing A:
A
Constructing B:
A
B
Constructing C:
A
B
C
Constructing D:
A
B
C
D

Conclusion

C++ constructs derived classes in phases, starting with the most-base class (at the top of the inheritance tree) and finishing with the most-child class (at the bottom of the inheritance tree). As each class is constructed, the appropriate constructor from that class is called to initialize that part of the class.

You will note that our example classes in this section have all used base class default constructors (for simplicity). In the next lesson, we will take a closer look at the role of constructors in the process of constructing derived classes (including how to explicitly choose which base class constructor you want your derived class to use).

11.4 -- Constructors and initialization of derived classes
Index
11.2 -- Basic inheritance in C++

9 comments to 11.3 — Order of construction of derived classes

  • Omri

    Hello Alex,
    A thought:
    Discussion up to here is understood assuming a single constructor for each of the parent classes.
    In my view this should be emphasized from the beginning since I was immediately occupied with the dilemma what should occur when a class has more then one constructor? Which one of them will be executed when its child class object is instantiated?
    I have no clue…

    • Alex

      This topic is covered in detail in the next lesson. I’ve updated the note at the bottom of this lesson to indicate so, in case other readers have the same question.

  • Matt

    In the code before section "Order of construction for inheritance chains", you declare "using namespace std" but you still use "std::cout" in both of your constructors.

  • Vlad

    Hello Alex

    While the derived classes does allow simplification of more complex problems/classes, how does this translate into the executable itself? Will this be an overhead/performance issue?

    And, regarding the next few lessons (I went back here to ask, seems more appropiate), if a derived class is modified -- for example, the constructor, to include the base class in its definition -- how will this impact the performance of the final executable?

    I’m thinking in terms of a 3rd party library, closed source: it comes with a set of functions, but suppose you see potential improvements. You can only add derived classes to it, so how will those improvements fare with the addition of classes, in terms of the binary executable execution, memory, etc if the improvements aren’t on a major scale? I hope I managed to get the message.

    • Alex

      Assuming you avoid virtual functions, the only performance overhead may be that your program might use slightly more memory (due to padding issues). However, virtual functions do have a performance impact, since virtual function resolution happens at runtime.

  • Ian

    That was amazing, the inheritance idea really snapped into my head after producing your examples myself. Fantastic stuff Alex. Great Tutorials!

  • Beutifull text, thx!
    ___________________
    my site: http://almaz-job.ru/

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter