Dashboard/Block 1: Foundation/Module 6
Module 6Foundation

Structs, Enums & Data Organization

Alright, let's think about something. So far, you've been storing data in separate variables — an int for age, a string for name, a double for GPA. That works fine for one student. But what happens when you need to keep track of 100 students? Are you going to create 300 separate variables? Yeah, that would be a nightmare.

This is where structs come in, and they're going to change the way you think about organizing data. A struct (short for structure) lets you bundle related variables together under one name, essentially creating your own custom data type. Think of it like a form or a record card — a student card has fields for name, age, and GPA, all on one card. You can then have a whole stack of these cards (which, in code, is just a vector of structs).

We'll also learn about enum class — a way to create variables that can only hold specific, named values. Instead of using random integers like 0, 1, 2 to represent colors (which is confusing and error-prone), you define enum class Color { Red, Green, Blue }; and write Color c = Color::Red;. Way more readable, and it prevents bugs where you accidentally assign something invalid.

ConceptWhat It SolvesExample
StructGroups related data into one custom typestruct Student { string name; int age; double gpa; };
Struct methodsLets structs have their own functionsvoid display() const { cout << name; }
Enum classRestricts a variable to a fixed set of named valuesenum class Color { Red, Green, Blue };
Vector of structsManages a collection of structured recordsvector<Student> roster;

By the end of this week, you'll be designing your own data types, storing collections of structured data in vectors, and using type-safe enumerations to make your code cleaner. And here's the exciting part — structs are the stepping stones to classes and object-oriented programming, which we'll tackle in later weeks.

Learning Objectives

Define and use structs to group related data
Add member functions to structs
Use enum class for type-safe enumerations
Store structs in vectors
Pass structs to functions by const reference

Key Concepts

Structs: Grouping Data

Enum Class (Type-Safe Enums)

Vectors of Structs