Types of Inheritance in C++: An Easy Guide.

Welcome to the exciting world of C++! Today, we look into the engaging concept of “Types of Inheritance.” Inheritance allows us to create new classes based on existing ones, inheriting their properties and functionalities. But did you know that there are different types of inheritance? In this article, we’ll explore why inheritance is essential in object-oriented programming, the various types of inheritance like single, multiple, multilevel, and hierarchical inheritance, and how we can use them to build powerful and flexible code structures. Let’s dive in and unlock the full potential of inheritance in C++!
Why Do We Need Different Types of Inheritance in C++?
In C++, various types of inheritance are used to add flexibility and make coding more efficient in object-oriented programming. These different inheritance types, such as single, multiple, multilevel, and hierarchical, help us build complex class structures and create meaningful connections between classes. This way, we can inherit attributes and functions from existing classes, which makes our code neater and avoids repetition.
Each kind of inheritance is like a tool that suits different situations, allowing programmers to pick the best method for their specific tasks. Learning and making use of these types of inheritance empower us to create programs that are versatile and well-organized. This makes our coding work smoother and more effective.
What Are the Types of Inheritance in C++?
In C++, there are five types of inheritance: single, multiple, multilevel, hierarchical, and hybrid inheritance. Each type of inheritance shows how classes are connected to each other and what they can inherit from each other. It’s like different ways of forming relationships between classes to make our code more organized and efficient.
Each type of inheritance has its unique purpose and benefits, giving us various options to choose from when designing our programs. Understanding these inheritance types helps us create better and more versatile programs in C++.
- Single Inheritance: Single inheritance involves inheriting from only one base class. It is useful when you want to create a specialized class that extends the properties and behaviors of a single existing class. For example, you might have a base class “Vehicle” and a derived class “Car” that inherits from it. This keeps the code simple and maintains a clear hierarchy.
- Multiple Inheritance: Multiple inheritance allows a class to inherit from more than one base class. It is useful when you need a class to have features from multiple sources. For instance, you might have a class “Teacher” inheriting from both “Person” and “Subject” classes. This helps in code reuse and represents complex relationships.
- Multilevel Inheritance: Multilevel inheritance involves creating a chain of inheritance, where a class inherits from another derived class. It helps in building hierarchical structures and creating specialized classes. For example, “Grandchild” inheriting from “Child” which further inherits from “Parent.”
- Hierarchical Inheritance: Hierarchical inheritance is when multiple classes inherit from a single base class. This is useful when different classes share common properties or behaviors. For instance, “Cat,” “Dog,” and “Horse” inheriting from “Animal” class.
- Hybrid Inheritance: Hybrid inheritance combines different types of inheritance. It is useful when complex relationships need to be represented. For example, a class inheriting from multiple classes that have their own derived classes.

A Problem to Solve
Problem Statement:
You are developing a school management system. In this system, you are required to model different roles like “Person”, “Teacher”, “Student”, and “TeachingAssistant”. A Teaching Assistant is both a Teacher and a Student. You must use different types of inheritance to design these classes:
- Person Class: Base class with basic attributes like name and age.
- Teacher Class: Derived from Person, with additional attributes like subject and salary.
- Student Class: Derived from Person, with additional attributes like grade and marks.
- TeachingAssistant Class: Derived from both Teacher and Student. A Teaching Assistant has both student and teacher properties.
Use the following types of inheritance:
- Single Inheritance: Use this for Teacher and Student classes deriving from Person.
- Multiple Inheritance: Use this for TeachingAssistant class deriving from both Teacher and Student.
Write a C++ program to demonstrate this hierarchy. Implement constructors for initialization, and member functions to display details for each class.
Additional Requirements:
- Person Class: Should have member functions to set and display name and age.
- Teacher Class: Should have member functions to set and display subject and salary.
- Student Class: Should have member functions to set and display grade and marks.
- TeachingAssistant Class: Should have member functions to set and display all the properties.
Create objects for each class and display their details.
Hints:
- Make sure to properly initialize the base and derived classes using constructors.
- Use the ‘
public
‘ keyword for inheritance to maintain the accessibility of the members. - Take care of the constructor calling sequence in multiple inheritance.
- Be cautious about any potential ambiguities, and make sure to define clear paths for the derived classes.
Expected Output:
The output should display the details of the Person, Teacher, Student, and TeachingAssistant objects, such as name, age, subject, salary, grade, and marks.
Example 1: Single Inheritance
#include<iostream>
using namespace std;
// Base class
class Animal {
public:
void eat() {
cout << "The animal eats." << endl;
}
};
// Derived class
class Dog : public Animal {
public:
void bark() {
cout << "The dog barks." << endl;
}
};
int main() {
Dog dog1;
dog1.eat();
dog1.bark();
return 0;
}
Output:
The animal eats.
The dog barks.
Explanation:
- The code defines a base class called Animal and a derived class called Dog.
- The Animal class has a function named
eat()
that outputs “The animal eats.” when called. - The Dog class inherits from the Animal class using the
public
access specifier. - The Dog class adds its own function named
bark()
that outputs “The dog barks.” when called. - In the main function, an object of the Dog class named
dog1
is created, representing a dog. - The
eat()
andbark()
functions are called on thedog1
object, resulting in the respective outputs.
Example 2: Multiple Inheritance
#include<iostream>
using namespace std;
// Base class 1
class Fly {
public:
void canFly() {
cout << "Can fly." << endl;
}
};
// Base class 2
class Swim {
public:
void canSwim() {
cout << "Can swim." << endl;
}
};
// Derived class
class Duck : public Fly, public Swim {
public:
void quack() {
cout << "The duck quacks." << endl;
}
};
int main() {
Duck duck1;
duck1.canFly();
duck1.canSwim();
duck1.quack();
return 0;
}
Output:
Can fly.
Can swim.
The duck quacks.
Explanation:
- The code defines three classes: Fly, Swim, and Duck.
- The Fly class represents a base class that has a function named
canFly()
that outputs “Can fly.” when called. - The Swim class represents another base class that has a function named
canSwim()
that outputs “Can swim.” when called. - The Duck class is the derived class that inherits from both the Fly and Swim classes using multiple inheritance.
- The Duck class also adds its own function named
quack()
that outputs “The duck quacks.” when called. - In the main function, an object of the Duck class named
duck1
is created. - The
canFly()
,canSwim()
, andquack()
functions are called on theduck1
object, resulting in the respective outputs.
Example 3: Multilevel Inheritance
#include<iostream>
using namespace std;
// Base class
class Animal {
public:
void eat() {
cout << "The animal eats." << endl;
}
};
// Intermediate class
class Bird : public Animal {
public:
void fly() {
cout << "The bird flies." << endl;
}
};
// Derived class
class Eagle : public Bird {
public:
void hunt() {
cout << "The eagle hunts." << endl;
}
};
int main() {
Eagle eagle1;
eagle1.eat();
eagle1.fly();
eagle1.hunt();
return 0;
}
Output:
The animal eats.
The bird flies.
The eagle hunts.
Explanation:
- The code defines a class hierarchy with three classes: Animal, Bird, and Eagle.
- The Animal class is the base class, which has a function named
eat()
that outputs “The animal eats.” when called. - The Bird class is an intermediate class that inherits from Animal and adds a function named
fly()
that outputs “The bird flies.” when called. - The Eagle class is the derived class that inherits from Bird and adds a function named
hunt()
that outputs “The eagle hunts.” when called. - In the main function, an object of the Eagle class named
eagle1
is created. - The
eat()
,fly()
, andhunt()
functions are called on theeagle1
object, resulting in the respective outputs.
Example 4: Hierarchical Inheritance
Hierarchical inheritance is a type of inheritance where multiple classes are derived from a single base class. Here’s a code example of hierarchical inheritance in C++ with an explanation of the code and its output:
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void eat() {
cout << "Animal is eating" << endl;
}
};
// Derived class 1
class Dog : public Animal {
public:
void bark() {
cout << "Dog is barking" << endl;
}
};
// Derived class 2
class Cat : public Animal {
public:
void meow() {
cout << "Cat is meowing" << endl;
}
};
int main() {
Dog dog;
Cat cat;
cout << "Dog:" << endl;
dog.eat();
dog.bark();
cout << "\nCat:" << endl;
cat.eat();
cat.meow();
return 0;
}
Explanation:
- We have a base class called
Animal
with a member functioneat()
. - The classes
Dog
andCat
are derived from theAnimal
class. - Both
Dog
andCat
have their own specific functions (bark()
andmeow()
respectively).
Output:
Dog:
Animal is eating
Dog is barking
Cat:
Animal is eating
Cat is meowing
Example 5: Hybrid Inheritance
#include <iostream>
using namespace std;
class Base {
public:
void show() {
cout << "This is the Base class." << endl;
}
};
class Derived1 : public Base {
public:
void showDerived1() {
cout << "This is Derived1 class." << endl;
}
};
class Derived2 : public Base {
public:
void showDerived2() {
cout << "This is Derived2 class." << endl;
}
};
class Derived3 : public Derived1, public Derived2 {
public:
void showDerived3() {
cout << "This is Derived3 class." << endl;
}
};
int main() {
Derived3 obj;
obj.show(); // Accessing show() from Base class
obj.showDerived1(); // Accessing showDerived1() from Derived1 class
obj.showDerived2(); // Accessing showDerived2() from Derived2 class
obj.showDerived3(); // Accessing showDerived3() from Derived3 class
return 0;
}
Output:
This is the Base class.
This is Derived1 class.
This is Derived2 class.
This is Derived3 class.
Explanation:
- Base & Derived Classes: A base class “Base” and derived classes “Derived1” and “Derived2” are created.
- Hybrid Inheritance: Class “Derived3” inherits from both “Derived1” and “Derived2,” forming a hybrid inheritance.
- Inherited Functions: Base class has “show()” function, while derived classes have unique functions like “showDerived1(),” “showDerived2(),” and “showDerived3().”
- Accessible Features: All functions from base and derived classes are accessible in the hybrid inheritance structure.
- Output: The program’s execution demonstrates that functions from all classes can be used in hybrid inheritance.
The Pros and Cons of Using Different Types of Inheritance
Types of Inheritance | Pros | Cons |
---|---|---|
Single Inheritance | – Simple and easy to understand code. | – Limited reusability of code if multiple inheritance is needed. |
– Encourages a hierarchical and organized class structure. | – Can lead to deep inheritance hierarchies, making maintenance difficult. | |
Multiple Inheritance | – Allows classes to inherit properties from multiple base classes. | – Increases complexity, making it harder to understand the code. |
– Promotes code reusability and modularity. | – Can lead to the “diamond problem,” causing ambiguity and confusion in the code. | |
Multilevel Inheritance | – Hierarchical and organized class structure. | – Can become complex if there are too many levels of inheritance. |
– Promotes code reusability by inheriting properties step by step. | – Maintenance can become challenging if the base class changes. | |
Hierarchical Inheritance | – Promotes code reusability by sharing properties among sibling classes. | – May lead to code duplication if properties are shared among sibling classes. |
– Easy to understand and manage class relationships. | – Can lead to a large number of classes if there are many levels in the hierarchy. | |
Hybrid Inheritance | – Provides flexibility to create complex class relationships. | – Increases code complexity and may result in ambiguity. |
– Allows code reusability and promotes modularity. | – Difficult to understand and debug, especially if used improperly. |
Key Takeaways
- Versatile Class Relationships: C++ offers various inheritance types, allowing you to establish diverse relationships between classes.
- Enhanced Program Efficiency: Mastering inheritance helps create more advanced and streamlined programs.
- Improved Code Maintenance: Understanding inheritance types leads to code that’s easier to maintain and comprehend.
- Tailored Code Organization: Choosing the right inheritance type aligns with your program’s needs, enhancing organization and reusability.
- Higher Software Quality: Selecting appropriate inheritance promotes better quality, scalability, and overall effectiveness of your software.
Conclusion
In conclusion, comprehending the various inheritance types in C++ is a potent asset for programmers. Mastering their utilization enhances your ability to craft optimized and superior programs. Through consistent practice, you’ll swiftly acquire proficiency in employing diverse inheritance strategies, thereby strengthening your programming skills and capabilities.
FAQs
- What are the types of inheritance in C++? In C++, there are five types of inheritance: single, multiple, multilevel, hierarchical, and hybrid inheritance. Each type of inheritance represents a different relationship between classes.
- Why do we use different types of inheritance in C++? We use different types of inheritance in C++ to create complex relationships between classes. They allow us to create new classes that reuse, extend, and modify the behavior defined in other classes.
- How do we use different types of inheritance in C++? We use different types of inheritance in C++ by creating derived classes from base classes. The derived class inherits all the properties and methods of the base class.
- Can use different types of inheritance make code more confusing? Yes, if you use different types of inheritance incorrectly, it can lead to problems. It’s important to understand how different types of inheritance work and when to use them.
- What are some examples of using different types of inheritance in C++? Some examples include using single inheritance to create a Dog class from an Animal class, multiple inheritance to create a Duck class from Fly and Swim classes, or multilevel inheritance to create an Eagle class from a Bird class which in turn inherits from an Animal class.