How to implement the operator for the class:

Department operator+=(Course &); 

The class itself:

 using namespace std; class Student; class Course; class Department { // overload input output streams friend ostream &operator<<(ostream &, const Department &); friend istream &operator>>(istream &, Department &); private: string name; long id; Course** coursesOfDepartment; // list of pointers course in this department Student** studendOfDepartment; // list of pointers studetns of this department Student** badStudentsOfDepartment; // list of point students by the course points < 65 public: static int departmentsCounter; // count num of elements Department(); // set get block void setId(long); void setName(string); string getName() { return name; } long getId() { return id; } // overload block Department &operator=(const Department &); // instead copy constructor bool operator>(const Department &) const; // check if count of students greater than count of students in another department Department operator+=(Course &); // add new course to to the department list Department operator+=(Student &); // add new student to to the department list ~Department(); }; 

In the operator, you need to create a new object of the Course class and add it to the Course** coursesOfDepartment array Course** coursesOfDepartment .

  • 2
    And where does the main , sorry? - VladD
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

Apparently under this:

 Course** coursesOfDepartment; // list of pointers course in this department Student** studendOfDepartment; // list of pointers studetns of this department Student** badStudentsOfDepartment; // list of point students by the course points < 65 

you mean dynamic arrays of corresponding pointers. In this case, you still lack some data, namely the current length of each of these arrays.

Then the algorithm for adding a new element to the course array will be as follows:

  1. allocate a section of memory for an array of courses = length (the current length of the array of courses is +1);
  2. move all objects from the original course array to this new array;
  3. release the memory that occupied the source array;
  4. add a copy of the new course to the end of the new array.

In order not to fence the whole garden using / freeing memory, the easiest way to use the standard class is std::vector . Then your code becomes very simple:

 std::vector<Course> coursesOfDepartment; // list of pointers course in this department std::vector<Student> studendOfDepartment; // list of pointers studetns of this department std::vector<Student> badStudentsOfDepartment; // list of point students by the course points < 65 

And your operator will be like this:

 Department& Department::operator+=(const Course& course) { courcesOfDepartment.push_back(course); return *this; } 

PS: My personal opinion is that operator+= in this case does not fit in its semantics. I would use the function instead:

 void Department::addCourse(const Course& course) { courcesOfDepartment.push_back(course); } 
  • @roma, notice that I changed the signature of your operator to Department& operator+=(const Course& course); . Those. We take the argument for a constant (!) link - because we do not plan to change it, but return not a new Department object, but a reference to the current one. - aleks.andr