Inheritance-Virtual Base Class( Example-Done by me)

 #include <iostream>

using namespace std;


class student{

  protected:

  int roll_number;

  string name;

  public:

  void get_student_data(int r,string n)

  {

      roll_number=r;

      name=n;

  }

  void display_student_data()

  {

      cout<<"The roll number is "<<roll_number<<endl;

      cout<<"The name of student is "<<name<<endl;

  }

  

};


class academics:virtual public student{

    protected:

    int score=0;

    public:

    void set_test_score(int m,int c,int p)

    {

        score=m+p+c;

    }

    void display_test_score()

    {

        cout<<"The total score is "<<score<<endl;

        float average=float(score)/3;

        cout<<"The average is "<<average<<endl;

    }

};


class sports: virtual public student{

    protected:

    char point;

    public:

    void set_sports_score(char sc)

    {

        point=sc;

    }

    void display_sports_point()

    {

        cout<<"The points scored in sports is "<<point<<endl;

    }

};


class result: public academics,public sports{

    public:

  

    void display_result()

    {

    display_student_data();

display_test_score();

display_sports_point();

cout<<"The student has done overall a good job. Best of luck!!"<<endl;

    }

    

};


int main() {

result r1;

r1.get_student_data(1265,"Peeyush Mishra");

r1.set_test_score(96,95,98);

r1.set_sports_score('A');

r1.display_result();


return 0;

}


Comments

Popular posts from this blog

Hackerrank-Accessing Inherited Functions problem-Inheritance

Inheritance- Code with Harry-Lecture 41 solution (Multiple inheritance)