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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago