← C++ from Scratch Lesson 35 of 62 · 5 XP

Warm-up: a hero warm-up

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

A class describes what an object is made of. Fields after public: can be reached from outside with a dot:

class Hero {
public:
    std::string name;
    int hp;
};

Hero h;
h.name = "Kai";
h.hp = 100;
std::cout << h.name << ": " << h.hp << " HP\n";   // Kai: 100 HP

Task. The Hero class is already there. Read a name and health into the object's fields and print name: N HP.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.