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



































Related Posts:

  • this pointer in C++ programming this pointer in C++ programming C++ uses a unique keyword called this to represent an object that invokes member function. this is a pointer that points to the object for which this function was called. // C++ program on … Read More
  • Order of Constructor and Destructor Call with Inheritance in C++ We have presented you the sequence in which constructors and destructors get called in single and multiple inheritance. *Constructor and destructor in single inheritance: ->Base class constructors are called first and t… Read More
  • Virtual base class in C++ Virtual base class in C++ -> An ambiguity can arise when several paths exist to a class from the same base class. This means that a child class could have duplicate sets of members inherited from a single base class. -&… Read More
  • Hybrid - Inheritance in C++ 5.) Hybrid - Inheritance   What is Hybrid ? The inheritance in which the derivation of a class involves more than one form of any inheritance is called hybrid inheritance. Basically C++ hybrid inheritance is&nb… Read More
  • Types of Polymorphism in C++ (Compile time Polymorphism) 1) Compile time Polymorphism:- Linking of function call to a function definition which is known as compile time polymorphism or static binding or static linking or early binding. Compile time Polymorphism Example In t… Read More

Translate

Popular Posts