|
-
May 16th, 2008, 11:28 PM
#1
struct questions.
here they are:
1.Can a struct inherit from another struct?
2.Can a struct inherit from another class?
3.Can a struct be the base of another class?
4.Can a struct implement an interface?
A one line explanation for each, instead of a simple yes or no would help a lot.
Thanking all in anticipation.
Regards
I
-
May 16th, 2008, 11:32 PM
#2
Re: struct questions.
In C++, structures and classes are identical in all respects but two:
1. Default member access; public for structures, private for classes.
2. Default inheritance; public for structures, private for classes.
-
May 16th, 2008, 11:34 PM
#3
Re: struct questions.
This sounds like homework to me.
The differences between structs and classes have been discussed probably hundreds of times on this forum. A search is in order, perhaps?
-
May 16th, 2008, 11:52 PM
#4
Re: struct questions.
mm..it is kind of homework.
but on second thoughts I should not have put up all the four questions. I kinda know the difference between structs and classes, don't know if they can be mingled in a hierarchy.
so just someone tell me if a struct can inherit from or form the base of a class.
thanks.
I
-
May 16th, 2008, 11:53 PM
#5
Re: struct questions.
 Originally Posted by indrajit_p1
so just someone tell me if a struct can inherit from or form the base of a class.
What part of my post did you not understand? Whatever you can do with a class, you can do with a structure...
P.S.
It's not that hard to try it yourself, you know.
-
May 17th, 2008, 12:04 AM
#6
Re: struct questions.
 Originally Posted by Plasmator
P.S.
It's not that hard to try it yourself, you know.
Hey Thanks! that option somehow skipped my mind. probably because I have written much less C++ code than I have read about the language.
Thanks again.
I
-
May 17th, 2008, 12:06 AM
#7
Re: struct questions.
 Originally Posted by Plasmator
What part of my post did you not understand? Whatever you can do with a class, you can do with a structure...
P.S.
It's not that hard to try it yourself, you know.
Yeah I agree. That's why we suggest you post a problem with the code in question so that you know yourself that it doesn't work before posting anything. I've learned that following this rule can greatly reduce the need for posting on the forums.
I've submitted an FAQ on the difference between classes and structs since we often find ourselves repeating it. If it is accepted then we can just give people a link.
-
May 17th, 2008, 02:06 AM
#8
Re: struct questions.
The only difference between class and struct is:
Member of classes, inheritance is private by default.
Member of structs, inheritance is public by default.
If you read any other differences - it's a core crap!
Yes, when you mix unions with either of these, there would be complixities and limitations.
Last edited by Ajay Vijay; May 17th, 2008 at 02:18 AM.
-
May 17th, 2008, 02:15 AM
#9
Re: struct questions.
The only difference between class and struct is:
Member of classes are private by default.
Member of structs are public by default.
If you read any other differences - it's a core crap!
No, Plasmator is correct. The C++ Standard states: "A structure is a class defined with the class-key struct; its members and base classes are public by default."
Thus, another difference between classes and structs is that classes use private inheritance by default and structs use public inheritance by default.
Oh, and then there are the trivial differences like difference in spelling between "class" and "struct"
Last edited by laserlight; May 17th, 2008 at 02:17 AM.
-
May 17th, 2008, 02:19 AM
#10
Re: struct questions.
Thus, another difference between classes and structs is that classes use private inheritance by default and structs use public inheritance by default.
You did not read it properly, I also mentioned it!
-
May 17th, 2008, 02:24 AM
#11
Re: struct questions.
You did not read it properly, I also mentioned it!
Oh, okay. I had the impression that you were contradicting what has already been said since indrajit_p1 seemed satisfied with what was already stated.
-
May 17th, 2008, 10:47 AM
#12
Re: struct questions.
 Originally Posted by laserlight
The C++ Standard states: "A structure is a class defined with the class-key struct; its members and base classes are public by default."
amidst all the crossfire of brilliant ideas and all that wisdom, this is the first time someone gave a REAL answer to my real question (which I made clear a little late). since the standards say that "a structure is a class", that answers my querry "if they can inherit from one another ( a class and a struct)".
I tried it on my g++ (after being adviced by Plasmator), and it let's me instantiate a struct derived from a class which is in turn derived from a struct, and also call a function on it without as much as a whimper. but that would have left me with "my g++ lets me do it" as an answer when faced with that question in a formal discussion (and I said it was kind of a homework - really from a set of possible questions for a Microsoft, USA telephonic interview handed to me by my agent).
Thanks laserlight.
p.s.: and that was a small line from the standards, not worth surfing to a brand new internet URL, is it?
p.s.: I don't know where to say this, but as a rule of the thumb, people should be clear about what the questioner is asking and how much (and which much) of it they can and are REALLY answering and clearly state the later in their post. it is far too often that I find people not caring to clarify themselves on the first part (like saying "this is the difference between struct and class" in reply to "if they can be mingled in an inheritance hierarchy". though it's clear now that they are not, they could, by all means, be two completely different things which share most of their properties, for someone who is not informed enough) and almost always not giving a single penny for the second. -Regards/Indrajit
Last edited by indrajit_p1; May 17th, 2008 at 10:51 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|