In C++, structs and classes are pretty much the same; the only difference is that where access modifiers (for member variables, methods, and base classes) in classes default to private, access modifiers in structs default to public.
However, in C, a struct is just an aggregate collection of (public) data, and has no other class-like features: no methods, no constructor, no base classes, etc. Although C++ inherited the keyword, it extended the semantics. (This, however, is why things default to public in structs—a struct written like a C struct behaves like one.)
While it's possible to fake some OOP in C—for instance, defining functions which all take a pointer to a struct as their first parameter, or occasionally coercing structs with the same first few fields to be "sub/superclasses"—it's always sort of bolted on, and isn't really part of the language.
Answer from Antal Spector-Zabusky on Stack OverflowIn C++, structs and classes are pretty much the same; the only difference is that where access modifiers (for member variables, methods, and base classes) in classes default to private, access modifiers in structs default to public.
However, in C, a struct is just an aggregate collection of (public) data, and has no other class-like features: no methods, no constructor, no base classes, etc. Although C++ inherited the keyword, it extended the semantics. (This, however, is why things default to public in structs—a struct written like a C struct behaves like one.)
While it's possible to fake some OOP in C—for instance, defining functions which all take a pointer to a struct as their first parameter, or occasionally coercing structs with the same first few fields to be "sub/superclasses"—it's always sort of bolted on, and isn't really part of the language.
Other that the differences in the default access (public/private), there is no difference.
However, some shops that code in C and C++ will use "class/struct" to indicate that which can be used in C and C++ (struct) and which are C++ only (class). In other words, in this style all structs must work with C and C++. This is kind of why there was a difference in the first place long ago, back when C++ was still known as "C with Classes."
Note that C unions work with C++, but not the other way around. For example
union WorksWithCppOnly{
WorksWithCppOnly():a(0){}
friend class FloatAccessor;
int a;
private:
float b;
};
And likewise
typedef union friend{
int a;
float b;
} class;
only works in C
Hi !
I'm coming from a Java background and am used to create classes.
But in C++ you have also structures.
When would you use a struct and when a class ?
Practical example:
For learning purposes, I'm creating a program which plots geographical locations on a window.
My "Java instinct" tells me to create a CPoint class containing:
string id;
double lat;
double lng;
int x;
int y;
and a protected method "translate" to do the conversion.
a constructor to call translate() whenever a new object is created, generating x and y
in C++: Could I do that also using a struct and why ? or why not ?
Thanks a lot Redditers ! :-)
Edit 2: The language is C++. sorry everyone
So I have been going through my course instruction using classes to implement data structures, yet LeetCode uses structs to implement them. I read that structs are faster than classes in that they do not use heap memory.
Edit: Someone has pointed out that both can be allocated on the heap and the stack
What is the purpose of implementing a data structure via a class if structs can be used? Sorry if this is a dumb question
The differences between a class and a struct in C++ are:
structmembers and base classes/structs arepublicby default.classmembers and base classes/structs areprivateby default.
Both classes and structs can have a mixture of public, protected and private members, can use inheritance, and can have member functions.
I would recommend you:
- use
structfor plain-old-data structures without any class-like features; - use
classwhen you make use of features such asprivateorprotectedmembers, non-default constructors and operators, etc.
As everyone else notes there are really only two actual language differences:
structdefaults to public access andclassdefaults to private access.- When inheriting,
structdefaults topublicinheritance andclassdefaults toprivateinheritance. (Ironically, as with so many things in C++, the default is backwards:publicinheritance is by far the more common choice, but people rarely declarestructs just to save on typing the "public" keyword.
But the real difference in practice is between a class/struct that declares a constructor/destructor and one that doesn't. There are certain guarantees to a "plain-old-data" POD type, that no longer apply once you take over the class's construction. To keep this distinction clear, many people deliberately only use structs for POD types, and, if they are going to add any methods at all, use classes. The difference between the two fragments below is otherwise meaningless:
class X
{
public:
// ...
};
struct X
{
// ...
};
(Incidentally, here's a thread with some good explanations about what "POD type" actually means: What are POD types in C++?)