Write a c++ program to implement virtual inheritence

#include<iostream>

using namespace std;

     class student

     {

      protected:

      int roll_no;

      public:

      void get_no(int x);

      void put_no();

     };

     void student::get_no(int x)

     {

      roll_no=x;

     }

void student::put_no()

     {

      cout<<“the roll is\n”<<roll_no<<“\n”;

     }

     class test:public virtual student

     {

      protected:

      float marks1,marks2;

      public:

void get_marks(float a,float b);

void put_marks();

      };

  void test::get_marks(float a,float b)

  {

  marks1=a;

  marks2=b;

  }

  void test::put_marks()

  {

  cout<<“the marks of the students are\n”<<marks1<<marks2<<“\n”;

  }

  class sports:public virtual student

  {

  protected:

  float score;

  public:

    Void  get_score(float s);

              Void  put_score();

  };

  void sports::get_score(float s)

  {

  score=s;

  }

  void sports::put_score()

  {

  cout<<“the score of the student is\n”<<score<<“\n”;

  }

  class result:public test,public sports

  {

  public:

  float total;

  void display();

  };

  void result::display()

  {

  total=marks1+marks2+score;

  cout<<“the total of the student is\n”<<total<<“\n”;

    put_no();

    put_marks();

    put_score();

  }

  int main()

  {

  result r;

  r.get_no(2);

  r.get_marks(60.6,80.5);

  r.get_score(55.4);

  r.put_no();

  r.put_marks();

  r.put_score();

  r.display();

  return(0);

  }

Output:

The roll is

2

60.6 80.5

The marks of the students are

The score of the student is

55.4

The total of the student is

196.5

The roll is

2

60.6 80.5

The marks of the students are

The score of the student is

55.4

Leave a Comment

Your email address will not be published.