C++ Core
Classes vs Structs
Only two real differences; the rest is convention.
Concept core idea Watch out easy to get wrong
Concept
Key concepts
- Only two real language differences: (1) default access —
structpublic,classprivate; (2) default inheritance —structpublic,classprivate. - Everything else is identical (both can have ctors, methods, virtuals, inheritance, templates).
template<class T>/template<typename T>allowed;template<struct T>is not.- Convention: struct = passive data bundles; class = encapsulated types with behavior/invariants.
Watch out
Easily confused
class default vs struct default
Members are
private by default in a class, public by default in a struct (same for default inheritance mode).Test yourself
5 questions on this lesson. Write your answers from memory, then compare them with the model answers side by side.