Possible Duplicate:
What are the differences between struct and class in C++
I've done my homework and had diverse answers on Google.
Some say structs do not have inheritance, some say structs do not have access specifiers, while others say they have both.
Could someone clarify then, the differences between a struct and a class in C and C++, and also the difference between a struct in C & C++.
In C, classes do not exist. In C++, structs have a default access specifier of
public
, while classes default toprivate
.There are several differences between structs in C and C++; in C++ they can inherit from other classes or structs, they can contain member functions, and their names don't need to be referred to with an elaborated type specifier.
In C++, the only difference between a struct and a class is that struct members are public by default, and class members are private by default.
However, as a matter of style, it's best to use the
struct
keyword for something that could reasonably be a struct in C (more or less POD types), and theclass
keyword if it uses C++-specific features such as inheritance and member functions.C does not have classes.
C structs cannot use C++-specific features.
EDIT:
The C++ FAQ Lite, question 7.9, has this to say:
And quoting Stroustrup's "The C++ Programming Language", 4th edition, section 16.2.4: