on Leave a Comment

Types of Inheritance in C++

This article contains Types of Inheritance in C++.

To understand types of inheritance in c++, you must know about Inheritance in C++.

Types of Inheritance in C++

(1) Single Inheritance
(2) Multiple Inheritance
(3) Multilevel Inheritance 
(4) Hierarchical Inheritance
(5) Hybrid Inheritance

Single Inheritance

In single inheritance, a derived class inherits properties of only one base class or super class. This is most simple type of inheritance.
Single inheritance in c++
Example:

class A
{
        public:
            int a;
};
class B: public A
{
        public:
            int b;
};

Multiple Inheritance

In multiple inheritance, a derived class inherits from two or more than two base classes.
multiple inheritance in c++
Example:

class A
{
       public:
           int a;
};
class B
{
       public:
           int b;
};
class C: public A, public B
{
       public:
           int c;
};

Multilevel Inheritance 

In multilevel inheritance, a derived class inherits from one base class and some other derived inherits from that derived class and so on.
multilevel inheritance in c++
Example:

class A
{
       public:
           int a;
};
class B: public A
{
       public:
           int b;
};
class C: public B
{
       public:
           int c;
};

Hierarchical Inheritance

In hierarchical inheritance, multiple derived class inherits from single base class.
Hierarchical Inheritance in C++
Example:

class A
{
       public:
           int a;
};
class B: public A
{
       public:
           int b;
};
class C: public A
       public:
           int c;
};

Hybrid Inheritance 

Hybrid inheritance is a combination of two or more inheritance.
Hybrid Inheritance in C++
Example:

class A
{
       public:
           int a;
};
class B: public A
{
       public:
           int b;
};
class C: public A
       public:
           int c;
};
class D: public B, public C
{
      public:
          int d;
};

0 comments:

Post a Comment