In OOP, inheritance is one of its key features. Inheritance allows a user to create a new class (or derived class) from an existing class (or base class).
Furthermore, a derived class inherits all features from a base class and can have extra or additional features of its own.
In C++ programming, there are five types of Inheritance, and they include:
All non-private members of a base class can be accessed by its derived class. Therefore, base-class members that do not need to be accessed by the member functions of derived classes should be declared private in the base class.
The table below shows different access types according to who can access them in the following way:
Access | public | protected | private |
---|---|---|---|
Same class | yes | yes | yes |
Derived classes | yes | yes | no |
Outside classes | yes | no | no |
If you are deriving a class from a base class, you must know that the base class may be inherited through public, protected, or private inheritance
In C++ programming, if you are deriving a class from a public base class, the public members of the base class become public members of the derived class; while the protected members of the base class become the protected members of the derived class.
You can never access a base class's private member directly from a derived class, however, you can access it through calls to the public and protected members of the base class.
If you are deriving from a protected base class, the public and protected members of the base class then become protected members of the derived class.
If you are deriving from a private base class, the public and the protected members of the base class then become the private-members of the derived class.