Classes: public or private?

This is a discussion on Classes: public or private? within the C++ Programming forums, part of the General Programming Boards category; What kind of information should be kept in public and what should be kept in private?...

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Classes: public or private?

    What kind of information should be kept in public and what should be kept in private?
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Hmm... I posted too soon in the other thread. Anyways, here it is:

    Typically, all of your data (variables) are private. There really should be no need for outside code (with the exception of friend classes/functions) to access these parts. Making classes like this does a few things:

    1) Allows any changes to be validated first (possibly throwing exceptions if necessary).
    2) Prevents objects from entering invalid states easily (insuring that all variables are properly updated when a change is made). (Though some types of objects may still be able to enter invalid states - a socket connection lost, or something - the class can deal with it, and prevent invalid data from being treated as though it is valid.)
    3) Makes client code dependent upon a particular interface, not a particular implementation.

    Just to name a few.
    Functions which define the interface to your class should be made public. Utility functions which are not part of the public interface should be made private (or possibly protected).
    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.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    Public information should be public and private information should be private.

    Any information you don't want to be accessed outside of the class should be private...But you first have to think about how the objects are going to interact with eachother, then to figure out if member A needs to be private, ask yourself "Does anything else need to access this member, or does this member belongs only to this class?" Everything is set to private by default.
    Last edited by funkydude9; 08-18-2003 at 09:34 PM.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  4. #4
    1479
    Join Date
    Aug 2003
    Posts
    253
    Most of that sounded like chinese to me. What I picked up from that was that public should only be used to access private to insure a 'protected and safe' class?
    Knowledge is power and I want it all

    -0RealityFusion0-

  5. #5
    1479
    Join Date
    Aug 2003
    Posts
    253
    Originally posted by funkydude9
    Public information should be public and private information should be private.

    Any information you don't want to be accessed outside of the class should be private...But you first have to think about how the objects are going to interact with eachother, then to figure out if member A needs to be private, ask yourself "Does anything else need to access this member, or does this member belongs only to this class?" Everything is set to private by default.
    Well that brings me to another question. Are you saying that I could have the same functions in two different classes? For example:

    Code:
    class person1
         {
          public:
               void GetAge();
          private:
                int age; 
           }
    person1::GetAge()
         {
           return age;
          }
    
    
    class person2
           {
            public:
                void GetAge();
            private:
                 int age;
             }
    person2::GetAge()
            {
              return age;
             }
    If this works it would simplify it all. Would this be possible? If person 1 and person 2 had different ages would it keep the ages different even though they would be declared 2 different ages?
    Knowledge is power and I want it all

    -0RealityFusion0-

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    Well, you can do that but I don't think that there's any reason to. You don't need two of the same class...Just make one class and when you declare your class type variable, make it an array. Example:

    Code:
    class Person
    {
    public:
         int GetAge();
    private:
          int age; 
    };
    
    int Person::GetAge()
    {
           return age;
    }
    
    int main()
    {
         Person person[2]; //2 different people
         cout<<"Person 1's age: "<<person[0].GetAge();
         cout<<"Person 2's age: "<<person[1].GetAge();
    
         return 0;
    }
    Last edited by funkydude9; 08-18-2003 at 10:18 PM.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Yes, but if both the classes are the same then there's no reason to have two classes .

    To clarify what private means:
    Code:
    class person1
         {
          public:
                person1 ():age(0){}
                person1(int x):age(x){}
                int GetAge(){return age;}//must return a value...
         private:
                int age; 
           };//trailing semicolon...
    
    
    int main(void)
    {
    person1 myperson(20);
    cout<<myperson.GetAge(); //works, GetAge() is public
    cout<<myperson.age; //doesnt work, age is private
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    1479
    Join Date
    Aug 2003
    Posts
    253
    Thank you funkydude!!!!! I have wondering if that was possible. I started to do it in different program but I couldn't get the concept down......but you cleared it up! Thank you!
    Knowledge is power and I want it all

    -0RealityFusion0-

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    just to make it even clearer...


    Code:
    ...
    class thisClass
    {
       public:
          /* only what needs to be used by the caller of the class */
          int IntFunc();
       private:
          /* everything else goes here */
          float FloatFunc();
    };
    ...
    and this is how it's called...
    Code:
    ...
    thisClass classObject;   //initializer called
    classObject.IntFunc();   //calls the public class
    classObject.FloatFunc();   //not legal - don't try it... it can be called from within the IntFunc function in the thisClass class...
    ...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    major_small, don't you have your comments switched in your class definition?
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  11. #11
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Well, there are 2 major kinds of classes:

    1) "struct-like" classes. These are only a collection of data members. They have no methods at all (except perhaps a default constructor to initialize). They consist solely of variables, and all are public.

    2) Non-"struct-like" classes. A well-designed class conforms to these rules of good design:

    * public contains only methods, and no variables. Any method that should be useable to anyone exists here; this is the public interface of the class.

    * protected also contains only methods. Unlike the public interface, these methods are made to be used in derived classes. This is the protected interface of the class.

    * private contains every variable, and any additional methods. This area contains implementation, not interface.

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Originally posted by funkydude9
    major_small, don't you have your comments switched in your class definition?
    eh, not really... i should have just made them clearer... the first one should be more like: "only stuff that is directly accessed by the caller" and the second (private) one should be: "everything not touched by the caller, unless through a public function"
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. webBrowser problem
    By algi in forum C# Programming
    Replies: 7
    Last Post: 08-19-2005, 10:35 AM
  2. Problem with character arrays in classes
    By spoketoosoon in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2004, 03:57 AM
  3. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM
  4. program to unpack packed data
    By vsk in forum C Programming
    Replies: 5
    Last Post: 11-14-2002, 09:17 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 02:26 PM

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