.

Inheritance in C++

Inheritance is defined as deriving the classNamefrom already existing className. This is the very important feature of object oriented programming. This inheritance is classified under two classes. They are

Sub className

Super className

Sub className– Inherits the properties from other className

Super className– Inherits the properties by sub className

Base className

The base classNameis the classNamethat is defined in the declaration of the className.

Derived className

The derived classNameis defined as deriving the classNamefrom already existing classNameor the base className.

Syntax

    classNamederived_class_name :: visibility-mode base_class_name  
    {  
        // body of the derived className.  
    }
    

Access control and Inheritance

The access specifiers may be public, private and protected. The default access specifier is private. Access specifiers affect accessibility of data member of base classNamefrom the derived className. It determines the accessibility of data member of the base classNameoutside the derived className.

Public Inheritance

This mode of inheritance is used frequently.

inheritance img1

Private Inheritance

The public and protected members of base classNamebecome more private members of derived className.

inheritance img2

Protected Inheritance

The public and protected members of base classNamebecome more protected members of derived className.

inheritance img3

Types of Inheritance

There are five different types of inheritance. They are

  • Single inheritance
  • Multiple inheritance
  • Hierarchical inheritance
  • Multilevel inheritance
  • Hybrid inheritance

Single Inheritance

When one classNameis derived from another classNameis known as single inheritance.

Example

Multiple inheritances

When one classNameis inherited from more than one base classNameis called as multiple inheritances.

Example

Hierarchical inheritance

When more than one sub classNameis inherited from a single base classNameis called as hierarchical inheritance.

Example

Multilevel inheritance

A multilevel inheritance a derived classNameis derived from already existing derived className.

Example

Hybrid inheritance

In hybrid inheritance more than one classNameis derived from a single base className.

Example

Multiple Inheritances

In multiple inheritances the single classNameis inherited from more than one base className.

Example

.