Public vs. Private variables in classes

This is a discussion on Public vs. Private variables in classes within the C++ Programming forums, part of the General Programming Boards category; Okay, what's the point of having a private variable when you can't access it? I mean why not just keep ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    100

    Public vs. Private variables in classes

    Okay, what's the point of having a private variable when you can't access it? I mean why not just keep everything public.

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Quote Originally Posted by blankstare77
    Okay, what's the point of having a private variable when you can't access it?
    A private variable is for use internally within that class only. This prevents people who are using your class from messing with data (by mistake, or because they think they're smarter) that might affect the overall state of an object of that class.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    You can also use public accessor functions to access a private variable.

    i.e.
    GetVariable()
    SetVariable()

    I can't remember the exact reasoning behind this, but it's supposed to be better object oriented programming.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    There are many reasons you might want to control access:

    1. You may need to ensure that your variable is set to a valid value, so you report an error if the user tries to set it to something that is out of range.
    2. Setting your variable may require updates to other data.
    3. Not all of your variables will need to be accessed by the user of a class (that is, they may be totally internal).
    3a. The algorithms that your class use may change; you can change the internals without modifying the interface, and the user of your class is not affected.

    This is not comprehensive, but a sample of the important reasons for restricting access.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    It simplifies an object's interface because users don't need to know (and in most cases they shouldn't be concerned with) how an object does what it does; they simply need to know how to use it.

  6. #6
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Also, some private variables are read-only, and have only GetVariable() functions.

    It tries to solve the following problem in C, where comments are used to say which variables are read-only. C++ just enforces it with private variables. (example taken from the SDL library).

    Code:
    typedef struct SDL_Surface {
            Uint32 flags;                           /* Read-only */
            SDL_PixelFormat *format;                /* Read-only */
            int w, h;                               /* Read-only */
            Uint16 pitch;                           /* Read-only */
            void *pixels;                           /* Read-write */
    
            /* clipping information */
            SDL_Rect clip_rect;                     /* Read-only */
    
            /* Reference count -- used when freeing surface */
            int refcount;                           /* Read-mostly */
    
    	/* This structure also contains private fields not shown here */
    } SDL_Surface;

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    So basically public variables the user can change but private variables it's not to be affected by the user? Am i way off?

  8. #8
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Actually, you're a bit off, blankstare. Private variables and member functions can not even be accessed by users. A compiler error will occur if a private access is attempted.

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    Maybe I worded it wrong but yeah that's what i meant. Thanks for the clarification though .

  10. #10
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    So basically public variables the user can change but private variables it's not to be affected by the user?
    Right. It can't be directly changed or read from outside of the class. But, you can write a public accessor function to indirectly change it.

    Code:
    X = MyObject.Y;   //  Won't work if Y is private
    MyObject.Y = X;  // Won't work if Y is private
    
    X = MyObject.GetY();  // OK if function GetY() is is public
    MyObject.SetY(X);  // OK if SetY() is public

  11. #11
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    You can also be nasty and do a memcpy() on the object.

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    ... And if you ever find yourself trying to do a memcpy() on an object, simply ask yourself why you are using data hiding, and then trying to break through it.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  13. #13
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    or of course you could always return a pointer to the private variable... but as of yet i'm still unsure why that is useful. but it just is... that's all i know
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    Everything public on a class is called the interface. This is the stuff you can use to order the class to do things. Generally you want the interface of a class to be as small as possible, and this also means variables you don't NEED to change from outside of the class shouldn't be part of the interface.
    If a variable IS in fact part of the interface, it's still better to make it private and have a function to read it and a function to change it because this way you can prevent your user from setting the variable to something that doesn't make sense.
    Imagine an amplifier with a configurable volume that's a float between zero and ten. If you just leave the variable part of the interface there's nothing that prevents the user from setting the volume to 11, 259268 or -3, even though those values might cause severe trouble later on. Having the variable private with a Get and Set function would allow you to automatically limit the values of the variable.

    Mostly though, making stuff private is just convenient for the same reason minimising the amount of global variables and functions is convenient: if your project gets large it's very practical that you don't have to know what variables should be changeable and which aren't. (like what Dante Shamest posted)
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. about private class variables
    By bulletbutter in forum C++ Programming
    Replies: 5
    Last Post: 04-18-2008, 12:13 PM
  2. #define private public
    By revelation437 in forum C++ Programming
    Replies: 21
    Last Post: 12-20-2003, 03:37 PM
  3. I need a code for Tic Tac Toe game ?
    By martyr in forum C++ Programming
    Replies: 11
    Last Post: 12-07-2003, 02:29 AM
  4. public function returning const char * of private member
    By VanJay011379 in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2002, 03:28 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21