Challenge - 5 Problems
Function Overriding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediateOutput 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; }
Attempts:
2 left
π‘ Hint
Remember that virtual functions allow the derived class version to be called through a base pointer.
β Incorrect
Because show() is declared virtual in Base and overridden in Derived, calling show() through a Base pointer to a Derived object calls Derived's version.
β Predict Output
intermediateOutput 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; }
Attempts:
2 left
π‘ Hint
Check if the function is declared virtual in the base class.
β Incorrect
Since display() is not virtual, the function called depends on the pointer type, not the object type. The base class version is called.
π§ Debug
advancedIdentify 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; }
Attempts:
2 left
π‘ Hint
Check if the function signature exactly matches the base class virtual function.
β Incorrect
The Derived class's func(double) does not match Base's func(int), so override keyword causes a compilation error.
π§ Conceptual
advancedEffect 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?
Attempts:
2 left
π‘ Hint
The 'final' keyword prevents further overriding.
β Incorrect
Marking a virtual function as final in the base class forbids derived classes from overriding it, causing a compilation error if attempted.
β Predict Output
expertOutput 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; }
Attempts:
2 left
π‘ Hint
Each base pointer calls its own overridden version; D overrides show() for itself.
β Incorrect
Class D overrides show(), so obj.show() prints D. bptr points to B part of obj, calling B::show(). cptr points to C part, calling C::show().
