0
0
C++programming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Function Overriding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of overridden function call via base pointer
What is the output of this C++ code when calling the function through a base class pointer?
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* ptr = new Derived();
    ptr->show();
    delete ptr;
    return 0;
}
ABase show
BDerived show
CRuntime error
DCompilation error
Attempts:
2 left
πŸ’‘ Hint
Remember that virtual functions allow the derived class version to be called through a base pointer.
❓ Predict Output
intermediate
2:00remaining
Output when overriding function is not virtual
What will this C++ program print when calling the function through a base class pointer?
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;
}
ARuntime error
BCompilation error
CDerived display
DBase display
Attempts:
2 left
πŸ’‘ Hint
Check if the function is declared virtual in the base class.
πŸ”§ Debug
advanced
2:00remaining
Identify the error in function overriding
What error does this C++ code produce when compiled?
C++
#include <iostream>
using namespace std;

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

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

int main() {
    Derived d;
    d.func(5);
    return 0;
}
ACompilation error: 'func' marked override but does not override
BRuntime error: ambiguous call
COutput: Derived: 5
DOutput: Base: 5
Attempts:
2 left
πŸ’‘ Hint
Check if the function signature exactly matches the base class virtual function.
🧠 Conceptual
advanced
2:00remaining
Effect of 'final' on function overriding
What happens if a virtual function in a base class is marked with 'final' and a derived class tries to override it?
ADerived class function overrides successfully
BRuntime error when calling the function
CCompilation error: overriding a final function is not allowed
DThe base class function is hidden but still called
Attempts:
2 left
πŸ’‘ Hint
The 'final' keyword prevents further overriding.
❓ Predict Output
expert
3:00remaining
Output of multiple inheritance with overridden functions
What is the output of this C++ program?
C++
#include <iostream>
using namespace std;

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

class B : public A {
public:
    void show() override { cout << "B" << endl; }
};

class C : public A {
public:
    void show() override { cout << "C" << endl; }
};

class D : public B, public C {
public:
    void show() override { cout << "D" << endl; }
};

int main() {
    D obj;
    obj.show();
    B* bptr = &obj;
    bptr->show();
    C* cptr = &obj;
    cptr->show();
    return 0;
}
A
D
B
C
B
D
D
D
CCompilation error due to ambiguity
D
D
A
A
Attempts:
2 left
πŸ’‘ Hint
Each base pointer calls its own overridden version; D overrides show() for itself.