There are several problems with the design:
1:
As the previous poster points out, calculateGrade should take a reference to a student. If not, it gets it's own local copy. Changing the grade has no effect outside the scope of the function. To prevent this kind of "invisible" bugs, you should consider dissallowing copy operations on the student class, by adding these lines:
1 2 3
|
private:
Student(const Student&);
Student& operator=(const Student&);
|
2:
The comparison operator should take a const reference parameter, like this: (otherwise it won't work after the previous change)
|
bool Student::operator== (const Student &otherStudent )
|
3:
Overloading the comparison operator in this case might actually be a bad design. It's not obvious that student1 == student2 will compare the grades. It looks like the students themselves are compared. A function named e.g. compareGrades might be better.
4:
Friend classes like this should be avoided. It would be better to add a setter for the FinalGrade, and call it from the Teacher class.
5:
The student class has a redundancy issue, because the value of FinalGrade is dependant on the other grade values. I would consider making the FinalGrade a read-only attribute calculated on-the-fly, something like this:
1 2 3
|
int Student::getFinalGrade() const {
return static_cast<int>(Grade[0]+Grade[1]+Grade[2]+Grade[3]);
}
|
This would of course break the requirement that the teacher should calculate the final grade. So why not just move the function to the teacher class, and let it return the grade, like this:
1 2 3
|
int Teacher::calculateGrade(const Student &x) const {
return static_cast<int>(x.getGrade(0)+x.getGrade(1)+x.getGrade(2)+x.getGrade(3));
}
|
Then change the main function like this:
1 2 3 4 5 6 7 8 9 10 11
|
Student x, y;
x.setGrade(g1);
y.setGrade(g2);
Teacher z;
int fg1 = z.calculateGrade(x);
int fg2 = z.calculateGrade(y);
if(fg1==fg2)
cout << " Students x and y have the same grade!" << endl;
else
cout << " Students x and y don't have the same grade!" << endl;
|
Keep it simple! This gets rid of the overloaded operator, the friend relationship, the FinalGrade field, and it removes confusion.