C++ Tutorial: Structs & Enums
struct for grouping data, enum and enum class for named constants, unions, and the difference from classes.
Group related data together
A `struct` bundles related data under one name. Before OOP, structs were how all complex data was organised in C. In modern C++, structs with methods are nearly identical to classes — the only difference is default access: `struct` defaults to public, `class` to private.
`enum class` (C++11) replaces old-style enums with type-safe, scoped named constants.
What you'll learn in this C++ structs & enums tutorial
This interactive C++ tutorial has 6 hands-on exercises. Estimated time: 16 minutes.
- Your first struct — A `struct` groups related data:
- Structs with methods — Structs can have methods — making them nearly identical to classes:
- enum class — type-safe enumerations — Old-style `enum` pollutes the namespace and converts to int implicitly — dangerous.
- Structs as function parameters — Pass structs by **const reference** to avoid copying:
- Nested structs — Structs can contain other structs — building complex data models.
- Build a playing card system — No starter code.
C++ Structs & Enums concepts covered
- Group related data together