I have the following student class with two similar function- One is static and one is not.
class Student { public: Student(std::string name_ , int const id_); virtual ~Student(); void addGrade(int const grade2add);//grade above 100 void removeGrade (int const grade2remove); //update maxGrade void print(); //id, name, grades int getStudentMaxGrade(); static int getMaxGrade(); private: std::string name; //max 20 chars int const id; //5 digits, only digits std::vector<int> grades; float avg; static int maxGrade; }; The static int maxGradeis initialize with 0.
I implement the function:
int Student::maxGrade = 0; int Student::getStudentMaxGrade() { return *max_element(grades.begin(), grades.end()); } I'm not sure how to implement the static function. I tried:
int Student::getMaxGrade() { maxGrade= *max_element(grades.begin(), grades.end()); return maxGrade; } But it doesn't work (doesn't compile)
I'm not sure how to implement the ststic functionThat rather depends on what you want it to do, which was never adequately explained. Recall that a static member is not tied to a particular object (that's the whole point ofstatic). In other words, it cannot refer to any particular individual student.Student.Studentis a class, not an object. If you haveStudent s;,sis an object;Studentisn't.