samedi 18 octobre 2014

How can I instantiate private data members of two base classes by defining a constructor of a derived class?


Vote count:

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.



asked 1 min ago







How can I instantiate private data members of two base classes by defining a constructor of a derived class?

Aucun commentaire:

Enregistrer un commentaire