0

This is the problem statement:

Design a base class called Student with the foll. 2 fields:- (i) Name (ii) Id. Derive 2 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 used to display the final result. Illustrate the usage of these classes in main.

#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 : 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: 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();
        }
};

This was my attempt, but this is quite heavily flawed.

Any solutions?

Any help is much appreciated.

Suyash Shetty
  • 513
  • 3
  • 8
  • 17
  • 1
    1. Pass the String as a const reference. 2. Use initialisation lists. 3. What is the question – Ed Heal Oct 19 '14 at 06:13
  • 1
    The call to `Student::display();` should not compile, because it is ambiguous: the `Results` class contains *two* instances of `Student` -- one through `Sports` and one through `Exam`. – cdhowie Oct 19 '14 at 06:14
  • @cdhowie I used a virtual inheritance to inherit `Student` now. It still doesn't compile. What should I do? – Suyash Shetty Oct 19 '14 at 06:17
  • Just so you know, this is *horrible* API design. Please tell your teacher to stop teaching bad habits. – o11c Oct 19 '14 at 06:25
  • @EdHeal I got it! I used an initialization list and virtually inherited the base class. No errors! Thank you, and cheers! – Suyash Shetty Oct 19 '14 at 06:27
  • @o11c I know. My teacher is very, very bad when it comes to teaching. **stackoverflow** FTW though. – Suyash Shetty Oct 19 '14 at 06:28
  • @SuyashShetty - Use also const and pass by reference – Ed Heal Oct 19 '14 at 06:28

2 Answers2

3

Student class must be virtual. Other wise there will be two copy's of student members in result. That will lead to an ambiguity.

1

You should use virtual inheritance when deriving Sports and Exam from class Student. This will ensure that only one Student instance will be created for a Results object.

#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();
        }
};
Laura Maftei
  • 1,863
  • 1
  • 15
  • 25