15 Sept 2019

  • September 15, 2019
  • Amitraj

5.) Hybrid - Inheritance  


What is Hybrid ?

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.


C++ Hybrid Inheritance Block Diagram






C++ Hybrid Inheritance Example

Here is a simple program to illustrate the concept of hybrid inheritance in C++.

// hybrid inheritance.cpp
#include <iostream>
using namespace std;

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;
  }
};

int main()
{
         D obj1;          //object of derived class D
  obj1.sum();
  return 0;
}                //end of program

Output

Sum= 14


Related Posts:

  • Templates in C++ Templates in C++ -> Templates is one of the features added to C++ Recently. it is a new concept which enable us to define Generic classes and functions and thus provides support for Generic programming. -> Generic pr… Read More
  • Static Class member in Class Template Static Class member in Class Template -> Each instantiation of class template has its own copy of member static variables. For example, in the following program there are two instances collection and collection. So … Read More
  • Virtual Destructors in C++ Virtual Destructors in C++ A virtual destructor however, is pretty much feasible in C++. in fact, its use is often promoted in certain situations. One such situation is when we need to make sure that the different dest… Read More
  • Overloading function Templates in C++ Overloading function Templates in C++ -> We may overload a function Template either by a non - template function template. -> if we call the name of an overloaded function template, the compiler will try to deduce it… Read More
  • Advantages / Benefits of Exception Handling Advantages of Exception Handling -> Following are the advantages of exception handling: 1. Exception handling separates the exception handling code from the main logic of program. 2. Exceptions can be handled outside … Read More

Translate

Popular Posts