Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote