If a derived class object is created, which constructor is called first?

Question: If a derived class object is created, which constructor is called first?
a. Derived class constructor
b. Base class constructor
c. Totally Depends on that how we call the object
d. Both a and B
MCQs Answer:

Base class constructor


If a derived class object is created, which constructor is called first

Now, let’s see the diagram. The numbers 1, 2, 3, and 4 in the diagram represents a sequence of execution.

The diagram has 3 parts.

  1. The Left most diagram represents single inheritance
    • When the object of the child class creates, then firstly constructor of the base class will execute(represents with 1), and then the child class constructor will execute (represents with 2).
  2. The central diagram represents Multiple inheritance
    • When the object of the child class creates, then firstly constructor of the base classes will execute(represents with 1 and 2 in parent classes), and then the child class constructor will execute (represents with 3).
  3. The rightmost diagram represents Multilevel inheritance
    • When the object of the child class creates, then firstly constructor of the topmost base classes will execute(represents with 1  in top parent class), and then the constructor of the middle base classes will execute(represents with 2  in middle parent class), and then the child class constructor will execute (represents with 3).

C++ Program to represents that If a derived class object is created, then which constructor is called first?

Output

The Parent class constructor will execute first.

The Child class constructor will execute second.

The Child class destructor will execute first.

The Parent class destructor will execute second.

Note: in the case of destructors, the destructor of the child class will execute first and then the destructor of the parent class will execute.

1000+ Programming C Plus Plus MCQs