Hybrid Inheritance in C++ Programming


Inheritance is a basic object oriented feature in which one class acquires and inherit the properties of another class. All the properties of the Base Class ( also known as the Parent Class or Super class ) are present in the Derived Class ( also known as the Child Class or Sub class ). Hybrid Inheritance can also be achieved using a combination of Multilevel and Hierarchical inheritance.

In this program, we have four classes: grandfather, father, mother, and son.

  • The class grandfather is the base class and has a public member function house that displays the message "3BHK House." The class father is derived from the grandfather class and has a public member function land that displays the message "5Arcs of Land." The class mother is another base class and has a public member function gold that displays the message "25g of Gold."
  • The son class is derived from both the father and mother classes and has a public member function car that displays the message "Audi Car."
  • In the main function, we create an object o of the son class and use the dot (.) operator to call the member functions of all the inherited classes. We can see that the son class is able to access the member functions of both the father and mother classes, as well as the member function of the grandfather class through inheritance.

Source Code

#include<iostream>
using namespace std;
//Hybrid Inheritance in C++ Programming
class grandfather
{
public:
    void house()
    {
        cout<<"3BHK House."<<endl;
    }
};
class father:public grandfather
{
public:
    void land()
    {
        cout<<"5Arcs of Land."<<endl;
    }
};
class mother
{
public:
    void gold()
    {
        cout<<"25g of Gold."<<endl;
    }
};
class son:public father,public mother
{
  public:
    void car()
    {
        cout<<"Audi Car."<<endl;
    }
};
 
 
int main()
{
    son o;
    o.house();
    o.land();
    o.car();
    o.gold();
    return 0;
}
 

Output

3BHK House.
5Arcs of Land.
Audi Car.
25g of Gold.
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples