0
0
C++programming~10 mins

Types of inheritance in C++ - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a class Dog that inherits publicly from class Animal.

C++
class Dog : [1] Animal {
public:
    void bark() {
        std::cout << "Woof!" << std::endl;
    }
};
Drag options to blanks, or click blank then click option'
Apublic
Bprivate
Cprotected
Dvirtual
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using private inheritance hides base class public members.
Forgetting to specify inheritance type defaults to private in classes.
2fill in blank
medium

Complete the code to show multiple inheritance where class SmartPhone inherits from both Phone and Camera.

C++
class SmartPhone : [1] Phone, [2] Camera {
public:
    void use() {
        call();
        takePhoto();
    }
};
Drag options to blanks, or click blank then click option'
Apublic
Bprivate
Cprotected
Dvirtual
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using different inheritance types for each base class.
Using private inheritance which hides base class members.
3fill in blank
hard

Fix the error in the code to correctly implement multilevel inheritance where Car inherits from Vehicle and ElectricCar inherits from Car.

C++
class Vehicle {
public:
    void start() { std::cout << "Vehicle started" << std::endl; }
};

class Car : [1] Vehicle {
public:
    void drive() { std::cout << "Car driving" << std::endl; }
};

class ElectricCar : public Car {
public:
    void charge() { std::cout << "Charging battery" << std::endl; }
};
Drag options to blanks, or click blank then click option'
Apublic
Bvirtual
Cprotected
Dprivate
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using private inheritance which restricts access to base class members.
Omitting inheritance specifier defaults to private in classes.
4fill in blank
hard

Fill both blanks to create a hierarchical inheritance where Teacher and Student both inherit from Person.

C++
class Teacher : [1] Person {};
class Student : [2] Person {};
Drag options to blanks, or click blank then click option'
Apublic
Bprivate
Cprotected
Dvirtual
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using private inheritance which hides base class members.
Mixing different inheritance types for the two classes.
5fill in blank
hard

Fill all three blanks to implement hybrid inheritance combining multiple and multilevel inheritance.

C++
class A {
public:
    void funcA() { std::cout << "A" << std::endl; }
};

class B : [1] A {
public:
    void funcB() { std::cout << "B" << std::endl; }
};

class C : [2] A {
public:
    void funcC() { std::cout << "C" << std::endl; }
};

class D : [3] B, public C {
public:
    void funcD() { std::cout << "D" << std::endl; }
};
Drag options to blanks, or click blank then click option'
Apublic
Bprivate
Cprotected
Dvirtual
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using private inheritance which hides base class members.
Mixing inheritance types causing access issues.