Question:
in c++. why class was created if struct was able to do all : encapsualtion, polymorphism and inheritance?
pammy
2010-06-07 22:22:41 UTC
i am a c++ learner, what i want to do with classes, i am easily able to do with the struct, that was available in c.
So what's the major difference b/w classes and structs other than "structs have default public declarations and classes have default private declarations".
Five answers:
husoski
2010-06-08 08:26:37 UTC
The base classes are also public by default in a struct. But, you're right--those are indeed the onty differences.



In "The Annotated C++ Reference Manual" by Stroustrup and Ellis (this was the language definition in 1990 just before ANSI and ISO developed standards), "class", "struct" and "union" keywords are "class keys" and all introduce class data types. (Unions can have methods and constructors, but cannot have base classes or be used as a base class and have a few other peculiarities.) So, the ability to do "OOP with structs" seems to have been in the language for a long time.



In what I've read from Bjarne Stroustrup (the creator of C++), the language was intended to enhance C rather than replace it. So, like oops, who posted before, I think that "class" was added to introduce new features and struct was retained for compatibility with C. The similarity in features may or may not have grown since the origins of C++ in the early 80s, but the final form was there by 1990. The place to look for an authoritative answer to this is in "The Design and Evolution of C++" by Bjarne Stroustrup. Unfortunately, I don't have it at all, much less handy.



The difference in defaults isn't a small issue, by the way. For OOP encapsulation, you really do want everything hidden that isn't explicitly declared public. However, structs must have it the other way for C compatibility--and that's no small thing either. Here's a Stroustrup quote from a 2000 Q&A on SlashDot:



---begin quote---

Let me just mention something I wouldn't have done differently: compatibility. Had C not been there to be compatible with, I'd have chosen compatibility with some another language. Innovation should focus on improvements and what works should be left as unchanged as possible. That way, people keep their existing tools and techniques and can develop from a base that is functionally complete. Also it saves the effort to re-invent the wheel and to teach "new" stuff that is equivalent to old stuff. Thus, C++ is as close to C as possible - but no closer.

---end quote---
oops
2010-06-07 23:32:12 UTC
As far as c++ is concerned, there is no other difference. However, those features you mention (polymorphism and inheritance, private members) are not available in c structs, only in c++. Originally though, c++ structs didn't have those features, only classes did. And c++ had to keep structs in order to maintain backwards compatibility with c. Someone must have realized that there is no reason for structs not to have those features as well, so they were added later. And of course they can't now remove classes, since that would break a ton of code.



So, your choice of whether to use classes or structs is largely irrelevant. But most people use structs for what's called "plain old data". With mostly, if not all, public members, and few, if any, methods. Whereas they use classes for more complex objects.
?
2010-06-07 22:45:09 UTC
The best answer is that in c++ if you use a struct you have almost all the capabilities that a class does, the problem with that is it's not c. If you do that under a c compiler with a .c file you will get errors. Like the previous person said, structs don't have methods, they are simply used to aggregate data. Though we know that we can build several data structures with just a struct. Play around with just plain c for a bit. You can do things like put variables wherever you want in c++ and in c99 (I THINK) but really in c you have to put variables at the top of your blocks; meaning that you can't do things like



for(int i = 0; i < 10; ++i)...;



Because you are not allowed to use variables like that.
?
2016-06-03 05:14:02 UTC
You need to move the definition of 'struct node' outside of the definition of 'class foo'.
anonymous
2010-06-07 22:24:22 UTC
Classes have methods and structs don't.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...