# School - Semester - Course - Student ```cpp #include<iostream> #include<vector> #include<string> using namespace std; class School; class Semester; class Course; class Student; class School{ private: string _name; public: School(){ _name = "The Umbrella Academy"; } School(string name){ _name = name; } ~School(){ // Nothing to do here!!! } void print(){ cout << "School name: " << _name << endl; } }; class Semester{ private: string _name; School *_school; public: Semester(){ _name = ""; _school = NULL; } Semester(string name, School * school){ _name = name; _school = school; } ~Semester(){ // pointer *_school is shared among object, do not delete here } void print(){ cout << "Semester name: " << _name << endl; if(_school != NULL){ _school->print(); } } }; class Course{ private: string _code; string _name; int _nStudents; int _maxStudents; Semester *_semester; // if you want to keep track of enrolled students, add a vector<Student*> here vector<Student*> _enrolledStudents; public: Course(){ _code = ""; _name = ""; _nStudents = 0; _maxStudents = 100; _semester = NULL; } Course(string code, string name, int maxStudents, Semester *semester){ _code = code; _name = name; _nStudents = 0; _maxStudents = maxStudents; _semester = semester; } ~Course(){ // pointer *_semester is shared among object, do not delete here } void print(){ cout << "Course code: " << _code << endl; cout << "Course name: " << _name << endl; cout << "Number of students: " << _nStudents << endl; cout << "Max number of students allowed: " << _maxStudents << endl; if(_semester != NULL){ _semester->print(); } } // if you want to keep track of enrolled students, add check function here bool isAllowedToEnroll(Student *student){ if(_nStudents >= _maxStudents) return false; else return true; } string getCode(){ return _code; } bool enrollAStudent(Student *student){ if(isAllowedToEnroll(student)){ _enrolledStudents.push_back(student); _nStudents++; return true; } else{ return false; } } }; class Student{ private: string _code; string _name; School *_school; vector<Course*> _enrolledCourses; public: Student(){ _code = ""; _name = ""; _school = NULL; } Student(string code, string name, School *school){ _code = code; _name = name; _school = school; } ~Student(){ // Do not delete _school and _enrolledCourses[i] } void print(){ cout << "Student code: " << _code << endl; cout << "Student name: " << _name << endl; if(_school != NULL){ _school->print(); } cout << "Number of enrolled courses: " << _enrolledCourses.size() << endl; for(int i = 0; i < _enrolledCourses.size(); i++){ _enrolledCourses[i]->print(); } } void enrollACourse(Course *course){ bool hasEnrolled = false; for(int i = 0; i < _enrolledCourses.size(); i++){ if(_enrolledCourses[i]->getCode() == course->getCode()){ hasEnrolled = true; } } if(hasEnrolled == false){ bool ans = course->enrollAStudent(this); if(ans){ _enrolledCourses.push_back(course); cout << "Enroll successfully" << endl; } else{ cout << "Enroll failed" << endl; } } else{ cout << "Enroll failed" << endl; } } }; int main(){ School *hcmus = new School("VNU-HCMUS"); Semester *summer2019 = new Semester("Summer 2019", hcmus); Course* oop = new Course("CTT006", "OOP", 100, summer2019); Course* integral2 = new Course("MTH00006", "Integral 2", 90, summer2019); Course* linear = new Course("MTH00008", "Linear Algebra", 80, summer2019); Course* physics1 = new Course("PHY00005", "Physics 1", 70, summer2019); Student *htthanh = new Student("18127001", "Ho Tuan Thanh", hcmus); htthanh->enrollACourse(oop); // allowed htthanh->enrollACourse(integral2); // allowed htthanh->enrollACourse(physics1); // allowed htthanh->enrollACourse(integral2); // not allowed, has enrolled Student *nkhuy = new Student("18127002", "Nguyen Khac Huy", hcmus); nkhuy->enrollACourse(oop); // allowed nkhuy->enrollACourse(linear); // allowed nkhuy->enrollACourse(integral2); // allowed nkhuy->enrollACourse(physics1); // allowed nkhuy->enrollACourse(oop); // not allowed, has enrolled delete htthanh; delete nkhuy; delete physics1; delete linear; delete integral2; delete oop; delete summer2019; delete hcmus; return 0; } ```