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.
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.
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.
}; — 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.
Click a row, then a field. The expression that reaches it appears underneath.
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.
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:
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.
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.
Name a thing: a library book, a train journey, a recipe, a football match. List the fields and their types.
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.
Parallel arrays drift apart the moment anything is sorted. A struct cannot.
cls[1].score reads exactly as you would say it out loud.
}; ends a struct. Forgetting it produces an error about the next line entirely.
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.
Have a real go before revealing. Being wrong here is worth more than being right in three weeks.
You keep names, scores and ages in three separate arrays. What goes wrong first?
What does cls[1].score mean?
What is easy to forget when defining a struct?
← back to the whole C++ track · stuck on anything? ask peter.