What is the difference between classes and structs (except of course the acces level), and is there any? Are they the same except classes are implicitly private and structs are implicity public? Are
C++ defines the new keyword "class" to allow the C++ language to evolve while maintaining compatibility with C. This is is why C++ did not elaborate on the stuct semantic. It would break C compatibility.
But C++ does elaborate struct in the same way it does class. C# has a more sensible approach in this regard where struct is just a record without elaboration.
Well, it is what I read somewhere some time ago. Maybe the C++ purists that lurk this website can quote the C++ standard here? Maybe it is just that compilers allow people to work on structs the same way they do on classes, as a non-standard extension due to popularity?
In my opinion, a struct should not be treated as a full C++ class even if a compiler allows it.
a struct should not be treated as a full C++ class
The C++ standard makes it clear; a struct is a class with different default privacy. Constructors, destructors, the whole lot. It's not a compiler extension. It's C++.
Although there are many standards in computing, you'll find there are even more conventions.
There's no standard that says Unix config files go in etc, it's a reasonable convention to follow.
The C++ standard doesn't stop you from using operator>> for addition or to open a file or any generic functionality. It's convention that makes us use it for shift or read operations.
Just because the C++ standard doesn't prohibit something, doesn't mean you should do it.
If structs and classes are the same why did they introduce classes?
They're not exactly the same, though. Something that's a struct in C will work in the same code compiled as C++. Changing structin C to class, and then compiling as C++, will break the code. As a guess, Stroustrup needed to maintain backwards compatibility AND ensure C++ has classes that behave like they should - keeping their secrets unless you specifically tell them not to.
I feel like it would make almost more sense to just leave structs how they were in C (as I think most C++ programmers use them anyway) and just add classes.
I never properly learned C, so what ResidentBiscuit says intrigues me. What are the capabilities of a struct in C, exactly? I guess I can trial and error in Visual Studio 2010 in .c files, but I guess VS2010 is not all that standard to begin with, so it just won't be good enough.
If someone could exhaustively enumerate the capabilities of a struct in C, then I guess it is very simple to determine if C++ extended them or not to the point of being a class with default public membership.
A C struct can kind of be seen as the ancestor to the class today (don't quote me on this). ASFAIK, C brought the first way to group data together that serves a similar purpose (ie data relevant to a single entity). What C structs didn't have was methods attached to them, or access modifiers of course.
//Proper C struct. No methods, only data.
struct Coords
{
float x;
float y;
};
//From here we can create instances of this just like a class.
Coords coords;
//And access elements in the same manner.
coords.x = 12.3;
coords.y = 7.9;
//Pointers to structs also work the same
Coords* pCoords;
pCoords -> x = 3.8;
(*pCoords).y = 4.7;
//Can also initialize this way:
Coords coords = {1.2, 4.5};
//Though I think this is not as easy to read as you have to know the order of the elements.
In use, the only difference between C structs and C++ structs is C structs have no methods or access modifiers.
EDIT:
I'm no master at this, so I probably left out some more in depth things. Such as having pointer members, and how to perform deep copies.
OK, I didn't ask what's the difference between C structs and C++ structs! I asked what's the difference between c++ structs and c++ classes! Except for the default access, is there any?
There is another difference: the default inheritance access level differs depending on which keyword is used to define the derived class. A derived class defined using the class keyword has private inheritance by default. A class defined with the struct keyword, has public inheritance by default.
Yea, the "acces level" includes the "inheritance acces level". So it's obvious that since structs have access level deafulat public, the same is for in heritance. Same goes for classes, only they are private by default.