Suppose that a C++ class D is derived from a base class B. Class B has a public
ID: 3901190 • Letter: S
Question
Suppose that a C++ class D is derived from a base class B. Class B has a public member function Func() that is declared to be virtual, and class D redefines its own version of Func(). At execution time, suppose that a D object is passed to the following function:
void DoSomething( B x )
{
x.Func();
}
Within the DoSomething function, whose version of Func() is called?
A) B's version
B) D's version
C) Both B’s version and D’s version
D) None of these answers is correct.
Explanation / Answer
As the function Func is defined as virtual in the base class B, the invocation of the method shall decide which object's method needs to be invoked (late aka dynamic binding).
Answer: D's version (Option B). As an object of Type D is being passed, it's method shall be invoked.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.