cadence_learn
the_c++_track / level 13 of 15 ▶ 15 min

invent a type.

A student is a name and a score and an age. Keep them in three arrays and one sort puts everyone’s marks on the wrong person — keep them in a struct and it stops being possible.

after this level you'll be able to
  • Define a struct and reach its fields with a dot
  • Say why grouped fields beat parallel arrays
  • Put your own type in a vector and pass it to a function
things that belong together

one name for a group of values

A student is a name and a score and an age. You could keep three separate arrays — and people do, and it goes wrong the first time anything is sorted.

why three arrays is a bug waiting to happen
THREE PARALLEL ARRAYS — FRAGILE names Ada Grace Alan scores 91 72 88 ages 19 20 18 one array sorted, the others not — Grace now has someone else’s score ONE ARRAY OF STRUCTS — EACH ROW TRAVELS TOGETHER Ada9119 Grace8820 Alan7218 Sort it, shuffle it, hand one to a function — the three values cannot be separated, because they are one thing. A struct is you telling C++ “these belong together” — and once you have, it stops being possible to get them out of step.
Parallel arrays work right up until something moves. One sort, one insert, one delete, and every row is quietly wrong — with nothing anywhere reporting a problem.
struct Student { string name; int score; int age; }; // the semicolon here is REQUIRED and always forgotten Student s; s.name = "Ada"; // a dot to reach inside s.score = 91; cout << s.name << " " << s.score; // Ada 91

You have invented a new type. Student now sits alongside int and string — you can make one, put it in a vector, hand it to a function, and return it. Your catalog talks about “the use, design and implementation” of types; this is the design part, and it is the first time you get to decide what a type is.

the semicolon after the closing brace

}; — not just }. Everyone forgets it, and the error is one of the confusing ones because it points at whatever comes next. When a struct definition produces a bizarre message about the following line, check for that semicolon first.

◈ ask an ai about this

“What is a struct in C++, and why is it better than keeping several separate arrays?”

chatgpt ↗ claude ↗

try it yourself

reach inside with a dot

▤ lab 12 · a vector of students

Click a row, then a field. The expression that reaches it appears underneath.

Click a row and a field. Notice the shape: which one, then which part.

The expression reads left to right exactly as you would say it: which one, then which part. cls[1].score is “student number 1, their score”. Verified — that really gives 88.

the payoff

a whole record in one variable

Once a student is one thing, everything you already know works on it unchanged. Level 9’s vector holds them. Level 7’s loop walks them. Level 8’s functions take them:

void print(Student s) { cout << s.name << ": " << s.score << endl; } vector<Student> cls = {{"Ada", 91, 19}, {"Grace", 88, 20}}; for (int i = 0; i < cls.size(); i++) { print(cls[i]); }

And level 5’s find-the-biggest shape works too — keep the best student so far rather than the best number, and you get their name for free at the end. That is the whole reason to group things: the answer stays attached to who it belongs to.

this is the door to classes

A struct holds data. A class is the same idea with the functions that work on that data kept inside it too. Your course may reach classes at the very end, or leave them for the next one — either way, if structs make sense then classes are a short step, not a new world.

◈ ask an ai about this

“How do I put structs in a vector and pass them to functions in C++? And what is the difference between a struct and a class?”

chatgpt ↗ claude ↗

do this together

design a type

☶ two people · 15 minutes · paper

Pick something real and decide what it is made of.

person a

Name a thing: a library book, a train journey, a recipe, a football match. List the fields and their types.

person b

Attack the list. What is missing? What should not be there? Is date really a string? Should price be a double, or whole pennies as an int (level 8)?

Then the question that decides it: name one field that is really a thing of its own. A book’s author has a name and a birth year — so is author a string, or another struct? There is no single right answer, and arguing about it is precisely the design work your catalog is describing.

what to keep

three things worth remembering

01

Group what belongs together

Parallel arrays drift apart the moment anything is sorted. A struct cannot.

02

Which one, then which part

cls[1].score reads exactly as you would say it out loud.

03

The semicolon after }

}; ends a struct. Forgetting it produces an error about the next line entirely.

check yourself

3 questions before you move on

Not recall — these are the shapes an exam actually uses. Every answer below was produced by compiling and running the code, so if you disagree with one, the compiler is the one to believe.

▢ check yourself3 questions

Have a real go before revealing. Being wrong here is worth more than being right in three weeks.

01

You keep names, scores and ages in three separate arrays. What goes wrong first?

02

What does cls[1].score mean?

03

What is easy to forget when defining a struct?

answered: 0 of 3right first time: 0
Stuck? Peter reads these personally and replies to your email.
Ask Peter →