Multiple-Inheritance-in-C++ .

In this session i have discussed multiple inheritance in C++ .


A class can inherit the more than one class this is called the multiple inheritance . That means more than one parent and only one child class .


Multiple Inheritance :

multiple inheritance image

The class A , B , C inherit in D class. The class A , B , C is the parent class and class D is child class .

1-Multiple Inheritance Demo

Example : Write a program that demonstrate the multiple inheritance in C++

    #include<iostream>
    using namespace std;  
    class A
    {
        public :    
        void sum(int x,int y)
        {
            int result = x + y;
            cout<<"\nSum : "<<result;
        }
    };
    class B
    {
        public :
        void sub(int x,int y)
        {
            int result = x - y;
            cout<<"\nSub : "<<result;
        }
    };
    class C
    {
        public :
        void multi(int x,int y)
        {
            int result = x * y;
            cout<<"\nMulti : "<<result;
        }
    };
    class D : public A ,public B ,public C
    {
        public :
        void div(int x,int y)
        {
            int result = x/y;
            cout<<"\nDiv : "<<result;
        }
    };
    int main()
    {
        D obj;
        obj.sum(20,10);
        obj.sub(20,10);
        obj.multi(20,10);
        obj.div(20,10);
        return 0;
    }
    
 OUTPUT : Sum : 30
          Sub : 10
          Multi :200
          Div : 2
 

Welcome to AndroidATPoint.

back image                     next image