I'm working on something and it just seems a lot easier to keep the members public so I can get to them from everywhere else in the program.
What are the advantages of making object members private?
I'm working on something and it just seems a lot easier to keep the members public so I can get to them from everywhere else in the program.
What are the advantages of making object members private?
Encapsulation!
nuff said.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
i don't get it... why would you take control away from your self?
Ok see that's the thing. I don't think I really understand the purpose of private. To me it just seems like private makes it harder to get to the members from other parts of the program.
What do you mean take control away from myself?
I'm not doubting that there's a good reason for private, I just don't think I understand it properly.
"Why not go mad?"
Suppose you have a class with 50 functions but only 10 are going to be directly used outside of the class, the other 40 are simply 'helper' functions. By making those 40 private when someone else uses the class they can see exactly what can be done with the class, only the useful stuff.
Same applies to variables. You have to think about when the classes are going to be reused, so you want as little access as possible to the implementation details. This way another user of your class has almost no chance of messing it up and it makes it simple to use.
I really hope thats clear...
Couldn't think of anything interesting, cool or funny - sorry.
If I have a class "time", I store the time in a set of 3 integers: hours, minutes and seconds. Suppose I made these integers public, you could, or the program could easily modify the hours to exceed 12. By making the variable private, and using set and get functions you can ensure that the values being passed to your private data members are valid.
Code:bool time::sethours(int iHour) { if (iHour < 1 || iHour > 12) { m_iHours = 0; return 0; } else { m_iHours = iHour; return 1; } }
Good example Eibro. Its all about when people are going to reuse your classes, they should be able to use them without knowing the details of how they work.
Couldn't think of anything interesting, cool or funny - sorry.
its not only for code reuse. People sometimes hear that and think, "I'm the only one who's going to edit this so I don't need to do it". There is a matter of cleanliness. old style C is sloppy. referencing variables across modules is sloppy. Before you know it you'll have a program that is impossible to edit without breaking something you had not expected to change. private variables are very simply things that outside objects should not be able to touch. If they REALLY want to touch it, well let's just say there should be more thought in the structure of the code.
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
Thanks guys! I got it now.
Once again I appreciate the help!
"Why not go mad?"