12 Sept 2019

  • September 12, 2019
  • Amitraj
3.)  Multilevel - Inheritance

*In this type of inheritance, a derived class is created from another derived class.

     *If a class is derived from another derived class then it is called multilevel inheritance. So in C++ multilevel inheritance, a class has more than one parent class.

For example, if we take animals as a base class then mammals are the derived class which has features of animals and then humans are the also derived class that is derived from sub-class mammals which inherit all the features of mammals.


*A diagram that demonstrates multilevel inheritance is given below











// cpp program on multilevel inheritance.



#include<iostream>


using namespace std;


class student


{


 protected:


  int roll_number;


 public:


  void getnumber(int a)


  {


   roll_number=a;


  }


  void putnumber()


  {


   cout<<"roll number:"<<roll_number<<"\n";


  }


};


class test :public student


{


 protected:


  float sub1,sub2;


  public:


   void getmarks(float x,float y)


   {


    sub1=x;


    sub2=y;


   }


   void putmarks();


};


void test ::putmarks()


{


 cout<<"marks in sub1="<<sub1<<"\n";


 cout<<"marks in sub2="<<sub2<<"\n";


}


class result :public test


{


 float total;


 public:


    void display()


      {


   


        total=sub1+sub2;


           putnumber();


           putmarks();


            cout<<"total="<<total<<"\n";


}


};


int main()


{


 result r;


 r.getnumber(1);


 r.getmarks(55,77);


 r.display();


 return 0;


}


input/output:-


roll number:1


marks in sub1=55


marks in sub2=77


total=132




































Translate

Popular Posts