Write a c++ program to implement multilevel inheritance

#include<iostream>

using namespace std;

 class student

 {

  protected:

  int rollno;

  public:

  void get_number(int n);

  void show_number ();

};

 void student::get_number(int n)

 {

  rollno=n;

 }

 void student::show_number()

 {

        cout<<“\n the roll number \n”<<rollno;

 }

class test:public student

{

protected:

          float sub1;

          float sub2;

public :

    void get_marks(float m1,float m2);

    void show_marks();

    };

 void test::get_marks(float m1,float m2)

 {

  sub1=m1;

  sub2=m2;

 }

void test::show_marks()

 {

  cout<<“\nThe marks of subject 1 \n”<<sub1;

  cout<<“\nThe marks of subject 2 \n”<<sub2;

 }

 class result:public test

 {

  protected :

  float r;

  public :

  void calc();

  void show_cal();

};

void result::calc()

 {

  r=sub1+sub2;

 }

  void result::show_cal()

  {

  cout<<“\nTotal marks = “<<r<<“\n”;

}

int main()

{

result R;

R.get_number(111);

R.show_number();

R.get_marks(45,87);

R.show_marks();

R.calc();

R.show_cal();

return 0;

}

Output :

The roll no

111

The marks of the subject1

45

The marks of the subject2

87

Total marks = 132

Leave a Comment

Your email address will not be published. Required fields are marked *