I'd like to know if the following class design is good or bad:
#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
struct Student {
string Firstname;
string Surname;
string IDNumber;
int Age;
};
class Exam {
string ExamNo;
int Questions;
int PassScore;
string Subject;
map <string,string> ExamTaken;
public:
Exam(string exam,int Q , string s) : ExamNo(exam), Questions(Q), Subject(s) {};
void TakeExam(const string S) { ExamTaken[S] = ExamNo; };
};
class Teacher {
int studentscore;
string ClassRoom;
public:
bool StudentPassed ();
void setScore (int studentsc) ;
};
class ClassRoom {
vector<Student> students;
string ClassName;
double ClassAverage;
public:
ClassRoom(string classname) : ClassName(classname) {};
void AddStudent(Student &s) { students.push_back(s); };
void ListStudents() const;
int StudentCount() const { return students.size(); };
void StudentLookup () const;
string getClassName() const { return ClassName; };
};
// list all students in class
void ClassRoom::ListStudents() const {
/* old c++ style iterator
for (vector<Student>::const_iterator it = students.begin();
it != students.end();++it){
cout << it->Firstname << " , ";
cout << it->Surname << " , ";
cout << it->Age;
cout << endl;
}
*/
// c++11
for (auto &x : students){
cout << x.Firstname << ",";
cout << x.Surname << ",";
cout << x.Age << endl;
}
}
int main(){
Student a,b;
ClassRoom c("CS1002");
Exam e("A002", 200 , "Biology");
a.Firstname = "John";
a.Surname = "Doe";
a.IDNumber = "8123295073081";
a.Age = 30;
e.TakeExam(a.IDNumber);
b.Firstname = "Jane";
b.Surname = "Doe";
b.IDNumber = "8123225073281";
b.Age = 27;
c.AddStudent(a);
c.AddStudent(b);
c.ListStudents();
cout << "There are "
<< c.StudentCount()
<< " students in the classroom "
<< c.getClassName() << endl;
}
Maybe the Teacher
class should be removed and replaced with a database
class, that keeps a record of grades for each student.
I was thinking of changing the student class and adding object composition, for the exam
class. I'm not sure how to do it properly yet, so I'm still experimenting with that.