Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

private vs public

Status
Not open for further replies.

butthead

Programmer
Feb 24, 2002
545
US
in the class definition below I have chosen to declare certain items as private versus public. what does one use as the basis of deciding what is public and what is private.
I know that private protects certain elements from outside sources but the only outside source I can think of is the programmer himself. Though I am sure there are other instances were the variable or other item can be accessed outside my code. Other than creating exceptionally bullet proof code I sometimes wonder why use private at all.

I would be grateful for someone to recommend a source that would help me make the decisions neccessary

Code:
class MHDBTable
 {
     // For all functions with a return type of int, the value
     // of 0 is for no error and 1 is for an error (unspecified).

     private:

         bool active;
         int _Insert;
         int _DataOffset;
         int slack;
         int _RecNo;
         char *_Record;
         char *_TableName;
         char *Blank;
         char *buff;
         int _TableVersion;
         int _MDBEVersion;
         int _RecordLength;
         char header [HEADERLENGTH];
         void __fastcall HeaderInitialize (void);

     public:
         __fastcall MHDBTable ();
         __fastcall ~MHDBTable (); 

         vector<string> Records;

         field *Fields;

         int __fastcall Open (void);
         int __fastcall Close (void);
         int __fastcall CreateTable (void);
         bool __fastcall Active (void);

         char* __fastcall TableName (void);
         int __fastcall TableName (char *str);

         char* __fastcall Record (void);
         int __fastcall Record (char *string);
         int __fastcall RecordLength (void);
         int __fastcall RecNo (int recno);
         int __fastcall RecNo (void);

         int __fastcall First (void);
         int __fastcall Next (void);
         int __fastcall Prior (void);
         int __fastcall Last (void);
         int __fastcall Insert (void);
         int __fastcall Delete (void);
         int __fastcall Post (void);

         int __fastcall InsertStatus (void);
         int __fastcall InsertStatus (bool stat);
         int __fastcall Pack (void);
         int __fastcall Slack (void);

         int __fastcall DataOffset (void);
         char __fastcall Version (void);
         char __fastcall MDBEVersion (void);
  
         int __fastcall LoadRecord (field *fields, char *record, int recordlength);
         int __fastcall Locate (char *KeyFields, char *KeyValues, int options);

 };

thanx in advance

tomcruz.net
 
Private just keeps the programmer (anyone who uses your code) from accessing a part of the object that you don't want them to or a part that they just don't need to know about. It could be solely for the purpose of keeping cleaner code. Or it could be that you have an internal structure/functionality that MUST be used for your code to work and you don't want them to modify it or call part of it without the rest.

For instance, say you have a scenario where you want to load a file with information stored in your proprietary format. You don't want them knowing the format you load the file in, so you create your structure (the one used to save the file) in private. Then you break the structure down or re-arrange it in a different way in your public section. Then they call a simple function &quot;LoadFile(AnsiString Filename);&quot;. All they need to know is the filename that loads, for instance, settings for your program.

Another reason is that you may call 4 separate private functions in your &quot;LoadFile()&quot; function. You don't want the programmer seeing those four functions, it may confuse them as to which function to call to load the file or just overwhelm them with too many functions at their disposal.

Hope that helps,
Chris
 
As I havent studied much on classes (I just jumped in) I was wondering if there was anything I was missing. I think your statement has just reworded all other sources I have read. I suppose a lot of the choices are just personal coding style. thanx for your input.

tomcruz.net
 
It's not really much to do with style. It has to do with encapsulation and separating interface from implementation.


Think of a VCR. You have a play button, a pause button, fast forward, rewind, stop, record, and eject. You have a place to put the tape in. That's the public interface.

The guts and wires on the inside are private. You get separated from those. If you broke in and looked at them, and figured out how they worked, you could control the VCR by manipulating them. But you shouldn't. The VCR manufacturer might make their next VCR completely different on the inside, and your knowledge will become useless, and you'll have to spend time relearning how to work your VCR. If you had just accessed its functionality through the buttons, you'd already know how to use the new VCR because externally, it's exactly the same.


Same goes with a class. Say you have a Person class with a name, an age, and a sex. You can access these through the public interface (get_name, get_age, get_sex), or you can get to them directly by accessing the string, enum, and integer in the object.

The latter method works until the author of the Person class changes it to look up information in a database. Then those memebrs no longer exist, your code breaks, and you have to rewrite it. If you had accessed them through the public interface, though, you wouldn't have to chagne your code. Indeed, you wouldn't even need to know what changes had been made.


Public and private prevent things like that. They keep programmers from accessing things they're just not meant to play with, and allow a class to change implementation details without affecting client code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top