RE: [Dev-C++] Newbie question Classes/Objects/Accessors????
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Smith, J. N. <jn...@ut...> - 2003-05-20 20:47:22
|
-----Original Message----- From: Smith, Jacob N. Sent: Tuesday, May 20, 2003 3:47 PM To: 'Rus...@ao...' Subject: RE: [Dev-C++] Newbie question Classes/Objects/Accessors???? > I understand the basics of Classes, but what determines which > members/functions should be public/private? A simple (and effective) rule is to make everything private and only expose those things which are needed for the client interface. Bruce Eckel has an online book (for free) at www.bruceeckel.com for beginners. I'm sure others could suggest more OOP-newbie books. > of the same class. What is the logic behind this? I can > understand what is > being done, but I don't understand the reasoning. Sometimes a simple variable such as as "renderedTextLength( const Font&, const std::string& )" has a simple meaning (what is the length of a string using a font): with a fixed-width font the function is simple ::size()*width, for non-console fonts this must be calculated on the fly using the font rules. By explicitly encapsulating all "value" calls (size, width, height, et al) you (A) keep clients from accidently messing up your class internals (B) provide a buffer so you can change implementation details, e.g. consider the size function for an image. If the old image class used a pointer to pixels then it's size is simply the length of the array, if a new class uses a rectangular array then it's size is width*height. If you'd exposed the height member to the clients you'd be stuck with a specific implementation. Note that any sensible compiler will be able to optimize the layer of indirection away so there is no overhead for "accessor" methods. -j. |