What kind of information should be kept in public and what should be kept in 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-
Hmm... I posted too soon in the other thread. Anyways, here it is:
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).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.
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.
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 08: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.
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-
Well that brings me to another question. Are you saying that I could have the same functions in two different classes? For example: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.
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?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; }
Knowledge is power and I want it all
-0RealityFusion0-
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 09: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.
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
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-
just to make it even clearer...
and this is how it's called...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(); }; ...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
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.
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.
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"Originally posted by funkydude9
major_small, don't you have your comments switched in your class definition?
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