← C++ from Scratch
Lesson 36 of 62 · 10 XP
Classes and objects
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
class Hero {
public: // accessible from the outside
std::string name;
int hp = 100;
};
Hero h;
h.name = "Kai";
h.hp -= 30;
Hero party[3]; // an array of objects
Each object has its own copies of the fields: changing h.hp doesn't touch other objects.
A class is a type, like int: it can be passed to functions and stored in arrays and vectors.
Task. Create a class Point with the fields x, y (integers). Read n points and print the one closest
to the origin, in the format (x, y). If there's a tie, print the first one.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.