ProgrammingVery hardGATE CS · TCS NQT Prime · Product Company SDE
Which run-time mechanism lets a call through a base-class pointer invoke the correct derived-class override in C++?
- A. Function overloading resolved by the compiler
- B. Name mangling of the function signature
- C. The C preprocessor rewriting the call
- D. A virtual function table (vtable) and a hidden vptr in each polymorphic object
Correct answer: D. A virtual function table (vtable) and a hidden vptr in each polymorphic object
Each class with virtual functions gets a vtable of function pointers; every object of that hierarchy carries a hidden vptr to its own class's vtable, which the call is indirected through at run time.
More OOP with C++ and Java questions
- 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…
- Why can a private method in a Java superclass never be overridden by a subclass, only unintentionally hidden (i.e. always statically bound)?
- cpp struct A { int x = 1; }; struct B : A {}; struct C : A {}; struct D : B, C {}; int main() { D d; // d.x ? } Without virtual…
- Why did Java and C# choose single inheritance of classes plus multiple inheritance of interfaces, instead of C++-style full multiple…
- Which statement correctly distinguishes an abstract class from an interface in Java (interfaces may have default/static/private methods…
- Within a single derived class's own constructor body, in what order are its direct base class subobjects (assuming no virtual inheritance)…