Bird
Raised Fist0
C++programming~20 mins

Method overriding in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Method Overriding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of overridden method call

What is the output of this C++ program?

C++
#include <iostream>
using namespace std;

class Base {
public:
    virtual void show() {
        cout << "Base show" << endl;
    }
};

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

int main() {
    Base* obj = new Derived();
    obj->show();
    delete obj;
    return 0;
}
ACompilation error
BBase show
CDerived show
DRuntime error
Attempts:
2 left
πŸ’‘ Hint

Think about which version of show() is called when using a base class pointer to a derived object.

❓ Predict Output
intermediate
2:00remaining
Output when base method is not virtual

What will this program print?

C++
#include <iostream>
using namespace std;

class Base {
public:
    void display() {
        cout << "Base display" << endl;
    }
};

class Derived : public Base {
public:
    void display() {
        cout << "Derived display" << endl;
    }
};

int main() {
    Base* ptr = new Derived();
    ptr->display();
    delete ptr;
    return 0;
}
ABase display
BDerived display
CCompilation error
DRuntime error
Attempts:
2 left
πŸ’‘ Hint

Check if the base class method is declared virtual or not.

πŸ”§ Debug
advanced
2:00remaining
Identify the error in method overriding

What error does this code produce?

C++
#include <iostream>
using namespace std;

class Parent {
public:
    virtual void func(int x) {
        cout << "Parent func: " << x << endl;
    }
};

class Child : public Parent {
public:
    void func(double x) override {
        cout << "Child func: " << x << endl;
    }
};

int main() {
    Child c;
    c.func(5);
    return 0;
}
AOutputs: Parent func: 5
BRuntime error: ambiguous call
COutputs: Child func: 5
DCompilation error: 'func' does not override any base class method
Attempts:
2 left
πŸ’‘ Hint

Check the parameter types in the overridden method and the use of override keyword.

🧠 Conceptual
advanced
2:00remaining
Effect of virtual destructor in base class

Why is it important to declare a destructor as virtual in a base class when using method overriding?

ATo ensure the derived class destructor is called when deleting through a base pointer
BTo allow overloading the destructor in derived classes
CTo make the destructor faster at runtime
DTo prevent compilation errors when deleting derived objects
Attempts:
2 left
πŸ’‘ Hint

Think about what happens when deleting an object through a base class pointer.

❓ Predict Output
expert
3:00remaining
Output of multiple overridden methods with different signatures

What is the output of this program?

C++
#include <iostream>
using namespace std;

class A {
public:
    virtual void f(int x) {
        cout << "A::f(int): " << x << endl;
    }
    virtual void f(double x) {
        cout << "A::f(double): " << x << endl;
    }
};

class B : public A {
public:
    void f(int x) override {
        cout << "B::f(int): " << x << endl;
    }
    void f(float x) {
        cout << "B::f(float): " << x << endl;
    }
};

int main() {
    B b;
    A* ptr = &b;
    ptr->f(10);      // call 1
    ptr->f(10.5);    // call 2
    b.f(10.5f);      // call 3
    return 0;
}
A
A::f(int): 10
A::f(double): 10.5
B::f(float): 10.5
B
B::f(int): 10
A::f(double): 10.5
B::f(float): 10.5
C
B::f(int): 10
B::f(float): 10.5
B::f(float): 10.5
DCompilation error due to ambiguous call
Attempts:
2 left
πŸ’‘ Hint

Remember which methods are virtual and which are overridden. Also consider implicit conversions and overload resolution.

Practice

(1/5)
1. What is the main purpose of method overriding in C++?
easy
A. To create a new method with a different name in the child class
B. To allow a child class to provide a specific implementation of a method already defined in its parent class
C. To prevent a method from being used in the child class
D. To copy all methods from the parent class to the child class without changes

Solution

  1. Step 1: Understand method overriding concept

    Method overriding allows a child class to change how a method works that it inherits from its parent.
  2. Step 2: Identify the correct purpose

    The child class provides its own version of the method, customizing behavior while keeping the same method name.
  3. Final Answer:

    To allow a child class to provide a specific implementation of a method already defined in its parent class -> Option B
  4. Quick Check:

    Method overriding = child changes parent method [OK]
Hint: Overriding means child changes parent's method behavior [OK]
Common Mistakes:
  • Confusing overriding with method overloading
  • Thinking overriding creates new method names
  • Believing overriding copies methods without changes
2. Which of the following is the correct way to declare a method for overriding in C++?
easy
A. override void display();
B. void display() override;
C. void virtual display();
D. virtual void display();

Solution

  1. Step 1: Recall syntax for virtual methods

    In C++, the parent class method must be marked with virtual to allow overriding.
  2. Step 2: Check each option

    virtual void display(); correctly uses virtual void display();. void display() override; uses override incorrectly in declaration, it is used in child method definition. Options C and D have wrong syntax.
  3. Final Answer:

    virtual void display(); -> Option D
  4. Quick Check:

    Parent method uses virtual keyword [OK]
Hint: Parent method must be virtual for overriding [OK]
Common Mistakes:
  • Placing override keyword in parent method declaration
  • Using wrong order of keywords
  • Omitting virtual keyword in parent
3. What will be the output of the following C++ code?
class Base {
public:
    virtual void show() { std::cout << "Base" << std::endl; }
};
class Derived : public Base {
public:
    void show() override { std::cout << "Derived" << std::endl; }
};
int main() {
    Base* ptr = new Derived();
    ptr->show();
    delete ptr;
    return 0;
}
medium
A. Derived
B. Runtime error
C. Compilation error
D. Base

Solution

  1. Step 1: Understand virtual function behavior

    The pointer is of type Base* but points to Derived object. Because show() is virtual, the Derived version is called.
  2. Step 2: Trace the call ptr->show()

    At runtime, the program calls Derived::show(), printing "Derived".
  3. Final Answer:

    Derived -> Option A
  4. Quick Check:

    Virtual method calls child's version [OK]
Hint: Virtual calls use actual object method, not pointer type [OK]
Common Mistakes:
  • Expecting Base output due to pointer type
  • Confusing override keyword effect
  • Ignoring virtual keyword
4. Identify the error in the following code snippet related to method overriding:
class Parent {
public:
    void greet() { std::cout << "Hello from Parent" << std::endl; }
};
class Child : public Parent {
public:
    void greet() override { std::cout << "Hello from Child" << std::endl; }
};
medium
A. Parent class method greet() should be marked virtual
B. Child class method greet() should be marked virtual
C. override keyword is not allowed in C++
D. No error, code is correct

Solution

  1. Step 1: Check parent method declaration

    For overriding to work properly, the parent method must be declared virtual. Here, it is not.
  2. Step 2: Analyze override keyword usage

    The child method uses override, which requires the parent method to be virtual. Since parent method is not virtual, this causes a compilation error.
  3. Final Answer:

    Parent class method greet() should be marked virtual -> Option A
  4. Quick Check:

    Parent method must be virtual for override [OK]
Hint: Parent method must be virtual if child uses override [OK]
Common Mistakes:
  • Thinking child method must be virtual
  • Ignoring need for virtual in parent
  • Believing override keyword is optional
5. Given the classes below, what will be the output when ptr->display(); is called?
class Animal {
public:
    virtual void display() { std::cout << "Animal" << std::endl; }
};
class Dog : public Animal {
public:
    void display() override { std::cout << "Dog" << std::endl; }
};
class Puppy : public Dog {
public:
    void display() override { std::cout << "Puppy" << std::endl; }
};

int main() {
    Animal* ptr = new Puppy();
    ptr->display();
    delete ptr;
    return 0;
}
hard
A. Animal
B. Dog
C. Puppy
D. Compilation error due to multiple overrides

Solution

  1. Step 1: Understand multi-level overriding

    Each class overrides display() with its own version. The pointer is of type Animal* but points to a Puppy object.
  2. Step 2: Determine which method is called

    Because display() is virtual, the most derived override (Puppy::display) is called at runtime.
  3. Final Answer:

    Puppy -> Option C
  4. Quick Check:

    Virtual calls deepest override in inheritance chain [OK]
Hint: Virtual calls deepest override in inheritance chain [OK]
Common Mistakes:
  • Expecting Dog or Animal output due to pointer type
  • Thinking multiple overrides cause errors
  • Ignoring virtual keyword effect
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard