0
0
C++programming~30 mins

Types of inheritance in C++ - Mini Project: Build & Apply

Choose your learning style9 modes available
Types of inheritance
πŸ“– 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++
Need a 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++
Need a 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++
Need a 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++
Need a hint?

Create objects of each class and call their show() method in the order given. Use std::cout output to verify.