Quote:and I've seen several tutorials that go back and forth between private and public declarations, but that always seemed frivolous to me.
If you just mean that switching back and forth between public and private multiple times seems frivolous, then that's just a stylistic choice. There's no requirement to do it that way, and you can certainly just have one public section and one private section if you prefer.
If you're saying that use of public and private seems frivolous in general, then I take it you haven't yet been convinced of their usefulness :)
Quote:For that specific example of privatizing the screen surface, nothing would change other than possibly losing the ability to draw on the screen from some random function, but then what is the value of that restriction?
Part of the problem is that if you
can draw to the screen from some random function, then eventually you or someone else probably
will draw to the screen from some random function (most likely because it'll seem convenient at the time). If you take this approach throughout your code, you may end up with a convoluted mess whose interdependencies are difficult to keep track of. Modularity and reusability will suffer, and it'll become harder to make changes to the architecture.
Quote:I understand that not allowing certain chanegs to protect things is good style, but if the programmer intends to change whatever it is that they are being blocked off from wouldn't they simply find a way to do so?
Ok, how would you do it? How would you go about modifying a private member variable from outside the class?
Also, keep in mind that even in real life there are plenty of examples of safeguards that can be circumvented if desired, but nevertheless provide increased safety and security. A lot of the safeguards we put in place (both in real life and in software development) aren't intended to
prevent people from doing things (which is often impossible), but rather to
deter people from doing things.
Quote:Also, on direct variable setters/getters, isn't having a private variable, 'x' as well as public 'get_x(){return x;' and 'set_x(arg){x=arg;}' functions essentially the same as making 'x' public to begin with?
Yes, many would argue that having a getter/setter pair that doesn't do anything but 'get and set' has little point, and you might as well make the variable public. Furthermore, getter/setter pairs are generally discouraged in object-oriented design anyway, as they tend to expose implementation details and make the interface less abstract.