Inheritance is a fundamental concept in Object-Oriented Programming (OOP), providing developers with a powerful tool to build modular, reusable, and scalable applications in C++. By leveraging inheritance, a programmer can define a new class, known as the derived class, that inherits properties and behaviors from an existing class called the base class. This not only saves time but also enhances consistency and flexibility in code design.
In this blog, we will explore the foundational concepts of inheritance in C++, its types, and practical implementation examples. By the end, you’ll have a solid understanding of how inheritance can simplify your code and enhance its functionality.
Inheritance is one of the important concepts of Object-Oriented Programming (OOP) in C++. It lets programmers to define a new class which is based upon an existing class but that can also possess qualities and characteristics of it’s own.
It is said that one class, which is known as derived class, takes the characteristics of another class, which is called the base class. The derived class can use the factors from base class and at the same time, it can append or alter the factors inherited to it.
Code Reusability: Don’t rewrite entire pieces of code use the existing code instead.
Modularity: Simplify systems into broken down divisions, or hierarchal subsystems.
Scalability: Several new features could be incorporated without having to make changes to the original code usually.
Consistency: Ensure consistency with similar objects and affairs within an organization.
Base Class: The class in which its properties and methods can be inherited.
Derived Class: The class that is derived by using inheritance from another class known as the base class.
Access Specifiers: Accessment level keywords (Public, Protect, Private) used to limit access to inherited class members.
Overriding: Over riding a base class method in the derived class.
Virtual Functions: Used in an attempt to insist that the derived class’s method should be called during polymorphism.
C++ supports several types of inheritance to different use cases:
Here, the derived class is sub-class of only one base class in the single inheritance. This the most basic type of inheritance used when there is a direct association between the two classes.
Example: One Car class which is a subclass of a Vehicle class.
In multiple inheritance, one or more base or parent class tiers are subsumed into one derived class. On the one hand, it makes it possible for the derived class to achieve functionalities from sources that are different from that of the base class, on the other hand
Example: A FlyingCar class as a subclass of both Car and Airplane classes.
Multilevel inheritance means a form of inheritance where a derived class is used to make another derived class. It fosters hierarchy of this type of inheritance.
Example: A SmartPhone class that is a subclass of the Mobile class, and this is a subclass of an ElectronicDevice class.
In hierarchical inheritance there is only one base class whereas, numerous derived classes can be invented. They have applied it extensively in an endeavor of categorizing multiple category instances and variations of one category.
Example: Car, Bike and Truck classes which have inherited from a Vehicle class.
This type of inheritance inherits two or more of the known types of inheritance. It is commonly used to model other types of relations.
Example: The class structure that use both single and multiple inheritance in the same class.
These involve public member, private member and protected members, member access specifiers dictate how members in the base class are referred in the derived class. There are three types of access specifiers:
Public Inheritance: Members of the base class retain their access levels in the derived class.
Protected Inheritance: In the derived class all public and protected members of the base class remain protected.
Private Inheritance: The base classes own public and protected members remain private or are hidden from the level of the derived class.
Choosing the right access specifier depends on the level of encapsulation and exposure needed.
Indian Institute of Embedded Systems – IIES