Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

What is Hybrid Inheritance in C++

Hybrid or Multipath Inheritance in C++

As we know that inheritance is the process of  extending the source code of one class to another class

  • Definition: It’s interesting to know that we can have the combination of both multilevel and Hierarchical Inheritance
  • This type of inheritance is also known as Multipath or Virtual Inheritance

Syntax to Implement Multipath Inheritance

  • The class that is acquiring the behaviors is called child class or derived class or subclass
  • The class from which behaviors are taken is called parent class or superclass or base class
class parent
{
----;
};
class child1: access parent
{
-----;
//contains properties of both
//parent+child1
};
class child2: access parent
{
----;
//contains properties of both
//parent+child2
};
class child2_1: access child2
{
-----;
//contains properties of both
//parent+child2+child2_1
};
class child1_1:access child1
{
-----;
//contains properties of both
//parent+child2+child1_1
};
Hybrid Inheritance in C++
  • Here you can see that there is a hierarchal inheritance from parent to child1 and child2
  • multilevel inheritance from parent to child1 to child1_1(path1) and parent to child2 to child1_2(path2).

    Example Program demonstrating MultiPath Inheritance in C++

    class A
    {

    public:
    void m1()
    {
    cout<<"\n m1 from class A";
    }

    };

    class B: public A//B class inherits A as Parent
    {

    public:
    void m2()
    {
    cout<<"\n m2 from class B";
    }
    //contains data of A+B

    };

    class C: public A//C class inherits A as Parent
    {

    public:
    void m3()
    {
    cout<<"\n m3 from class C";
    }
    //Contains data of A+C

    };

    class D:public B//multilevel inheritance from A->B->D
    {

    public:
    void m4()
    {
    cout<<"\n m4 from class D";
    }
    //contains data of class A+B+D

    };

    class E:public C//multilevel inheritance from A->C->E
    {

    public:
    void m5()
    {
    cout<<"\n m5 from class E";
    }
    //contains data of class A+C+E

    };

    main()
    {

    D d;//contains A+B+D properties 
    d.m1();
    d.m2();
    d.m4();
    E e;//contains A+C+E properties 
    e.m1();
    e.m3();
    e.m5();

    }

O/P:

m1 from class A
m2 from class B
m4 from class D
m1 from class A
m3 from class C
m5 from class E

Hiarrichal Inheritance from A-B and A-C

  • Class B contains 2 methods m1() from class A and m2() by its own
  • Class C contains 2 methods m1() from class A and m3() by its own

Multilevel inheritance from class A-B-D and A-C-E

  • Class D contains 3 methods m1() from class A and m2() from class B and its own method m4()
  • Class E contains 3 methods m1() from class A and m3() from class C and its own method m5()