ACtE03 · Chapter 3Programming & Applications🏛️ Past Licensure QuestionID: 3a1dba32
Consider multiple inheritance in C++:
C++class Base1 { ... };
class Base2 { ... };
class Derived : public Base1, public Base2 { ... };
When an object of class Derived is created, what is the order of constructor execution?
Consider multiple inheritance in C++:
C++
class Base1 { ... };
class Base2 { ... };
class Derived : public Base1, public Base2 { ... };When an object of class Derived is created, what is the order of constructor execution?
Multiple Choice Options
- A
Only Base1 constructor is called
- B✓ Correct Option
Both Base1 and Base2 constructors are called (Base1 first, then Base2)
- C
Only Derived constructor is called
- D
Base2 constructor is called first, then Base1
Detailed Technical Solution & Explanation
In multiple inheritance in C++, base class constructors are executed in the order in which they appear in the class declaration list (public Base1, public Base2), followed by the derived class constructor. Therefore, both Base1 and Base2 constructors are called, with Base1 called before Base2.
Option Analysis:
•
Option A (Only Base1 constructor is called): Incorrect; all base class sub-objects must be initialized.
•
Option B (Both Base1 and Base2 constructors are...): Correct. Constructors are invoked in the order of inheritance declaration (
Base1 then Base2).•
Option C (Only Derived constructor is called): Incorrect; base constructors must run before derived constructor body executes.
•
Option D (Base2 constructor is called first, th...): Incorrect; constructor execution follows declaration order, not member initialization list order.
Reference: Demo · Set 4NEC Licensure Syllabus