Challenge - 5 Problems
Virtual Functions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of virtual function call through base pointer
What is the output of this C++ code?
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 how virtual functions behave when called through a base class pointer.
β Incorrect
Because show() is virtual, the call ptr->show() invokes Derived's version, printing "Derived show".
β Predict Output
intermediate2:00remaining
Output when virtual function is not overridden
What will this program print?
C++
#include <iostream> using namespace std; class Animal { public: virtual void speak() { cout << "Animal speaks" << endl; } }; class Dog : public Animal { // No override of speak() }; int main() { Animal* a = new Dog(); a->speak(); delete a; return 0; }
Attempts:
2 left
π‘ Hint
If a derived class does not override a virtual function, which version is called?
β Incorrect
Since Dog does not override speak(), the base class Animal's speak() is called, printing "Animal speaks".
π§ Debug
advanced2:00remaining
Identify the error in virtual destructor usage
What error will this code produce when compiled or run?
C++
#include <iostream> using namespace std; class Base { public: ~Base() { cout << "Base destructor" << endl; } }; class Derived : public Base { public: ~Derived() { cout << "Derived destructor" << endl; } }; int main() { Base* ptr = new Derived(); delete ptr; return 0; }
Attempts:
2 left
π‘ Hint
Think about what happens if the base class destructor is not virtual and you delete a derived object through a base pointer.
β Incorrect
Because Base's destructor is not virtual, deleting a Derived object through a Base pointer calls only Base's destructor, skipping Derived's destructor, which can cause resource leaks.
π Syntax
advanced2:00remaining
Which option causes a compilation error?
Given the following class, which option will cause a compilation error?
C++
class Shape { public: virtual void draw() = 0; };
Attempts:
2 left
π‘ Hint
What happens if a class inherits a pure virtual function but does not override it?
β Incorrect
Square does not override the pure virtual function draw(), so it remains abstract and cannot be instantiated, causing a compilation error if instantiated.
π Application
expert2:00remaining
Determine the number of virtual function calls
How many times is the virtual function 'action' called in this program?
C++
#include <iostream> using namespace std; class A { public: virtual void action() { cout << "A"; } }; class B : public A { public: void action() override { cout << "B"; } }; class C : public B { public: void action() override { cout << "C"; } }; int main() { A* arr[3]; arr[0] = new A(); arr[1] = new B(); arr[2] = new C(); for (int i = 0; i < 3; ++i) { arr[i]->action(); } for (int i = 0; i < 3; ++i) { delete arr[i]; } return 0; }
Attempts:
2 left
π‘ Hint
Count how many times the loop calls the virtual function.
β Incorrect
The loop calls action() exactly 3 times, once for each pointer in the array. Each call is virtual and calls the correct overridden version.