Jump to content
  • Advertisement

Archived

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

clonemaker

private parts

This topic is 6417 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

Encapsulation in C++. Is it nessesary, and do you use it? I''m talking about the physical act of using private and protected keywords to hide implimentation and data.

Share this post


Link to post
Share on other sites
Advertisement
public and private are formalities that help to insulate parts of the class that should not be accessable from the outside. There is not magic involved and is compleatly unnessary from a pure programming point of view.

Basicly to use them properly, remember a few things. Public keyword should be used for generic operations on your class, it should also be used on any data members you wish to expose ( the fewer the better for encapsulation ). Private should be used for any function that deals with internal mantinance of the object data or state.

Good luck

Share this post


Link to post
Share on other sites
I understand what the purpose of the keywords are, and what the purpose is to use them. I was just wondering how many people find it nessesary to program this way.

Share this post


Link to post
Share on other sites
I always try and use them. I also do alot of programming in java where they are more integral with the system. In terms of what use I really get out of them I perfer abstarct base classes to define functionality ( interfaces in java ). I then can design my functional classes in whatever way I want as long as it''s subclassing my abstract definitions.

Most of my stuff is based on the factory model of instansiation, and the MVC event modeling. Makes the stsem highly modular and configurable at almost any level.

Share this post


Link to post
Share on other sites
Data Hiding is very necessary for some forms of software engineering. Specifically, if you''re writing an extensible, flexible code library, you need to be able to rely 100% on inheritance mechanisms, and this only works if you have fully hidden data.
For more information, you could read a book on Design Patterns.


People might not remember what you said, or what you did, but they will always remember how you made them feel.
Mad Keith the V.

Share this post


Link to post
Share on other sites
quote:
Original post by MadKeithV

Data Hiding is very necessary for some forms of software engineering. Specifically, if you''re writing an extensible, flexible code library, you need to be able to rely 100% on inheritance mechanisms, and this only works if you have fully hidden data.
For more information, you could read a book on Design Patterns.



Okay, would you consider data-hiding as just a fundamental part of writing any class, or just something you impliment when needed?

Share this post


Link to post
Share on other sites
quote:
Original post by clonemaker

Okay, would you consider data-hiding as just a fundamental part of writing any class, or just something you impliment when needed?


I would consider it fundamental. When I design code, I do it in blocks. I then consider how the blocks interact with each other. I am not concerned on how the blocks do their jobs. In this way I can "hide" away data that is not really necessary outside that block. I can then easily test each block. UML programs are great for this It also makes it much easier to switch blocks around and insert better ones.


--------
Andrew

Share this post


Link to post
Share on other sites
I have always been a fan of allowing little to no access to a class''s member variables. Sometimes, this results in having to create many access routines (mostly inline) to access the data indirectly. This approach has many benefits and drawbacks.

The primary reason I do this is that I am strong advocate of contract programming and implementation isolation. When done right, it allows the programmer to make significant changes to a class without having to worry about who is accessing the class as long as the public method functionality (the contract) remains constant. Having a constant and well defined public method functionality also helps in the testing phase after internal implementation of a class has been changed.

Another benefit is that it helps to document who is accessing what functions. If a method is private, you know that it is not being accessed by anyone other than the class in question. Thus, it can be modified without having to worry about some obscure piece of software accessing the routine.

Of course, like all tools, it isn''t going to save the world. If not used properly, it can make the code harder to work with.

Tim

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!