Multilevel-Inheritance-in-C++ .

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


In multilevel inheritance the derived class inherit from base class and it is also act as a parent for other class .


CSharp single inheritance image

In above image the class B is derived from the A class and act as a parent for the class C .

Multilevel Inheritance Demo :

Example : Write a program that demonstrate the multilevel 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 A
    {
        public :
        void sub(int x,int y)
        {
            int result = x - y;
            cout<<"\nSub : "<<result;
        }
    };
    class C : public B
    { 
        public :
        void mul(int x,int y)
        {
            int result = x * y;
            cout<<"\nMulti : "<<result;
        }
    };
    int main()
    {
            C obj;
            obj.sum(20, 10);
            obj.sub(20, 10);
            obj.mul(20, 10);
            return 0;
    }
    OUTPUT : Sum  : 30
             Sub  : 10
             Multi: 200  
    

Welcome to AndroidATPoint.

back image                     next image