Well, from the editorial point of view the difference in their very names.
Functionally, a struct keyword leaves one option only for the constructing a compound
of their chain:
struct a{int y}; struct b{int y}; //e.t.c
struct c
{
a A;
b B;
//e.t.c.
g G;
};
void{c C;}
Of course each of them can be included in one another making eventually one to edit something like this:
x=A.B.C.D.E.F.G.y // to read y field of G struct
instead of
x=C.G.y or C->G.y
As for classes there is almost no limit to structures except for the cross reference
including compounds that may contain a lot of the unused functionality...
Structs are remainings from C
Classes is the C++ replacement!
This is not true.
Functionally, a struct keyword leaves one option only for the constructing a compound
of their chain
This is true of classes as well.
I'll say again.... functionally speaking... the only difference is that structs default to public, and classes default to private. Apart from that they are identical:
1 2 3 4 5 6 7 8 9
struct Foo
{
int this_will_be_public;
};
class Bar
{
int this_will_be_private;
};
In practice, the two are used in different styles. Generally structs have public members and are just clumps of data.
Whereas classes tend to be encapsulated objects.
This is not a "rule", though. It's just a style thing for clarity.
Structs are remainings from C
Classes is the C++ replacement!
Hmmm... above statements is not really true. From what I know C++ was created and built upon C. Struct already exist in C so when C++ was created and to make sure legacy C code still run perfect using a C++ compiler, the struct is retained. Classes was introduced in C++ with protection specifiers so I believe to make C struct co-exist peacefully with C++, they default struct protection specifiers to public. This mean legacy C code need not be changed when compiled using a C++ compiler.
I think the idea that time is a C++ compiler should be able compile legacy C code without developers needing to edit the C code. It must be a seam-less process. So this arise the scenario where we see struct and also class supported in C++.
Personally for me due to my C leanings, I tend to use struct and class interchangeably. Ideal should be all new C++ code should stick to class but then hehehe.... C++ cannot force me since it must support C constructs isn't it ? :)
Structs and classes differ in a few ways. Semantically, there are two differences: the default access specifer for members and the default access specifier for inheritance.
Beyond semantics, the two are commonly used to convey the implementor's intent. For example, struct is often used to represent "C-style" structs that are a simple grouping of data. They typically do not enforce invariantsnor do they add behavior. I often see more simple user-defined types, for tag dispatching, empty base classes, etc. implemented as structs, as well.
A class is more than just a grouping of data. Classes model an abstraction and add encapsulation. They often enforce invariants and provide behavior.