CPP Concepts of Inheritance

Concepts of Inheritance

  • Inheritance is one of the key feature of object-oriented programming.
  • it allows user to create a new class(derived class) from a existing class(base class).
  • The derived class inherits all feature from a base class and it can have additional features of its own.

Types of Inheritance:

C++, we have 5 different types of Inheritance. Namely,
  1. Single Inheritance
  2. Multiple Inheritance
  3. Hierarchical Inheritance
  4. Multilevel Inheritance
  5. Hybrid Inheritance (also known as Virtual Inheritance)

Access Control and Inheritance:

  • A derived class can access all the non-private members of its base class.
  • Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.
  • Following table 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

When deriving a class from a base class, the base class may be inherited through public, protected or private inheritance

Public Inheritance:

  • When deriving a class from a public base class,public members of the base class become public members of the derived class and protected members of the base class becomeprotected members of the derived class.
  • A base class's privatemembers are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.

Protected Inheritance:

  • When deriving from a protected base class,public and protected members of the base class become protectedmembers of the derived class.

Private Inheritance:

  • When deriving from a private base class,public and protected members of the base class become privatemembers of the derived class.

Share This Page on:

Question


Heli | 07-Nov-2016 01:52:55 pm

Can you provide some examples for inheritance.


Ask Question