Imagine you have a class called Animal. It has some common features, such as eating. Now you create another class called Dog. A dog can also eat, but it can do something extra, like barking. Instead of writing the eat() function again inside the Dog class, C++ allows Dog to inherit it from Animal. This is called inheritance.
Let’s understand about C++ Inheritance.
What is C++ Inheritance?
Inheritance allows one class to use the data and functions of another class.
- The class that gives its features is called the base class.
- The class that receives those features is called the derived class.
Base Class
↓
Derived Class
For example:
class Animal {
// base class
};
class Dog : public Animal {
// derived class
};
Here, Dog inherits from Animal.
Access Specifiers in Inheritance
When one class inherits another class, we can use public, protected, or private to control how the inherited members can be accessed.
For example:
class Animal {
public:
void eat() {
cout << "Animal is eating";
}
};
class Dog : public Animal {
};
Here, public means that the public members of Animal, such as eat(), remain public in Dog.
So we can write:
Dog dog;
dog.eat();
The dog object can use eat() because it was inherited using public inheritance.
C++ has three types of inheritance access:
public→ public members stay public.protected→ public and protected members become protected.private→ public and protected members become private.
Inheritance Example
Let’s see a complete example:
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Animal is eating\n";
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Dog is barking";
}
};
int main() {
Dog dog;
dog.eat();
dog.bark();
return 0;
}
Output:
Animal is eating
Dog is barking
Notice that we did not write eat() inside the Dog class. Because Dog inherits from Animal, the dog object can use:
dog.eat();
It can also use its own function:
dog.bark();
Why Do We Need Inheritance?
Inheritance mainly helps us reuse code.
Suppose Dog, Cat, and Cow all need an eat() function. Instead of writing the same function three times, we can put it inside an Animal class.
Animal
/ \
Dog Cat
Both Dog and Cat can use the features of Animal.
This makes the code shorter and easier to manage.
Types of C++ Inheritance
C++ provides different types of inheritance depending on how classes are connected. The main types of C++ inheritance are:
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Let’s understand each type with simple examples.
Single Inheritance in C++
When one class inherits from one other class, it is called single inheritance.
For example:
Person
↓
Student
Here, Student inherits from Person.
Example
Let’s see a complete example:
#include <iostream>
using namespace std;
class Person {
public:
void speak() {
cout << "Person can speak\n";
}
};
class Student : public Person {
public:
void study() {
cout << "Student is studying";
}
};
int main() {
Student student;
student.speak();
student.study();
return 0;
}
Output:
Person can speak
Student is studying
Here, Student inherits the speak() function from Person and also has its own study() function.
Multiple Inheritance in C++
When one class inherits from two or more classes, it is called multiple inheritance.
For example:
Person Student
\ /
\ /
Teacher
Here, Teacher inherits from both Person and Student.
Example
#include <iostream>
using namespace std;
class Person {
public:
void speak() {
cout << "Person can speak\n";
}
};
class Student {
public:
void study() {
cout << "Student is studying\n";
}
};
class Teacher : public Person, public Student {
public:
void teach() {
cout << "Teacher is teaching";
}
};
int main() {
Teacher teacher;
teacher.speak();
teacher.study();
teacher.teach();
return 0;
}
Output:
Person can speak
Student is studying
Teacher is teaching
The Teacher class inherits features from both Person and Student.
Multilevel Inheritance in C++
When inheritance happens at multiple levels, it is called multilevel inheritance.
For example:
Animal
↓
Dog
↓
Puppy
Here, Dog inherits from Animal, and Puppy inherits from Dog.
Example
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Animal is eating\n";
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Dog is barking\n";
}
};
class Puppy : public Dog {
public:
void play() {
cout << "Puppy is playing";
}
};
int main() {
Puppy puppy;
puppy.eat();
puppy.bark();
puppy.play();
return 0;
}
Output:
Animal is eating
Dog is barking
Puppy is playing
The Puppy object can use the accessible functions inherited through both Dog and Animal.
Hierarchical Inheritance in C++
When multiple classes inherit from the same base class, it is called hierarchical inheritance.
For example:
Animal
/ \
Dog Cat
Here, both Dog and Cat inherit from Animal.
Example
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Animal is eating\n";
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Dog is barking\n";
}
};
class Cat : public Animal {
public:
void meow() {
cout << "Cat is meowing";
}
};
int main() {
Dog dog;
Cat cat;
dog.eat();
dog.bark();
cat.eat();
cat.meow();
return 0;
}
Output:
Animal is eating
Dog is barking
Animal is eating
Cat is meowing
Both Dog and Cat can use the eat() function inherited from Animal.
Hybrid Inheritance in C++
When two or more types of inheritance are combined, it is called hybrid inheritance.
For example:
Animal
/ \
Dog Cat
\ /
Puppy
This example combines hierarchical inheritance and multiple inheritance.
Example
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Animal is eating\n";
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Dog is barking\n";
}
};
class Cat : public Animal {
public:
void meow() {
cout << "Cat is meowing\n";
}
};
class Puppy : public Dog, public Cat {
public:
void play() {
cout << "Puppy is playing";
}
};
int main() {
Puppy puppy;
puppy.bark();
puppy.meow();
puppy.play();
return 0;
}
Output:
Dog is barking
Cat is meowing
Puppy is playing
Here, Puppy inherits from both Dog and Cat, while both Dog and Cat inherit from Animal.
Note: In this example, we do not call
puppy.eat()becausePuppygets two copies ofAnimalthroughDogandCat. This can create ambiguity. C++ provides solutions such as virtual inheritance for such situations.
Quick Summary
| Type | Meaning |
|---|---|
| Single Inheritance | One class inherits from one base class |
| Multiple Inheritance | One class inherits from multiple base classes |
| Multilevel Inheritance | Inheritance happens through multiple levels |
| Hierarchical Inheritance | Multiple classes inherit from one base class |
| Hybrid Inheritance | Two or more types of inheritance are combined |
These five types help us understand how classes can be connected and how features can be reused through inheritance.
Key Points to Remember
- Inheritance allows one class to use features of another class.
- The base class provides the features.
- The derived class receives those features.
- Inheritance helps us reuse code.
:is used to inherit from a class.- Single, multiple, multilevel, hierarchical and hybrid inheritance are common types of inheritance.
Mini Quiz
Mini Project
Mini Project: Employee Salary System
Question:
Create a simple C++ program using inheritance to store information about different types of employees.
- Create a base class
Employee. - Store the employee’s name and basic salary.
- Create two derived classes:
–Developer
–Designer - Add a function in each derived class to calculate the employee’s total salary.
- Display the details of both employees.
View Answer Code
#include <iostream>
#include <string>
using namespace std;
class Employee {
public:
string name;
double salary;
void showBasicDetails() {
cout << "Name: " << name << "\n";
cout << "Basic Salary: " << salary << "\n";
}
};
class Developer : public Employee {
public:
double bonus;
void showSalary() {
showBasicDetails();
cout << "Bonus: " << bonus << "\n";
cout << "Total Salary: " << salary + bonus << "\n";
}
};
class Designer : public Employee {
public:
double bonus;
void showSalary() {
showBasicDetails();
cout << "Bonus: " << bonus << "\n";
cout << "Total Salary: " << salary + bonus << "\n";
}
};
int main() {
Developer developer;
developer.name = "Aarav";
developer.salary = 40000;
developer.bonus = 5000;
Designer designer;
designer.name = "Meera";
designer.salary = 35000;
designer.bonus = 4000;
cout << "Developer Details\n";
developer.showSalary();
cout << "\nDesigner Details\n";
designer.showSalary();
return 0;
}
What’s Next?
In the next chapter, we will learn about C++ Polymorphism and see how the same function can work in different ways.
Frequently Asked Questions (FAQs)
Q1. What is inheritance in C++?
Inheritance in C++ allows a derived class to use accessible data and functions from a base class, helping reuse existing code.
Q2. What is a base class and derived class in C++?
The base class provides features, while the derived class inherits those accessible features and can add its own functionality.
Q3. What are the types of inheritance in C++?
Common types include single, multilevel, multiple, hierarchical, and hybrid inheritance.
Q4. Why is inheritance used in C++?
Inheritance is mainly used for code reuse and to create relationships between related classes.
Q5. How do you implement single inheritance in C++?
Single inheritance is implemented when one derived class inherits from one base class using the : syntax.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
