Subscribe

Header Ads

Inheritance in C++

 



Inheritance is the relationship between the two or more classes in which a class that is derived from a base class inherits the properties of that base class.

Base class: 

The class whose properties are inherited by the derived class is known as the base class.
It is also known as superclass and parent class.

Derived class:

The class that is derived from the base class is known as the derived class.
It is also known as subclass and child class.

Types of inheritance are:

  1. Single inheritance
  2. Multilevel inheritance
  3. Multiple inheritance
  4. Hierarchical inheritance
  5. Hybrid inheritance

1. Single Inheritance:

The inheritance in which only one class derives from a single base class is known as single inheritance.
For example:

class A
{
      ... ;
};

class B: public A
{
        ...;
};

2) Multilevel Inheritance:

When a class that is derived from a base class is again derived into some other class. Such type of inheritance is known as multilevel inheritance.
For example:


class A
{
    ... ;
};

class B: public A
{
    ... ;
};

class C: public B
{
    ... ;
};

3. Multiple inheritance:

The inheritance in which a class is derived from more than one class is known as multiple inheritance.
For example:



class A
{
    ... ;
};
 class B
{
    ... ;
};

class C: public A, public B
{
     ... ;
};

4. Hierarchical inheritance:

The inheritance in which more than one class derives from a single base class is known as hierarchical inheritance.
For example:



class A
{
    ... ;
};

class B: public A
{
    ... ;
};

class C: public A
{
    ... ;
};

5. Hybrid Inheritance:

The inheritance in which more than one type of inheritance is used to derive a class is known as hybrid inheritance.
For example:



class A
{
    ... ;
};

class B: public A
{
    ... ;
};

class C: public A
{
    ... ;
};

class D: public B, public C
{
    ... ;
};

Post a Comment

2 Comments