Archived

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

Data Encapsulation Methodology

This topic is 5649 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Forum I am trying to avoid picking bad habits in encapsulating my object trying to have minimum exposure to the outside world. Well, is it a good engineering practice to declare as many as possible private data members, and private functions versus public members? I saw lots of use for set and get functions(in java) but I also read somewhere that this is not a good engineering practice. If it is not, then how do you access private members through a public object? Many thanks Without problems, How can we ever evolve to better humans

Share this post


Link to post
Share on other sites
quote:
Original post by HappyToLearnC
I saw lots of use for set and get functions(in java) but I also read somewhere that this is not a good engineering practice. If it is not, then how do you access private members through a public object?



Having lots of Get() and Set() functions is usually a sign of a badly thought out class. Your classes should be sufficiently self contained so that they do not need all those accessors. If you have functionality that requires access to private members, try and move it inside the class whereever possible.

Sometimes you''ll have a situation where you need two different classes private members in the same place. In these cases, either use accessors, or use the friend keyword.



Share this post


Link to post
Share on other sites
Private members are implementation details.

You should think of your classes in term of properties and method.

A property is a characteristic of your class. Somethimes it can map one to one to a private data, but sometimes it does not. For example if you create a rectangle object. 2 of It's properties could be

Width
Height

So you would have 2 functions

GetWidth()
GetHeight()


Now if internaly you store a width and a height, well they map one to one. But you could store the four corners of the rectangle, which means that to get the width or the height you will have to do some calculations. In this case they don't map one to one.


Methods are simply actions your classes can do.


[edited by - Gorg on June 25, 2002 12:18:45 PM]

Share this post


Link to post
Share on other sites