C++ Multiple, Multilevel and Hierarchical inheritance
C++ Multiple, Multilevel and Hierarchical inheritance
Inheritance
In Object-Oriented Programming, inheritance is integral to the process whereby one group can inherit skills and characteristics passed down from other sorts of groups. This tenet is among OOP's most critical characteristics.
In practice, this involves creating new groups using pre-existing ones through various derivative techniques where some older classes take on new techniques known as 'derived' or 'child' classes – while others form an original foundation named 'base' or 'parent' classes.
Derived categorizations inevitably inherit all attributes and skills associated with their Parent class without external modifications except where necessary additional features enhance functionality.
Subgroups are an example of this process as they rely on inherited aspects from earlier categorizations. Super classes provide foundational support for these derived categorizations by serving as a basis for new subgroups previously derived form.
With Access Control of Inheritance, all non-private members of a base category are accessible to derived categories. Elements that should not be accessed via member functions in derived class should have received 'private' classifications on their respective base class. This allows control and security over sensitive data or features, ensuring it remains inaccessible to unauthorized access during essential tasks.
Access | Public | private | Protected |
Same class | True | true | True |
Derived class | True | True | False |
Outside of other classes | True | False | False |
Types of Inheritance in C++
There are five types of inheritance in C++. In this we able to know how the derived class is inheriting the properties of base class.
- Single inheritance.
- Multilevel inheritance.
- Multiple inheritance.
- Hierarchical inheritance.
- Hybrid inheritance.
Single inheritance:
In C++, single inheritance is the most basic kind of inheritance. Inheritance is utilized in this way when a single class obtains the attributes of a base class. According to the inheritance's specified visibility mode (private, protected, or public), the derived class gets access to every data member of the base class.
![C++ Multiple, Multilevel and Hierarchical inheritance](https://static.tutorialandexample.com/cpp/cpp-multiple-multilevel-and-hierarchical-inheritance1.png)
Syntax:
class base class_A
{
//class definition
};
class derived_class: visibility_mode base_class_A
{
//class definition
};
A single base class is inherited by a single derived class. The control of base class members within the derived class is provided by the visibility mode when the derived class is declared.
Multiple inheritance:
Multiple inheritance is the process of inheritance when a class may derive from, or inherit properties from more than one base class, or where a derived class can have more than one base class. Access modifiers are supplied individually for each base class when it inherits another base class. The derived class has access to all base classes' data members in accordance with the access modifiers, and it can determine all of these classes' combined characteristics from them.
![C++ Multiple, Multilevel and Hierarchical inheritance](https://static.tutorialandexample.com/cpp/cpp-multiple-multilevel-and-hierarchical-inheritance2.png)
Syntax:
class base class_A
{
//class definition
};
class base class_B
{
//class definition
};
class derived_class: visibility_mode_1 base_class_A visibility_mode_2 base_class_B
{
//class definition
};
The derived class inherits properties from two base classes, base_class_1 and base_class_2. Each base class's visibility mode is specified when a derived class is declared. These modes could vary depending on the base class.
Multilevel inheritance:
Multilevel Inheritance refers to the inheritance process when a class can be inherited from another derived class. In this scenario, there are three classes: A, B, and C. The base class from which class B derives is A. The derived class of A is B. The class that is descended from class B at this point is C. Class B is now the derived class of class A and serves as the foundation class for class C. The Multilevel Inheritance is the situation that results in this. According to the given visibility modes, the derived classes of each respective base class can access the data members of that base class.
![C++ Multiple, Multilevel and Hierarchical inheritance](https://static.tutorialandexample.com/cpp/cpp-multiple-multilevel-and-hierarchical-inheritance3.png)
Syntax:
class ClassA
{
//class definition
};
class Class_B: visibility_mode Class_A
{
//class definition
};
class Class_C: visibility_mode Class_A
{
//class definition
};
The subclass class_B inherits from class_A. The subclass class_C inherits from the class_B. In each successive level, a subclass inherits a single class. A derived class is generated by using this kind of inheritance from another derived class. It is said to be inheriting the properties of one derived class to another derived class from base class.
Hierarchical inheritance:
Hierarchical inheritance is the type of inheritance when a single base class inherits from several derived classes. Since each class serves as the foundation class for one or more child classes, this inheritance has a structure like a tree. Each derived class's access to the data members is controlled by its unique visibility mode, which is provided during inheritance.
![C++ Multiple, Multilevel and Hierarchical inheritance](https://static.tutorialandexample.com/cpp/cpp-multiple-multilevel-and-hierarchical-inheritance4.png)
class Class_A
{
//class definition
};
class ClassB: visibility_mode class_A
{
//class definition
};
class ClassC: visibility_mode class_A
{
//class definition
};
class ClassD: visibility_mode class_B
{
//class definition
};
class ClassE: visibility_mode class_C
{
//class definition
};
class ClassF: visibility_mode class_E
{
//class definition
};
The characteristics of the base class classA are transmitted down to the subclasses class B and class C. Additionally, class D and class E, two additional subclasses, both inherit from these two subclasses.
Hybrid inheritance:
As the name indicates, hybrid inheritance is the blending of two or more different inheritance types. For instance, the classes in a program are set up so that they simultaneously display single inheritance and hierarchical inheritance. A hybrid inheritance is one such pattern. Among all the inheritance types in C++, this one is likely the most complicated. The base class's data members will be accessible in accordance with the chosen visibility mode.
![C++ Multiple, Multilevel and Hierarchical inheritance](https://static.tutorialandexample.com/cpp/cpp-multiple-multilevel-and-hierarchical-inheritance5.png)
class Class_A
{
//class definition
};
class Class_B
{
//class definition
};
class ClassC: visibility_mode class_A, visibility_mode class_B
{
//class definition
};
class ClassD: visibility_mode class_C
{
//class definition
};
class ClassE: visibility_mode class_C
{
//class definition
};
Class A and class B are two base classes from which the derived class C inherits. Multiple inheritance has this structure. Further inheriting from class C are two subclasses, class D and class E. Multiple inheritance types are present in Hybrid Inheritance's general structure.
Advantages of inheritance
- It takes less time to design an application.
- The software consumes less RAM.
- A shorter amount of time is needed for a program to operate.
- The application's performance has increased (improved).
- The repetition (redundancy) of the code is reduced or removed, improving output consistency and lowering storage costs.
Some other advantages of inheritance are given below:
Code Reusability:
In C++, inheritance facilitates code reuse by letting you create new classes based on previous ones rather than having to create the identical function from start. Therefore, creating new apps might help you save an immense amount of time and work.
Overriding :
In C++, you may change the functions of a derived class to change the behavior of a program without modifying the original code. This is a great way to change current code according to your needs.
Polymorphism:
Polymorphism is the ability to create objects by inheritance that can take many different forms, depending on the parent class from which they inherit. This increases the code's adaptability and responsiveness to shifting requirements.
Organization:
In C++, inheritance helps to logically and hierarchically organize your code. You may create a framework of linked classes that helps you understand how your code operates more easily by creating classes that inherit from other classes.
Modes of inheritance
The modes of inheritance are furtherly classified into three types. These are also known as access specifiers.
- Public: If a subclass is derived from a public base class, it is in the public mode. The protected members of the base class will then become protected in the derived class, and the public elements will change from protected to public.
- Protected: If a subclass is derived from a Protected base class, the mode is protected. The base class's protected and public members will then both become protected in the derived class.
- Private: If a subclass is derived from a Private base class, it is in the private mode. The base class's protected and public members will then both change to Private in the derived class.
Conclusion
Inheritance is the ability of a class to obtain or "inherit" properties from an already-existing class, sometimes referred to as the parent class. In essence, these characteristics are the parent class's data (variables) and methods (functions).
Code may be reused because of inheritance. The new class inherit all attributes and capabilities from the pre-existing parent class without changing a line of code. The new classes are known as child classes or derived classes, whereas the parent classes are also referred to as base classes.