0

I want to keep track of the number of students in my system so, My idea was to make a static datamember in the "StudentController" class called "_numOfStudents" and increment it with the Student's constructor but it didn't work so, I moved it into the "Student" class and made that when a Student object is created the number increment by the help of the constructor. The problem is: isn't it not the Student class's business to know how many students are there thus breaking the principle of least privilege. what can I do better to keep track of the student objects' count.

    Student(string firstName, string lastName, int age,vector<float>&subjects)//Student conctructor
{
    SetFirstName(firstName);
    setLastName(lastName);
    SetAge(age);
    SetMarks(subjects);
    this->m_id++;
    StudentController::_numberOfStudents++;
}



    class StudentController//this is where i declared the static data member
{
private:
list<Student> _students;
public:
    static int _numberOfStudents;

    StudentController() {};
    StudentController(list<Student>& st) :_students(st) {};

        }
    }   
};

int StudentController::_numberOfStudents = 0;
Tarek Moh
  • 13
  • 2
  • 1
    What do you mean by "didn't work"? Can you show the code you tried that didn't work? – Christian Halaszovich Dec 08 '21 at 12:05
  • ` Student(string firstName, string lastName, int age,vector&subjects) { SetFirstName(firstName); setLastName(lastName); SetAge(age); SetMarks(subjects); this->m_id++; StudentController::_numberOfStudents++; }` error num1 :'StudentController' is not a class or namespace name. error num2 :'_numOfStudents' undeclared identifier – Tarek Moh Dec 08 '21 at 12:11
  • I like the way you are thinking about what you are doing. But in this case I would say having a static member isn't bad. And this technique is often used to give classes unique identifiers too. If you want to model a bit more, then think of a name for a class that models a group of students. And then you can add/remove students to that class and let it do the counting. – Pepijn Kramer Dec 08 '21 at 12:13
  • 1
    from [wiki](https://en.wikipedia.org/wiki/Principle_of_least_privilege) " [...] that in a particular abstraction layer of a computing environment, every module (such as a process, a user, or a program, depending on the subject) must be able to access only the information and resources that are necessary for its legitimate purpose." If it is the purpose of the `StudentController` to count the `Student` instances then the minimum information it needs is the number of `Student` instances ;). Don't overthink principles, they are made to help you not to prevent you from using the simple solution – 463035818_is_not_an_ai Dec 08 '21 at 12:17
  • 5
    Why does the "controller" need to keep separate track of the number of student objects that have been created? I believe `_students.size()` is the relevant number. – molbdnilo Dec 08 '21 at 12:18

2 Answers2

1

When you try to do StudentController::_numberOfStudents++; in the constructor, the StudentController class is not yet defined, therefore the compiler doesn't know about that class and its static member.

1

Maybe keeping track of the students shouldn't be the role of the Student class itself. Instead it should be the role of a separate Classroom object:

struct Student;
struct Classroom {
    void add(Student&) {
        m_count++;
    }

    size_t count() const { return m_count; }
private:
    size_t m_count{0};
};

struct Student {
    Student(Classroom& classroom) {
        classroom.add(*this);
    }
};

int main()
{
    Classroom classroom;

    Student alice{classroom};
    Student bob{classroom};

    assert(classroom.count() == 2);
}
m88
  • 1,968
  • 6
  • 14