There is something that I've been wondering for a while and I hope you guys can share your thoughts on the matter. I first discovered this problem around a half a year ago when I programmed a robot that was controlled by a PLC. In structured text I used get-functions to return an object from a class. This way I could do anything I wanted to a member/variable of that class (with the use of getters and setters). This saved me a lot of work, since I didn't have to make seperate functions to get something out of a deeper lying object.
At the moment I am programming something in Qt C++ and yet again I face this issue. I am not sure if I am being completely clear right now (I am not a native English speaker and am also not that competent in coding jargon), so I will try to make it clear with an example. Let's say i have an array/vector with multiple objects in it of the class School. Each object has it's own array with classes (I mean a class with students, not a programming class), each class has it's own array with students and they of course have a name, student id, age, etc.
Now I would like to get the name of a random student. In Qt C++ I would do it something like this:
QString randomName = schoolVector.at(randomNumber)->getClassVector().at(anotherRandomNumber)->getStudentVector().at(yetAgainARandomNumber)->getName();
You can see that I made some get-functions to return an object in a certain class. This way it makes it easy for me to manipulate an object or variable in a class. Here is the reason I am asking this question: Is this the way to go? In school they teached me that the use of getters and setters is important so that you cannot manipulate a variable when you don't want to or you don't want anyone else to. But when I use a getter to return a class in the classVector I can still manipulate anything in there.
Can anyone tell me common practice and also if it is normal to use for-loops to find a certain object in a vector? I would really appreciate it!