Header Ads Widget

Ticker

6/recent/ticker-posts

C++ Hybrid Inheritance Programing Note+Coding - Digitalise Data

Digitalise Data, Hybrid Inheritance Programing

 C++ Hybrid Inheritance Note+Coding

The inheritance in which the derivation of a class involves more than one form of any inheritance is called hybrid inheritance. Basically C++ hybrid inheritance is combination of two or more types of inheritance. It can also be called multi path inheritance.

Following block diagram highlights the concept of hybrid inheritance which involves single and multiple inheritance.
Above block diagram shows the hybrid combination of single inheritance and multiple inheritance. Hybrid inheritance is used in a situation where we need to apply more than one inheritance in a program.

As in other inheritance, based on the visibility mode used or access specifier used while deriving, the properties of the base class are derived. Access specifier can be private, protected or public.

C++ Hybrid Inheritance Syntax
class A
{
     .........
};
class B : public A
{
     ..........
} ;
class C
{
     ...........
};
 class D : public B, public C
{
     ...........
};
As shown in block diagram class B is derived from class A which is single inheritance and then Class D is inherited from B and class C which is multiple inheritance. So single inheritance and multiple inheritance jointly results in hybrid inheritance.

C++ Hybrid Inheritance Example

// hybrid inheritance.cpp
#include <iostream.h>
#include<conio.h>
class A
{
  public:
  int x;
};
class B : public A
{
  public:
  B()      //constructor to initialize x in base class A
  {
     x = 10;
  }
};
class C
 {
  public:
  int y;
  C()   //constructor to initialize y
  {
      y = 4;
        }
};
class D : public B, public C   //D is derived from class B and class C
{
  public:
  void sum()
  {
      cout << "Sum= " << x + y;
  }
};

void main()
{
         D obj1;          //object of derived class D
  obj1.sum();
  getch();
}                //end of program

Output

Sum= 14

Run This Program Using Turbo C++ in Pc Or TurboCdroid in Android It Will Work

#DigitaliseData
#C++Programing



Post a Comment

0 Comments