Challenge - 5 Problems
Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of Single Inheritance Example
What is the output of this C++ code demonstrating single inheritance?
C++
#include <iostream> using namespace std; class Animal { public: void sound() { cout << "Animal sound" << endl; } }; class Dog : public Animal { public: void sound() { cout << "Dog barks" << endl; } }; int main() { Dog d; d.sound(); return 0; }
Attempts:
2 left
π‘ Hint
Look at which class's sound() method is called by the Dog object.
β Incorrect
The Dog class overrides the sound() method of Animal. When calling d.sound(), Dog's version runs, printing 'Dog barks'.
β Predict Output
intermediate2:00remaining
Output of Multiple Inheritance Example
What is the output of this C++ code demonstrating multiple inheritance?
C++
#include <iostream> using namespace std; class Printer { public: void print() { cout << "Printing document" << endl; } }; class Scanner { public: void scan() { cout << "Scanning document" << endl; } }; class Copier : public Printer, public Scanner { }; int main() { Copier c; c.print(); c.scan(); return 0; }
Attempts:
2 left
π‘ Hint
Copier inherits both Printer and Scanner, so it can call both methods.
β Incorrect
Copier inherits print() from Printer and scan() from Scanner. Both methods are called successfully, printing both lines.
β Predict Output
advanced2:00remaining
Output of Multilevel Inheritance with Overriding
What is the output of this C++ code demonstrating multilevel inheritance and method overriding?
C++
#include <iostream> using namespace std; class Vehicle { public: virtual void start() { cout << "Vehicle starts" << endl; } }; class Car : public Vehicle { public: void start() override { cout << "Car starts" << endl; } }; class SportsCar : public Car { public: void start() override { cout << "SportsCar starts quickly" << endl; } }; int main() { Vehicle* v = new SportsCar(); v->start(); delete v; return 0; }
Attempts:
2 left
π‘ Hint
Virtual functions allow the most derived class's method to run through base class pointer.
β Incorrect
Because start() is virtual, calling v->start() where v points to SportsCar runs SportsCar's start(), printing 'SportsCar starts quickly'.
β Predict Output
advanced2:00remaining
Output of Hierarchical Inheritance with Method Calls
What is the output of this C++ code demonstrating hierarchical inheritance?
C++
#include <iostream> using namespace std; class Shape { public: void draw() { cout << "Drawing shape" << endl; } }; class Circle : public Shape { public: void draw() { cout << "Drawing circle" << endl; } }; class Square : public Shape { public: void draw() { cout << "Drawing square" << endl; } }; int main() { Circle c; Square s; c.draw(); s.draw(); return 0; }
Attempts:
2 left
π‘ Hint
Each derived class has its own draw() method that hides the base class method.
β Incorrect
Circle and Square override draw(). Calling draw() on their objects prints their own messages, not the base class's.
π§ Conceptual
expert3:00remaining
Diamond Problem in Multiple Inheritance
In C++, what problem arises in multiple inheritance when two base classes inherit from the same grandparent class, and a derived class inherits from both base classes? What is the typical solution?
Attempts:
2 left
π‘ Hint
Think about how many copies of the grandparent class exist in the derived class.
β Incorrect
The diamond problem causes ambiguity because the derived class inherits two copies of the grandparent class. Virtual inheritance ensures only one shared copy exists, resolving ambiguity.