-
"Struct" or "Class"?
Hi,
I have completely learned the languages "C" and "Java".
In Java there were only Classes but NO Structures. And now
when I am learning "C++" language, I am a little bit confused:
Why, when there exist the Classes in C++, should I use Structures?
Or I will ask in another way: it's good to use ONLY Classes and the
Structures DON'T use ANYWAY?
Thanks.
Petike
-
In C++, structures and classes are the exact same thing, except that members in structs are public by default, whereas members in classes are private by default.
Typically, structs are used for Plain Old Data (similar to a struct in C), but actually a struct can have anything a class can.
-
In terms of the languages you listed:
- If I want to write a Java style class, I would use a C++ class.
- If I want a C style struct, I would use a C++ struct.
But you obviously don't have to do it that way.
-