Click to See Complete Forum and Search --> : need to prove a point to a friend
EscapeCharacter
06-24-2001, 03:51 AM
ive got this friend who says that when you create a class that if you dont put the functions or variables of the class under private, protected, or public they go into some special type that is only used in friends.
so he thinks
class whatever{
friend class someotherclass;
};
is not the same as saying
class whatever{
private:
friend class someotherclass;
};
now ive read in a book somewhere that if you dont put a label in the class it defaults to private though i cant find it now. so could someone confirm this
Bradmont2
06-24-2001, 04:46 AM
In classes, the default is private. In structs, the default is public. (BTW, this is, IIRC, the only difference between the two ;))
lsibn
06-24-2001, 01:17 PM
Originally posted by Bradmont:
<STRONG>In classes, the default is private. In structs, the default is public. (BTW, this is, IIRC, the only difference between the two ;))</STRONG>
I didn't know a struct could have member functions. Is this something specific to C++?
As for the question, I open "Sam's Teach Yourself C in 21 Days" to Bonus Day 3: Working with C++ Classes and objects. Page 665, paragraph one says:
<STRONG>It bears repeating that although structures and classes are very similar, there is a difference. By default, the data members of a structure are public, which means they are accessible outside of the structure's member functions. The default access control for a class is private. This means that by default, only the class's member functions can access the private members.</STRONG>
Bradmont2
06-24-2001, 03:27 PM
Originally posted by lsibn:
<STRONG>I didn't know a struct could have member functions. Is this something specific to C++?
As for the question, I open "Sam's Teach Yourself C in 21 Days" to Bonus Day 3: Working with C++ Classes and objects. Page 665, paragraph one says:
It bears repeating that although structures and classes are very similar, there is a difference. By default, the data members of a structure are public, which means they are accessible outside of the structure's member functions. The default access control for a class is private. This means that by default, only the class's member functions can access the private members.</STRONG>
Yes, I do believe that C structs are unable to have member functions, and this was changed in C++ (tho I have never done C, so I'm just going from what I've been told ;)). Not sure about objective C, tho...
<edit> I wish nested quotes were automatically stripped more reliably... oh well</edit>
[ 24 June 2001: Message edited by: Bradmont ]
Strike
06-24-2001, 11:20 PM
C structs can have member functions but you can't do inheritance and derived classes with structs.
And yes, the default is private. Bjarne himself says so in the language spec :) Tell your friend that.
Bradmont2
06-25-2001, 01:50 AM
Bjarne is a funny name.
JasonC
06-25-2001, 07:26 AM
Structs can have member functions, however, this is not common practice. Usually classes are used if member methods/functions are used.
BrianDrozd
06-25-2001, 08:03 AM
It sound like your friend has mixed up Java and C++. C++ there are only the three types: private, protected, and public. If members or methods are not specifically declaired as one of those three, then they are assigned a default.
In Java, however, your friend is correct. There are four types: private, protected, public, and the default. If members or methods are declaired without 'private', 'protected' or 'public' keywords, then they are given the default protections, which in Java means that they are public with respect to other classes in the same project but private everywhere else.
EscapeCharacter
06-25-2001, 10:35 AM
<STRONG>It bears repeating that although structures and classes are very similar, there is a difference. By default, the data members of a structure are public, which means they are accessible outside of the structure's member functions. The default access control for a class is private. This means that by default, only the class's member functions can access the private members.</STRONG>
i knew ive seen it somewhere and thats exactly where im bring my book to his house tomorrow
:D
lsibn
06-25-2001, 12:15 PM
I see a lot of people pointing out that structs can have member functions. I just wanted to say I guess I can't see any reason they wouldn't, given the fact that a function is somthing like 'code to return a variable,' so int age() shouldn't really be that different from int age. I know what I'm thinking, but the proper terminology escapes me. This is ok, though, because I think you know what I mean. :)