+ 2

real life scenario for private access specifier

hi guys i am doing the c++ tutorials and i am a bit confused in the private specifier topic and want a fully explained real life scenario may be with a real life experience.....your answers are really appreciated

4th Sep 2017, 5:58 PM
Ameen Bari
Ameen Bari - avatar
2 Respuestas
+ 2
Collaborative projects editing files, hiding private 'things' from unwanted or untrusted eyes. Function gathering all publically available classes in the same file and displaying in a list, for a developer to tinker with. Just for fun, hide everything from a file reader so nothing can be read. Secret file, in concept, but not in approach.
4th Sep 2017, 6:07 PM
ghostwalker13
ghostwalker13 - avatar
+ 1
I will use the vector class as an example. The vector class contains an attribute (private) that is an array of elements of type we chose Example : vector<int> v; If we could modify or access this attribute, I could very well do something like this : delete v.array; With this single line, my vector become broken and I can't use it anymore without having a Segmentation Fault as the memory of the array is now freed without the class to be able to know it. That is why we use private attribute : for data hiding. With the array untouchable outside of the class, we are sure that the object (here vector) will always work the way we defined it to.
4th Sep 2017, 6:03 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar