Inheritance is one of the fundamental concepts in object-oriented programming (OOP). It allows a class to inherit the properties and behaviors of another class, called the base or parent class. In other words, a derived or child class can be created by inheriting the features of an existing base class.
The derived class can then add its own properties and behaviors, or override the existing ones, without affecting the base class. This way, inheritance promotes code reuse and makes it easier to create and maintain complex software systems.
C++ supports several types of inheritance, including:
- Single inheritance
- Multiple inheritance
- Hierarchical inheritance
- Multilevel inheritance
- Hybrid inheritance
We’ll discuss each of these types in detail below.
Single inheritance is the most common type of inheritance in C++. It involves creating a derived class that inherits the properties and behaviors of a single base class. The derived class can then add its own properties and behaviors as needed.
Let’s take a look at an example of single inheritance:
In this example, the
Shape class is the base class, and the
Rectangle class is the derived class. The
Rectangle class inherits the
setHeight() methods from the
Shape class, and adds its own
getArea() method to calculate the area of a rectangle.
To create a derived class, we use the
public keyword followed by the name of the base class. This indicates that the derived class inherits the public members of the base class. In this example, we’re inheriting the
setHeight() methods, which are public.
Multiple inheritance allows a derived class to inherit the properties and behaviors of two or more base classes. This can be useful when you need to combine the features of multiple classes into a single derived class.
Let’s take a look at an example of multiple inheritance:
In this example, we have two base classes:
Rectangle class is derived from both of these base classes using the
public keyword followed by the names of the base classes. This indicates that the
Rectangle class inherits the public members of both base classes.
Rectangle class adds its own
getArea() method to calculate the area of a rectangle, and it also has access to the
setHeight() methods inherited from the
Shape class, as well as the
setColor() method inherited from the
Color class. The
color variable is protected in the
Color class, so it can be accessed by the
Hierarchical inheritance involves creating multiple derived classes from a single base class. This allows you to create a hierarchy of related classes with different levels of specialization.
Let’s take a look at an example of hierarchical inheritance:
In this example, we have a single base class
Animal and two derived classes
Dog inherit the
eat() method from the
Animal class, which prints a generic message about eating.
Cat class adds its own
meow() method to print the sound of a cat, while the
Dog class adds its own
bark() method to print the sound of a dog. This allows you to create specialized classes for different types of animals, while still sharing common functionality in the base class.
Multilevel inheritance involves creating a chain of derived classes, where each derived class is itself a base class for another derived class. This allows you to create increasingly specialized classes with each level of inheritance.
Let’s take a look at an example of multilevel inheritance:
In this example, we have a base class
Vehicle and an intermediate class
Car, which inherits from
Car class adds its own
drive() method to represent driving a car.
SportsCar class is derived from
Car, which means it inherits the
drive() methods from
Car, respectively. The
SportsCar class adds its own
accelerate() method to represent the unique behavior of a sports car.
This allows you to create increasingly specialized classes, with each level of inheritance adding its own properties and behaviors to the previous level.
Hybrid inheritance is a combination of two or more types of inheritance, such as multiple inheritance and hierarchical inheritance. In hybrid inheritance, you can create a derived class from multiple base classes and then create additional derived classes from that derived class.
For example, consider the following code:
In this example, we have a class hierarchy that includes four classes:
A is the base class of
B is one of the base classes of
C is a completely separate class that is not related to
D is derived from both
E is derived from
D is an example of hybrid inheritance because it inherits from both
E is also an example of hybrid inheritance because it is derived from
D, which is itself derived from multiple base classes.
With hybrid inheritance, you can create complex class hierarchies that model relationships between objects in a program. However, as with any type of inheritance, it is important to use hybrid inheritance judiciously and with care to avoid making your programs too complex and hard to maintain.
Inheritance is a powerful feature of object-oriented programming that allows you to create new classes by inheriting properties and behaviors from existing classes. C++ supports multiple types of inheritance, including single, multiple, hierarchical, multilevel, and, hybrid inheritance.
In each type of inheritance, the derived class inherits the properties and behaviors of the base class(es) and can also add its own properties and behaviors.
In C++, inheritance is implemented using the
class keyword, followed by a colon and the keyword
protected, depending on the desired level of access to the inherited members.
Inheritance is a powerful tool for creating efficient, reusable code and for modeling relationships between objects in a program. However, it should be used judiciously and with care, as it can make programs more complex and harder to maintain if overused.
By understanding the different types of inheritance and how they can be used effectively, you can create well-designed and maintainable object-oriented programs in C++.