0
0
C++programming~20 mins

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

Choose your learning style9 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.