Back to track
#117 Medium Object-Oriented C++

Function Overriding

Open the editor

Problem

Override a base method and call it via derived object.

Input

No input.

Output

Exactly what the described program prints.

Hints

  1. 1

    Use same signature

  2. 2

    Add override keyword for virtual

Worked solution

Reference solution

One correct implementation. Alternative approaches that satisfy the constraints are equally valid — the reviewer judges behaviour, not style.

#include <iostream>
using namespace std;

class Base {
public:
  virtual void show() const { cout << "Base\n"; }
};

class Derived : public Base {
public:
  void show() const override { cout << "Derived\n"; }
};

int main() {
  Derived d;
  d.show();
  return 0;
}

© 2026 ChitChat