Archived

This topic is now archived and is closed to further replies.

Android_s

Question about classes...

Recommended Posts

Android_s    164
Ok, so here''s the "problem". What exactly are the private: thingie in classes for? I know it''s to make only the class/friend to be able to access it, but for what purpose? This thing really fries my mind. I can''t come up with any good example where it is necesary. Anyone care to give me some?

Share this post


Link to post
Share on other sites
micepick    122
It's to make sure that other programmers (or you yourself) don't touch the inner workings of your classes. If you design your classes so that only the interface (the part the class's users have to know about and use in their programs) is public and all the little details are private, it is very easy to change the classes without breaking any existing code.

If you suddenly decide to make your class do what it does in a completely different (better) way, you can just change all the private stuff, but make sure it still works with the things you made public. That way, you can just change the one class and any other code that's using it will still work. If you have code directly accessing stuff that should be private, you may end up having to rewire random parts of your program when you change a class.

This whole idea of data protection in classes is called 'encapsulation'.

[edited by - micepick on April 30, 2003 11:24:50 PM]

Share this post


Link to post
Share on other sites
acraig    471
A good example is a Microwave. Think of the entire microwave as the class. You build it with a keypad ( ie your public stuff ) and you do all the internal wiring ( ie your private stuff ). Now when you have to use the microwave you deal with the public stuff ( ie Microwave::Cook( int time ), Microwave::OpenDoor() ).

This means you cannot mess up something by playing with the internal wiring and really cause a problem. It hides or encapsulates, as a previous poster said, all the messy interal stuff that a microwave needs to cook something.

Another benifit of this is that if you figure out a brand new way of designing your internal system it is totally transparent to anything using that class. So you can radically change Microwave::Cook() to do it totally differently. But to any thing that is using this class it is totally transparent. All it knows is the Cook() function.


Private data inside a class make that class like a black box. You have a set of inputs and a set of outputs. As long as the behaviour of how the outputs are related to the inputs is the same there can be many different implementations of the internal data.



-------
Andrew
PlaneShift - A MMORPG in development.

Share this post


Link to post
Share on other sites
OrangyTang    1298
To extend on this, it also means you can make the class more foolproof - openDoor() could check you''ve already switched it off (and either switch it off, do nothing or something else..). Each object should be in a consistant internal state at all times.

Share this post


Link to post
Share on other sites
Android_s    164
Ahh, thanks! I belive I understand what private: is good for. That thing about rewriting parts of the class without having to play Sherlock Holmes everytime sounds really useful. I don''t know how many times I have had to rewrite a whole (newbie)program when i changed one little thing in it

Share this post


Link to post
Share on other sites