Systems CourseC/C++ · Linux · Memory
C++ CoreLesson 06 of 441 min read5 questions

Classes vs Structs

Only two real differences; the rest is convention.

Test yourself ↓

Concept core idea Watch out easy to get wrong

Concept

Key concepts

  1. Only two real language differences: (1) default access — struct public, class private; (2) default inheritance — struct public, class private.
  2. Everything else is identical (both can have ctors, methods, virtuals, inheritance, templates).
  3. template<class T>/template<typename T> allowed; template<struct T> is not.
  4. 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.