Multiple 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 ). The inheritance in which a class can be derived from more than one class or a class can have more than one base class is known as multiple inheritance

In this program, there are three classes father, mother, and son. The father class has a public member function fishing() that prints the message "Learn Fishing." Similarly, the mother class has a public member function cooking() that prints the message "Learn Cooking."

The son class is derived from both the father and mother classes using the public keyword. This indicates that the public members of both the base classes are accessible from the son class. The son class has a public member function coding() that prints the message "Learn Coding."

In the main() function, an object o of the son class is created. Since the son class is derived from both the father and mother classes, it can access the fishing() member function of the father class and the cooking() member function of the mother class using the dot (.) operator. Hence, both the functions are called for the object o. Next, the coding() member function of the son class is called using the same object o.

The son class has inherited the fishing() member function from the father class, the cooking() member function from the mother class, and has defined its own member function coding(). The main() function calls all three functions using the object of the son class.

Source Code

#include<iostream>
using namespace std;
//Multiple Inheritance in C++ Programming
class father
{
    public:
    void fishing()
    {
        cout<<"Learn Fishing."<<endl;
    }
};
class mother
{
    public:
    void cooking()
    {
        cout<<"Learn Cooking."<<endl;
    }
};
class son:public father,public mother
{
    public:
    void coding()
    {
        cout<<"Learn Coding."<<endl;
    }
};
int main()
{
    son o;
    o.fishing();
    o.cooking();
    o.coding();
    return 0;
}
 

Output

Learn Fishing.
Learn Cooking.
Learn Coding.
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