What Is a Struct?
So far, every value in your programs has had a type that C++ already knows: int, double, std::string, a vector. In this lesson, you will create types of your own.
Think of a monster in a role-playing game. It has a name, health and an attack. With what you know so far, that's three variables for every monster:
std::string goblinName = "Goblin";
int goblinHealth = 30;
int goblinAttack = 5;For ten monsters, you would need thirty variables, and only their names would say which ones belong together. Vectors don't solve it either. In the playlist checkpoint, you had two vectors, songs and lengths, and only the index tied a song to its length. Add a song to one vector, forget the other, and the songs no longer match their lengths.
A struct (short for structure) groups values that belong together into one new type. First you describe what every monster has:
struct Monster {
std::string name;
int health;
int attack;
};This is the definition of the struct. struct is a keyword, and Monster is the name of the new type. Names of your own types start with a capital letter, so that you can tell them from variables. Inside the curly braces are the struct's members, each written like a variable, with a type and a name. Write the definition above main, so that the compiler knows the type before you use it.
The definition creates no monster yet. It's like an empty form with the fields Name, Health and Attack. Now you can create a variable of the type Monster, as you create one of the type int, and fill in its members. You reach a member with a dot: goblin.health is the goblin's health.
#include <iostream>
#include <string>
struct Monster {
std::string name;
int health;
int attack;
};
int main() {
Monster goblin;
goblin.name = "Goblin";
goblin.health = 30;
goblin.attack = 5;
std::cout << goblin.name << " has " << goblin.health << " health." << std::endl;
return 0;
}This prints:
Goblin has 30 health.A member works like an ordinary variable of its type: goblin.health -= 10; or goblin.attack++; change it, and the other members stay as they were.
The most common mistake is the semicolon after the closing brace. A function has none there, but a struct must have one. Without it, the program above doesn't compile:
main.cpp:8:2: error: expected ';' after struct
8 | }
| ^
| ;
1 error generated.And a typo in a member's name, such as goblin.helth, ends with no member named 'helth' in 'Monster': the compiler knows only the members from the definition.
Instructions
- 1
The program creates an item for the game's shop, but the type
Itemdoesn't exist yet, so it doesn't compile. In place of the comment, define the structItemwith two members:nameof typestd::stringandpriceof typeint. The program then printsSword costs 50 gold.Keep the linesword.price = 50;as it is: the exercise will also try another price. - 2
The shop raises its prices. At the end of
main(beforereturn 0;), add20to the sword's price with+=, and print the same line again. The program printsSword costs 50 gold.and thenSword costs 70 gold.