Categories: C++

Types of Inheritance in C++

There are 5 types of Inheritance in C++:

  • Single inheritance
  • Multiple inheritance
  • Hierarchical inheritance
  • Multilevel inheritance
  • Hybrid inheritance
Single Inheritance:

A class derives from only one base class in single inheritance. This means that there is only one subclass descended from a single superclass.

Syntax:

class base {
//Body of the base class
};
class derived : access_specifier base {
//Body of the derived class
};
Example:
#include <iostream>
using namespace std;

class Animal {
    public:
        void fun1() {
            cout<<"Animal Class"<<endl;
        }
};

class Dog : public Animal {
    public:
        void fun2() {
            cout<<"I am a dog"<<endl;
        }
};

int main() {
  Dog obj;
  obj.fun1();
  obj.fun2();
  return 0;
}
Output:
Animal Class
I am a dog
Multiple inheritance:

Multiple inheritance is a type of inheritance in which a class is derived from various classes. Class C, as illustrated in the diagram above, is a subclass of classes A and B.

Syntax:

class base1 {
  //body
};
class base2 {
  //body
};
.
.
.
class derived : access_specifier base1, access_specifier base2, ... {
  //body of the derived class
};
Example:
#include <iostream>
using namespace std;

class A {
    protected:
    int a;
    public:
    void seta(int x) {
        a = x;
    }
};

class B {
    protected:
    int b;
    public:
    void setb(int y) {
        b = y;
    }
};
class C : public A, public B {
    public:
    int add() {
        cout<<"Addition of two numbers = "<<a+b;
    }
};

int main() {
  C obj;
  obj.seta(4);
  obj.setb(9);
  obj.add();
  return 0;
}
Output:
Addition of two numbers = 13
Hierarchical inheritance:

In hierarchical inheritance, more than one class inherits from a single base class as shown in the representation above. This gives it a structure of hierarchy.

Example:
#include <iostream>
using namespace std;

class Values {
    protected:
    double a, b;
    public:
    void initialize(double x, double y) {
        a = x;
        b = y;
    }
};

class A : public Values {
    public:
    void add() {
        cout<<"addition = "<<a+b<<endl;
    }
};

class B : public Values {
    public:
    void subtract() {
        cout<<"subtraction = "<<a-b<<endl;
    }
};

int main() {
  A obj1;
  B obj2;
  obj1.initialize(4.5,8.7);
  obj1.add();
  obj2.initialize(3.6,11);
  obj2.subtract();
  return 0;
}
Output:
addition = 13.2
subtraction = -7.4
Multilevel inheritance:

A class is derived from another derived class in multilevel inheritance. If our implementation is correct, this inheritance can have as many levels as we want.

Example:
#include <iostream>
using namespace std;

class Animal {
    public:
    void fun1() {
        cout<<"Animal"<<endl;
    }
};

class PetAnimal : public Animal {
    public:
    void fun2() {
        cout<<"Pet animal"<<endl;
    }
};

class Dog : public PetAnimal {
    public:
    void fun3() {
          fun1();
        fun2();
        cout<<"Dog"<<endl;
    }
};

int main() {
  Dog obj;
  obj.fun3();
  return 0;
}
Output:
Animal
Pet animal
Dog
Hybrid Inheritance:

Hybrid Inheritance is a combination of Hierarchical and Multilevel Inheritance.

Note: also read about Inheritance in C++

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

10 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago