coderz.py

Keep Coding Keep Cheering!

Types of Inheritance in C++

Accessing Elements

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.

types of Inheritance

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:
types of 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

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement