ProgrammingHardGATE CS · TCS NQT Prime · Product Company SDE

In C++, a base class constructor calls a function that is overridden in a derived class. Which version actually runs while the base constructor is executing?

  1. A. It depends only on whether the function is pure virtual
  2. B. The base class's own version, because the object's dynamic type is still Base while the base constructor runs
  3. C. The derived class's override, because the object is really a Derived at heart
  4. D. It is undefined and typically crashes

Correct answer: B. The base class's own version, because the object's dynamic type is still Base while the base constructor runs

During a base class constructor's execution, the object behaves as if it were exactly of the base type; virtual calls resolve to the base's own version, not any not-yet-constructed derived override.

More OOP with C++ and Java questions

Exams that ask this topic