C++ · ОСНОВЫ · LESSON 19

Item with properties

struct · about 12 minutes

Learning with progressRead the summary for free. To save the work, log in to the account.
STEP 1

How it works.

struct describes the subject. Inside, you can name its properties, such as name and age.

struct Pet {
    string name;
    int age;
};

✦ After the closing bracket struct put a semicolon.

STEP 2

Try to understand.

After the closing bracket struct put a semicolon.

Pet dog = {"Бим", 3};
cout << dog.name << endl;

✦ Compare this to the first one. Then do the cool job yourself.

IN-CLASS PRACTICE

Type the code yourself

Manually type the program from the sample. So you'll remember commands and brackets.

#include <iostream>
#include <string>
using namespace std;

struct Pet {
    string name;
    int age;
};

int main() {
    Pet dog = {"Бим", 3};
    cout << dog.name << endl;
    return 0;
}
Open the editor for class work
HOMEWORK

Solve it yourself

Create struct Pet with string name and int age properties. Create pet with the name «Кот» and age 4; output pet.age.

The answer is hidden. Use similar examples from the lesson notes.

Opening up your homework