Module 17: Inheritance in C++
Inheritance is one of the four fundamental concepts of Object-Oriented Programming (OOP). It allows one class (called the derived class) to inherit the properties and behaviors of another class (called the base class).
Inheritance promotes code reusability, extensibility, and maintainability.
Benefits of Inheritance
- Reuse existing code.
- Reduce code duplication.
- Improve maintainability.
- Support code extensibility.
- Enable polymorphism.
1. Single Inheritance
In single inheritance, one derived class inherits from one base class.
Syntax
1class Derived : public Base 2{ 3};
Example
1#include <iostream> 2 3using namespace std; 4 5class Animal 6{ 7public: 8 void eat() 9 { 10 cout << "Animal is Eating" << endl; 11 } 12}; 13 14class Dog : public Animal 15{ 16public: 17 void bark() 18 { 19 cout << "Dog is Barking"; 20 } 21}; 22 23int main() 24{ 25 Dog dog; 26 27 dog.eat(); 28 dog.bark(); 29 30 return 0; 31}
Output
1Animal is Eating 2Dog is Barking
2. Multiple Inheritance
A class inherits from more than one base class.
Syntax
1class Derived : public Base1, public Base2 2{ 3};
Example
1#include <iostream> 2 3using namespace std; 4 5class Father 6{ 7public: 8 void work() 9 { 10 cout << "Working" << endl; 11 } 12}; 13 14class Mother 15{ 16public: 17 void cook() 18 { 19 cout << "Cooking" << endl; 20 } 21}; 22 23class Child : public Father, public Mother 24{ 25}; 26 27int main() 28{ 29 Child child; 30 31 child.work(); 32 child.cook(); 33 34 return 0; 35}
Output
1Working 2Cooking
3. Hierarchical Inheritance
Multiple derived classes inherit from a single base class.
Example
1#include <iostream> 2 3using namespace std; 4 5class Animal 6{ 7public: 8 void eat() 9 { 10 cout << "Eating" << endl; 11 } 12}; 13 14class Dog : public Animal 15{ 16}; 17 18class Cat : public Animal 19{ 20}; 21 22int main() 23{ 24 Dog dog; 25 Cat cat; 26 27 dog.eat(); 28 cat.eat(); 29 30 return 0; 31}
Output
1Eating 2Eating
4. Hybrid Inheritance
Hybrid inheritance combines two or more inheritance types, such as multiple and multilevel inheritance.
Example
1#include <iostream> 2 3using namespace std; 4 5class Person 6{ 7public: 8 void display() 9 { 10 cout << "Person" << endl; 11 } 12}; 13 14class Student : public Person 15{ 16}; 17 18class Employee 19{ 20public: 21 void work() 22 { 23 cout << "Working" << endl; 24 } 25}; 26 27class Intern : public Student, public Employee 28{ 29}; 30 31int main() 32{ 33 Intern obj; 34 35 obj.display(); 36 obj.work(); 37 38 return 0; 39}
Output
1Person 2Working
5. Multilevel Inheritance
In multilevel inheritance, a derived class becomes the base class for another class.
Example
1#include <iostream> 2 3using namespace std; 4 5class GrandParent 6{ 7public: 8 void family() 9 { 10 cout << "Grandparent" << endl; 11 } 12}; 13 14class Parent : public GrandParent 15{ 16}; 17 18class Child : public Parent 19{ 20}; 21 22int main() 23{ 24 Child child; 25 26 child.family(); 27 28 return 0; 29}
Output
1Grandparent
6. Protected Members
The protected access specifier allows members to be accessed within the class and its derived classes but not from outside the class.
Example
1#include <iostream> 2 3using namespace std; 4 5class Person 6{ 7protected: 8 string name = "Ankit"; 9}; 10 11class Student : public Person 12{ 13public: 14 void display() 15 { 16 cout << name; 17 } 18}; 19 20int main() 21{ 22 Student student; 23 24 student.display(); 25 26 return 0; 27}
Output
1Ankit
7. Virtual Base Class
A virtual base class solves the Diamond Problem in multiple inheritance by ensuring only one shared instance of the base class exists.
Without Virtual Inheritance
Animal
/ \
Dog Cat
\ /
Puppy
Puppy inherits two copies of Animal, causing ambiguity.
With Virtual Inheritance
1#include <iostream> 2 3using namespace std; 4 5class Animal 6{ 7public: 8 void eat() 9 { 10 cout << "Eating"; 11 } 12}; 13 14class Dog : virtual public Animal 15{ 16}; 17 18class Cat : virtual public Animal 19{ 20}; 21 22class Puppy : public Dog, public Cat 23{ 24}; 25 26int main() 27{ 28 Puppy puppy; 29 30 puppy.eat(); 31 32 return 0; 33}
Output
1Eating
Virtual inheritance ensures that only one Animal object exists.
Types of Inheritance
| Type | Description |
|---|---|
| Single | One base class → One derived class |
| Multiple | Multiple base classes → One derived class |
| Multilevel | Base → Derived → Derived |
| Hierarchical | One base class → Multiple derived classes |
| Hybrid | Combination of different inheritance types |
Best Practices
- Prefer public inheritance for "is-a" relationships.
- Keep base classes focused on shared behavior.
- Use
protectedonly when derived classes genuinely need access. - Avoid unnecessary multiple inheritance.
- Use virtual base classes to solve the Diamond Problem.
- Keep inheritance hierarchies simple and maintainable.
Practice Problems
- Create a single inheritance example using
PersonandStudent. - Implement multiple inheritance using
TeacherandResearcher. - Create a multilevel inheritance hierarchy.
- Implement hierarchical inheritance with different animal classes.
- Demonstrate hybrid inheritance.
- Solve the Diamond Problem using virtual inheritance.
Interview Questions
- What is inheritance in C++?
- What are the advantages of inheritance?
- What is the difference between single and multiple inheritance?
- What is multilevel inheritance?
- What is hierarchical inheritance?
- What is hybrid inheritance?
- What are protected members?
- What is the Diamond Problem?
- How does virtual inheritance solve the Diamond Problem?
- When should virtual base classes be used?
Summary
In this module, you learned:
- What inheritance is and why it is used in OOP.
- How single, multiple, multilevel, hierarchical, and hybrid inheritance work.
- How protected members allow controlled access in derived classes.
- What the Diamond Problem is in multiple inheritance.
- How virtual base classes eliminate duplicate base-class instances.
- Best practices for designing clean and reusable inheritance hierarchies.
Next Module: Module 18: Polymorphism — Function Overloading, Operator Overloading, Method Overriding, Virtual Functions, Pure Virtual Functions, Abstract Classes, Runtime Polymorphism, and Dynamic Binding.