Question:

What is the output of the following C++ code?

# include <iostream.h>
class Base {
public:
 void show() {cout << "Base"; }
};
class Derived : public Base {
public:
  void show() {cout << "Derived"; }
};
int main() {
  Base* b = new Derived();
  b->show();
}

Show Hint

Is show() declared virtual? Without virtual, a base pointer calls the base version.
Updated On: Jul 2, 2026
  • Derived
  • Base
  • Compilation error
  • Runtime error
Show Solution
collegedunia
Verified By Collegedunia

The Correct Option is B

Solution and Explanation

Step 1: Look at show() in the Base class. It is a plain member function. It is not marked virtual.

Step 2: The call is made through a Base pointer:
 Base* b = new Derived();
  b->show();

Step 3: For a non virtual function, C++ uses static binding. The function to call is chosen from the static type of the pointer, which is Base, not from the actual object type Derived.

Step 4: So \( b\text{-}\!>show() \) resolves to Base::show at compile time and prints Base. The Derived version is ignored because there is no virtual dispatch.

Answer: option B.
Was this answer helpful?
0
0