๐ Scenario: Imagine you are organizing a simple family tree using classes in C++. You want to show how different types of inheritance work by creating parent and child classes.
๐ฏ Goal: You will create a base class and derived classes to demonstrate single, multiple, and multilevel inheritance in C++. You will then print messages to show which class's method is called.
๐ What You'll Learn
Create a base class called Grandparent with a method void show() const that prints "I am the grandparent."
Create a class called Parent that inherits publicly from Grandparent and overrides show() to print "I am the parent."
Create a class called Child that inherits publicly from Parent and overrides show() to print "I am the child."
Create a class called Uncle with a method void show() const that prints "I am the uncle."
Create a class called Cousin that inherits publicly from both Uncle and Parent and overrides show() to print "I am the cousin."
Create objects of each class and call their show() method to display the messages.
๐ก Why This Matters
๐ Real World
Inheritance helps organize related classes in software, like family trees, employee hierarchies, or vehicle types.
๐ผ Career
Understanding inheritance is essential for object-oriented programming jobs, enabling you to write reusable and organized code.
Progress0 / 4 steps
1
Create the base class Grandparent
Create a class called Grandparent with a public method void show() const that prints exactly "I am the grandparent."
C++
Hint
Use class Grandparent and define void show() const inside it. Use std::cout to print the message.
2
Create Parent and Child classes with single and multilevel inheritance
Add a class called Parent that publicly inherits from Grandparent and overrides void show() const to print "I am the parent." Then add a class called Child that publicly inherits from Parent and overrides void show() const to print "I am the child."
C++
Hint
Use class Parent : public Grandparent and override show(). Then create class Child : public Parent and override show() similarly.
3
Create Uncle and Cousin classes with multiple inheritance
Create a class called Uncle with a public method void show() const that prints "I am the uncle." Then create a class called Cousin that publicly inherits from both Uncle and Parent and overrides void show() const to print "I am the cousin."
C++
Hint
Define class Uncle with show(). Then define class Cousin : public Uncle, public Parent and override show().
4
Create objects and call show() methods
Create objects called gp of type Grandparent, p of type Parent, c of type Child, u of type Uncle, and co of type Cousin. Call the show() method on each object in this order: gp, p, c, u, co. Each call should print the respective message on its own line.
C++
Hint
Create objects of each class and call their show() method in the order given. Use std::cout output to verify.
Practice
(1/5)
1. Which type of inheritance involves a single derived class inheriting from one base class in C++?
easy
A. Hierarchical inheritance
B. Multiple inheritance
C. Multilevel inheritance
D. Single inheritance
Solution
Step 1: Understand inheritance types
Single inheritance means one class inherits from exactly one base class.
Step 2: Match definition to options
Only Single inheritance describes a single derived class inheriting from one base class.
Final Answer:
Single inheritance -> Option D
Quick Check:
One derived, one base = Single inheritance [OK]
Hint: One derived from one base is single inheritance [OK]
Common Mistakes:
Confusing multiple inheritance with single inheritance
Thinking multilevel means one base class only
Mixing hierarchical with single inheritance
2. Which of the following is the correct syntax for multiple inheritance in C++?
easy
A. class Derived : public Base1 & Base2 {};
B. class Derived : public Base1, public Base2 {};
C. class Derived : Base1 Base2 {};
D. class Derived inherits Base1, Base2 {};
Solution
Step 1: Recall multiple inheritance syntax
In C++, multiple inheritance uses commas to separate base classes with access specifiers.
Step 2: Check each option
class Derived : public Base1, public Base2 {}; uses correct syntax: 'class Derived : public Base1, public Base2 {};'. Others have syntax errors.
Final Answer:
class Derived : public Base1, public Base2 {}; -> Option B
Quick Check:
Comma separates base classes with access specifiers [OK]
Hint: Use commas and access specifiers for multiple inheritance [OK]
Common Mistakes:
Omitting commas between base classes
Using '&' instead of commas
Writing 'inherits' keyword which doesn't exist in C++
3. What will be the output of the following C++ code?
class A { public: void show() { std::cout << "A"; } };
class B : public A { public: void show() { std::cout << "B"; } };
class C : public B {};
int main() {
C obj;
obj.show();
return 0;
}
medium
A. B
B. A
C. C
D. Compilation error
Solution
Step 1: Understand inheritance chain
Class C inherits from B, which inherits from A. B overrides show() method.
Step 2: Determine which show() is called
Calling obj.show() on C calls B's show() because C inherits B's version and does not override it.
Final Answer:
B -> Option A
Quick Check:
Derived class method called if overridden [OK]
Hint: Derived class method overrides base method unless not overridden [OK]
Common Mistakes:
Assuming base class method runs instead of overridden
Expecting 'C' output though C has no show()
Thinking code causes compilation error
4. Identify the error in this C++ code snippet involving inheritance:
class Base { public: int x; };
class Derived : Base {};
int main() {
Derived d;
d.x = 1;
return 0;
}
medium
A. Main function missing return type
B. Base class must be declared inside Derived
C. Missing access specifier before Base in Derived class
D. Derived class cannot be empty
Solution
Step 1: Check inheritance syntax
In C++, inheritance requires an access specifier like public, private, or protected before base class name.
Step 2: Identify the error
Derived : Base {} lacks access specifier; should be Derived : public Base {}.
Final Answer:
Missing access specifier before Base in Derived class -> Option C
Quick Check:
Inheritance needs access specifier [OK]
Hint: Always specify public/private/protected before base class [OK]
Common Mistakes:
Omitting access specifier in inheritance
Thinking empty class body is error
Confusing return type in main function
5. Given the following classes:
class Animal { public: void eat() { std::cout << "Eating"; } };
class Mammal : public Animal {};
class Bird : public Animal {};
class Bat : public Mammal, public Bird {};
int main() {
Bat b;
b.eat();
return 0;
}
What problem arises and how to fix it?
hard
A. Ambiguity error due to multiple inheritance; fix with virtual inheritance
B. No problem; code runs fine
C. Syntax error in Bat class declaration
D. eat() method must be overridden in Bat
Solution
Step 1: Analyze inheritance structure
Bat inherits Mammal and Bird, both inherit Animal, causing two Animal copies in Bat.
Step 2: Identify ambiguity
Calling b.eat() is ambiguous because Bat has two Animal parts; compiler error occurs.
Step 3: Fix with virtual inheritance
Use virtual inheritance for Mammal and Bird from Animal to share one Animal base.
Final Answer:
Ambiguity error due to multiple inheritance; fix with virtual inheritance -> Option A
Quick Check:
Diamond problem fixed by virtual inheritance [OK]
Hint: Use virtual inheritance to solve diamond ambiguity [OK]
Common Mistakes:
Ignoring diamond problem ambiguity
Thinking code compiles without error
Assuming method override fixes ambiguity
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.