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?
- A. It depends only on whether the function is pure virtual
- B. The base class's own version, because the object's dynamic type is still Base while the base constructor runs
- C. The derived class's override, because the object is really a Derived at heart
- 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
- A PaymentMethod type requires every concrete implementation to provide charge() and refund(), but there is no shared implementation at all…
- Which kind of call in C++ is resolved using static (compile-time) binding rather than dynamic (run-time) binding?
- Which of these can a Java interface still NOT have, even in a modern version such as Java 17?
- A class overrides equals() but does not override hashCode(). What bug does this risk for instances used as keys in a HashMap or stored in…
- Which run-time mechanism lets a call through a base-class pointer invoke the correct derived-class override in C++?
- cpp struct A { void f() { cout << "A::f"; } }; struct B : A {}; struct C : A {}; struct D : B, C {}; int main() { D d; d.f(); } What…