0
0
C++programming~20 mins

Types of inheritance in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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;
}
ADog barks\nAnimal sound
BDog barks
CCompilation error
DAnimal sound
Attempts:
2 left
πŸ’‘ Hint
Look at which class's sound() method is called by the Dog object.
❓ Predict Output
intermediate
2: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;
}
AScanning document
BCompilation error due to ambiguity
CPrinting document
DPrinting document\nScanning document
Attempts:
2 left
πŸ’‘ Hint
Copier inherits both Printer and Scanner, so it can call both methods.
❓ Predict Output
advanced
2: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;
}
AVehicle starts
BCar starts
CSportsCar starts quickly
DCompilation error due to override
Attempts:
2 left
πŸ’‘ Hint
Virtual functions allow the most derived class's method to run through base class pointer.
❓ Predict Output
advanced
2: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;
}
ADrawing circle\nDrawing square
BDrawing shape\nDrawing shape
CDrawing circle\nDrawing shape
DCompilation error due to draw() redefinition
Attempts:
2 left
πŸ’‘ Hint
Each derived class has its own draw() method that hides the base class method.
🧠 Conceptual
expert
3: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?
AAmbiguity in accessing grandparent members; solved by virtual inheritance
BCompilation error due to duplicate class names; solved by renaming classes
CRuntime error due to conflicting constructors; solved by deleting one base class
DNo problem arises; multiple inheritance always works without issues
Attempts:
2 left
πŸ’‘ Hint
Think about how many copies of the grandparent class exist in the derived class.