Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using private inheritance hides base class public members.
Forgetting to specify inheritance type defaults to private in classes.
β Incorrect
In C++, public inheritance means the public members of the base class remain public in the derived class.
2fill in blank
mediumComplete 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'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using different inheritance types for each base class.
Using private inheritance which hides base class members.
β Incorrect
Multiple inheritance uses the same access specifier for each base class; public inheritance keeps members accessible.
3fill in blank
hardFix 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'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using private inheritance which restricts access to base class members.
Omitting inheritance specifier defaults to private in classes.
β Incorrect
Public inheritance allows the derived class to access public members of the base class.
4fill in blank
hardFill 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'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using private inheritance which hides base class members.
Mixing different inheritance types for the two classes.
β Incorrect
Both classes should inherit publicly from Person to keep Person's public members accessible.
5fill in blank
hardFill 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'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using private inheritance which hides base class members.
Mixing inheritance types causing access issues.
β Incorrect
Public inheritance is used throughout to keep all base class members accessible in derived classes.