Challenge - 5 Problems
Pure Virtual Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of a class with pure virtual function
What is the output of this C++ program?
C++
#include <iostream> using namespace std; class Shape { public: virtual void draw() = 0; }; class Circle : public Shape { public: void draw() override { cout << "Drawing Circle" << endl; } }; int main() { Circle c; c.draw(); return 0; }
Attempts:
2 left
π‘ Hint
Look at which class implements the pure virtual function and which object is created.
β Incorrect
The class Shape has a pure virtual function draw(), making it abstract. Circle overrides draw(), so creating Circle object and calling draw() prints "Drawing Circle".
β Predict Output
intermediate2:00remaining
Output when calling pure virtual function from base pointer
What will be the output of this program?
C++
#include <iostream> using namespace std; class Animal { public: virtual void sound() = 0; }; class Dog : public Animal { public: void sound() override { cout << "Woof" << endl; } }; int main() { Animal* a = new Dog(); a->sound(); delete a; return 0; }
Attempts:
2 left
π‘ Hint
Check which class implements the pure virtual function and how the pointer is used.
β Incorrect
The pointer of type Animal points to a Dog object. Dog overrides sound(), so calling sound() prints "Woof".
π§ Debug
advanced2:00remaining
Identify the error in this pure virtual destructor example
What error will this code produce when compiled?
C++
#include <iostream> using namespace std; class Base { public: virtual ~Base() = 0; }; Base::~Base() { cout << "Base destructor" << endl; } class Derived : public Base { public: ~Derived() { cout << "Derived destructor" << endl; } }; int main() { Base* b = new Derived(); delete b; return 0; }
Attempts:
2 left
π‘ Hint
Pure virtual destructors must have a body to allow proper cleanup.
β Incorrect
The pure virtual destructor Base::~Base() has a definition, so the code compiles. Deleting through base pointer calls Derived destructor then Base destructor, printing both lines.
π Syntax
advanced2:00remaining
Which option correctly declares a pure virtual function?
Which of the following is the correct syntax to declare a pure virtual function named `foo` returning int?
Attempts:
2 left
π‘ Hint
Remember the order: 'virtual' keyword before return type, and '= 0;' at the end.
β Incorrect
The correct syntax is 'virtual int foo() = 0;'. Other options have wrong order or invalid syntax.
π Application
expert2:00remaining
Number of objects instantiated with pure virtual functions
Given the following code, how many objects of type `Base` or its derived classes are instantiated in `main`?
C++
#include <iostream> using namespace std; class Base { public: virtual void display() = 0; }; class Derived1 : public Base { public: void display() override { cout << "Derived1" << endl; } }; class Derived2 : public Base { public: void display() override { cout << "Derived2" << endl; } }; int main() { Derived1 d1; Base* b = new Derived2(); d1.display(); b->display(); delete b; return 0; }
Attempts:
2 left
π‘ Hint
Count all objects created, including those created with new.
β Incorrect
One object of Derived1 is created on stack (d1), and one object of Derived2 is created on heap (b). Total 2 objects.