patterncppMinor
Student Classroom class
Viewed 0 times
classroomstudentclass
Problem
I'd like to know if the following class design is good or bad:
Maybe the
#include
#include
#include
#include
using namespace std;
struct Student {
string Firstname;
string Surname;
string IDNumber;
int Age;
};
class Exam {
string ExamNo;
int Questions;
int PassScore;
string Subject;
map 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 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::const_iterator it = students.begin();
it != students.end();++it){
cout Firstname Surname 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 Solution
First of all I'd like to say that you did not provide guidelines, so I'm not completely sure what is required and what not.
ClassRoom
I would rename this to
Teachers
Your
If I were you I would create a
Instead of
Constructors
Instead of using
create a constructor like:
This is more concise and reads easier.
You might take a look into using an abstract factory pattern in order to create your student/teacher objects.
Style
Normally one does not start member variables with a upper case. perhaps take a look at this
apart from that you seem to do pretty well, style wise. But I'm not a C++ expert, I'm certain other can help you further with this.
ClassRoom
I would rename this to
Course, since it's not really related to the room where it takes place. A course probably has a instance variable that identifies the location of the course "Building A4.4" for instance. It indeed holds a vector of students, a name. But I wouldn't store a class average, instead provide a method for calculating that getCourseAverage() for example. Perhaps also store the Teacher object that gives this course.Teachers
Your
Teacher class seems not logically constructed, it is a person.If I were you I would create a
Person class and inherit your Teacher and Student class from it. A Person has a firstname, lastname , age. Probably they both have an id value. You can even go a step further and make a Employee class, that has Person as base class and of which Teacher inherits.Instead of
string ClassRoom, I would use composition and have it contain a list of Course object (courses that are given by that teacher).Constructors
Instead of using
a.Firstname = "John";
a.Surname = "Doe";
a.IDNumber = "8123295073081";
a.Age = 30;create a constructor like:
CreateStudent(name, surname, id, age);This is more concise and reads easier.
You might take a look into using an abstract factory pattern in order to create your student/teacher objects.
Style
Normally one does not start member variables with a upper case. perhaps take a look at this
apart from that you seem to do pretty well, style wise. But I'm not a C++ expert, I'm certain other can help you further with this.
Code Snippets
a.Firstname = "John";
a.Surname = "Doe";
a.IDNumber = "8123295073081";
a.Age = 30;Context
StackExchange Code Review Q#87724, answer score: 2
Revisions (0)
No revisions yet.