This question was already asked in the context of C#/.Net.
Now I\'d like to learn the differences between a struct and a class in C++. Please discuss the technical d
. In classes all the members by default are private but in structure members are public by default.
There is no term like constructor and destructor for structs, but for class compiler creates default if you don't provide.
Sizeof empty structure is 0 Bytes wer as Sizeof empty class is 1 Byte The struct default access type is public. A struct should typically be used for grouping data.
The class default access type is private, and the default mode for inheritance is private. A class should be used for grouping data and methods that operate on that data.
In short, the convention is to use struct when the purpose is to group data, and use classes when we require data abstraction and, perhaps inheritance.
In C++ structures and classes are passed by value, unless explicitly de-referenced. In other languages classes and structures may have distinct semantics - ie. objects (instances of classes) may be passed by reference and structures may be passed by value. Note: There are comments associated with this question. See the discussion page to add to the conversation.