Reputation: 161
I have the following student class, which creates a student, and then allows me to store that student in a students class vector. I want to now have a Courses vector which allows each student to have it's own grouping of courses that they attend. I want these courses to point to the student that owns them so that when a student is removed from the list of students, their courses are also deleted with them. Ideally, I would like to have this courses vector as a private member of the student class, such that these courses can only be accessed/altered when the specific student that owns them is specified.
Main:
#include <iostream>
#include <string>
#include "student.h"
#include "students.h"
using namespace std;
int main(){
Students stuList;
Student* bob = new Student ("Bob" "Jones" 10000909);
stuList.add(bob);
return 0;
}
Student h:
#include <ostream>
#include <string>
class Student {
public:
Student::Student(const string & FName, const string & LName, const int ID);
private:
string first;
string last;
int id;
};
students h:
#include <ostream>
#include <vector>
#include "student.h"
#include <string>
using namespace std;
class Students {
public:
Students();
void add(Student & aStudent);
private:
vector<Student*> collection;
};
I have been thinking of ways to accomplish this for a while now and I am drawing a blank. Any suggestions/tips would be highly appreciated.
Upvotes: 1
Views: 110
Reputation: 1522
In the Student class you can add one vector containing the courses owned by the student and additionally one vector for the group of courses attended:
class Student {
...
vector<Course*> ownedCourses;
vector<Course*> attendedCourses;
};
Then in your class Courses you will need a vector containing all the attendants of this course:
class Course {
...
vector<Student*> attendants;
};
If you now remove a stundent from your list, you will also have the earse all courses owned by him from the lists of the other students:
vector<Course*> ownedCourses = studentToRemove.getOwnedCourses();
for (const Course* course : ownedCourses)
{
vector<Student*> attendants = course->getStudents();
for(const Student* student : attendants) {
student->removeAttendedCourse(course);
}
}
Upvotes: 2