Types of Inheritance in C++

category: 

There are three types of inheritance in C++.

  1.   Private inheritance
  2.   Public inheritance
  3.   Protected inheritance

The access specifier private is not accessible outside the class. The access specifier public is accessible outside the class. The access specifier protected is accessible in the derived class with some restrictions, the explanation is given later.

Types of Inheritance in C++

1. Private inheritance

In the derived class, the reserved word private is optional before the base class but for a good programming practice it is a good idea to use it. Otherwise, it will be considered as private by default.

A member declared with the access speciifer private could be accessed only be members of a class where it is defined. The derived class is restricted to the private section.

For example

derived_calss:private base_class

{ };

Or

derived_class:base_class

{ };

2. Public inheritance

This type of inheritance must be explicitly mentioned. Instead of private, which is by default, simply use public.

For example

Derive_class: public base_class

{};

3. Protected inheritance

The word protected in the derived class means that if another class is derived from this current derived class that is derived_derived class, the protected members in the base class will be private in the first derived class instead of public. This prohibits the derived_derived class from being access to the protected members of the base class.

Inheritance guidelines

We know that inheritance is a relationship that is a way of relating two classes so that one class is capable of using another class members without redefining them. Anything that is associated with an access specfier public is accessible or visible to all derived classes of a base class. The information given to and taken from the member function of the same access specifier is accessible by code outside the class. The next specifier comes private, which is known as the highest level of data hidng. The private members of the base or super class remain inaccessible even to the derived class or sub class. The third and last access specifier is the protected. Data member and member function associated with this access specifier can be inherited by the sub class or child class or derived class. With two objects of the same class, they are not allowed to use each other data member of the protected access specifier. Both will have the same names with different data.

It is to be noted that when an object of a derived class is created, if there exist a constructor in the base class, it will be called first, followed by the derived class constructor. It is also to be noted that when a derived object is destroyed, the destructor it contains is called first, followed by the base class destructor, if it exists.

Conclusion of access specifiers

Usually, in the derived class, the base class is defined as public. With this access specifier, the public members become public members and the protected members become protected members of the derived class. The change of public to access specifier become protected members of the derived class. The change of to access specifier private as explained, both the public and protected members will be treated as protected members of the derived class. The use of private instead of public and protected, the public and protected members become private members of the derived class. It should be kept in mind that private members defined in the base class or parent class are private to that base class or parent class and therefore, cannot be accessed or in other owrds, not inherited. The three different access specifiers are at the discretionary of the programmer that how each member should be accessed inside and outside of the class where they are defined.

Conclusion of Inheritance

Inheritance is one of the most important components of the object oriented programming and in turns the hierarchies of inheritance classes. By now the concept must have been cleared that inheritance is used with the base or parent class via the derived class. We have already seen that c++ supports both simple and multiple inheritance. We have also seen the three different types of inheritance. If you want the protected members in the base class to be the same as protected and public members to be the same as public in the derived class, or in other words to keep these two access specifiers with the derived class. But mostly the programmer is concerned with public inheritance. In general, this would be advisable never to leave out the public access specifier. One of the biggestadvantages of inheritance is that with any access control specifier, a derived class can define its own members and also can redefine members of the base class. This means the derived class can override the inherited characteristics and can add new characteristcs.

At the end of the inheritance conclusion, inheritance is just a convenience for the programmer. If you really need simple inheritance, try to avoid complicated classes to overcome or minimize such situations. If you cannot avoid and really want ot include multiple inheritance, you better avoid the more complicated situations. If you avoid all these and try to model the situation as simple as possible, but there is no simpler.