Answer to Question #249512 in C++ for Elena

Question #249512

(Multipath Inheritance) Design a base class called Student with two

fields:- (i) Name (ii) roll number. Derive two classes called Sports and

Exam from the Student base class. Class Sports has a field called s_grade

and class Exam has a field called e_grade which are integer fields. Derive a

class called Results which inherit from Sports and Exam. This class has a

character array or string field to represent the final result. Also it has a

member function called display which can be be used to display the final result.Illustrate the usage of these classes in main.


1
Expert's answer
2021-10-10T16:56:39-0400
#include<iostream>
#include<string>
using namespace std;

class Student
{
    string name;
    int id;

    public:
        Student(string n, int a)    {name = n; id = a;}
        void display()  
        {
            cout<<"Student name: "<<name;
            cout<<"\nStudent I.D.: "<<id;
        }
};

class Sports : virtual public Student
{
    int s_grade;

    public:
        Sports(string n, int a,int s):Student(n,a)  {s_grade = s;}
        void display()  
        {
            cout<<"\nSports grade: "<<s_grade;
        }
};

class Exam: virtual public Student
{
    int e_grade;

    public:
        Exam(string n, int a,int e):Student(n,a)    {e_grade = e;}
        void display()  
        {   
            cout<<"\Exam grade: "<<e_grade;
        }
};

class Results: public Sports, public Exam
{
    string result;

    public:
        Results(string n,int i, int s, int e):Sports(n,i,s):Exam(n,i,e) {}
        void display()
        {
            Student::display();
            Sports::display();
            Exam::display();
        }
};

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS