ACtE03 · Chapter 3Programming & Applications🏛️ Past Licensure QuestionID: 69781822
Consider the following class declaration in C++:
C++class A : public B, virtual public C {
// ...
};
What is the order of constructor execution when an object of class A is instantiated?
Consider the following class declaration in C++:
C++
class A : public B, virtual public C {
// ...
};What is the order of constructor execution when an object of class A is instantiated?
Multiple Choice Options
- A
B(), C(), A()
- B✓ Correct Option
C(), B(), A()
- C
A(), B(), C()
- D
A(), C(), B()
Detailed Technical Solution & Explanation
In C++, the rules of constructor execution dictate that:
1. Virtual base class constructors are ALWAYS executed first, regardless of where they appear in the inheritance list (C()).
2. Non-virtual base class constructors are then executed in declaration order (B()).
3. Finally, the derived class constructor body executes (A()).
Hence, the execution sequence is C(), then B(), then A().
Option Analysis:
•
Option A (, C(), A): Incorrect; virtual base classes take precedence over non-virtual base classes.
•
Option B (, B(), A): Correct. Virtual base class
C initializes first, then non-virtual base B, then derived A.•
Option C (, B(), C): Inverted; base classes must always initialize before derived class body.
•
Option D (, C(), B): Inverted construction order.
Reference: Mock Test 02NEC Licensure Syllabus