Thread: Why Private

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    13

    Why Private

    I'm working on something and it just seems a lot easier to keep the members public so I can get to them from everywhere else in the program.

    What are the advantages of making object members private?

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Encapsulation!

    nuff said.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    i don't get it... why would you take control away from your self?

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    13
    Ok see that's the thing. I don't think I really understand the purpose of private. To me it just seems like private makes it harder to get to the members from other parts of the program.

    What do you mean take control away from myself?

    I'm not doubting that there's a good reason for private, I just don't think I understand it properly.
    "Why not go mad?"

  5. #5
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Suppose you have a class with 50 functions but only 10 are going to be directly used outside of the class, the other 40 are simply 'helper' functions. By making those 40 private when someone else uses the class they can see exactly what can be done with the class, only the useful stuff.

    Same applies to variables. You have to think about when the classes are going to be reused, so you want as little access as possible to the implementation details. This way another user of your class has almost no chance of messing it up and it makes it simple to use.

    I really hope thats clear...
    Couldn't think of anything interesting, cool or funny - sorry.

  6. #6
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    If I have a class "time", I store the time in a set of 3 integers: hours, minutes and seconds. Suppose I made these integers public, you could, or the program could easily modify the hours to exceed 12. By making the variable private, and using set and get functions you can ensure that the values being passed to your private data members are valid.

    Code:
    bool time::sethours(int iHour)
    {
        if (iHour < 1 || iHour > 12)
        {
              m_iHours = 0;
              return 0;
        } else {
              m_iHours = iHour;
              return 1;
        }
    }

  7. #7
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Good example Eibro. Its all about when people are going to reuse your classes, they should be able to use them without knowing the details of how they work.
    Couldn't think of anything interesting, cool or funny - sorry.

  8. #8
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    its not only for code reuse. People sometimes hear that and think, "I'm the only one who's going to edit this so I don't need to do it". There is a matter of cleanliness. old style C is sloppy. referencing variables across modules is sloppy. Before you know it you'll have a program that is impossible to edit without breaking something you had not expected to change. private variables are very simply things that outside objects should not be able to touch. If they REALLY want to touch it, well let's just say there should be more thought in the structure of the code.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    13
    Thanks guys! I got it now.

    Once again I appreciate the help!
    "Why not go mad?"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# and SQL
    By siten0308 in forum C# Programming
    Replies: 2
    Last Post: 07-09-2008, 12:34 PM
  2. webBrowser problem
    By algi in forum C# Programming
    Replies: 7
    Last Post: 08-19-2005, 09:35 AM
  3. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM
  4. I need a code for Tic Tac Toe game ?
    By martyr in forum C++ Programming
    Replies: 11
    Last Post: 12-07-2003, 03:29 AM
  5. Post programs
    By GaPe in forum C# Programming
    Replies: 8
    Last Post: 05-12-2002, 11:07 AM