Summary
In this lesson, you learned that:
- A method is a function written inside a struct or a class. You call it on an object with a dot,
aria.printStatus(), and inside it, the names of the members mean the members of that object. - A variable of a struct or class type is an object, and each object has its own members.
- A method can change its object,
aria.takeDamage(30), without any&. A rule written in the method, such as "the health never drops below 0", holds wherever the program calls it. - In a class, the members below
private:can be used only by the class's own methods, and those belowpublic:by the whole program. Using a private member from outside ends with the error'health' is a private member of 'Hero'. A struct is public unless you say otherwise, and a class private. - A getter such as
int getHealth() constlets the rest of the program read a private member, and a method such asbool isAlive() constanswers a question about the object. - A const method, with
constafter the parentheses, promises not to change its object, and the compiler checks it. Through aconst Hero&, and in a loop overconst Monster&, only const methods can be called. Mark every method that only reads asconst. - A constructor has the name of the class and no return type. It runs whenever an object is created,
Hero aria("Aria", 100);, and stores its parameters, which have names of their own, in the members. Once a class has a constructor,Hero hero;without values doesn't compile. - Keeping the data private, with the rules about it in the class's methods, is called encapsulation. When a rule changes, you change it in one place. A method can call the other methods of its object by their names:
isAlive(). - A method can take another object as a parameter, by reference when it changes it:
void hit(Monster& target). It can use only the public methods of the other class, and that class has to be defined above it. - Objects go into a vector as calls of the constructor:
{Monster("Goblin", 30), Monster("Orc", 50)}. Loop over them withMonster&to change them, and withconst Monster&to read them.
Next comes a small project, Checkpoint: Monster Arena, in which your hero fights a whole line of monsters with what you know about strings, structs and classes, and after it, the lesson on maps.
Instructions
- 1
Let's practice. Above
main, write a classPursefor the hero's gold: a private membergold, a constructor that takes the starting gold, a methodadd(int amount)that adds gold, and a const gettergetGold(). Inmain, create a purse withstartGold, add therewardto it, and printGold: 80. Keep the four lines at the top ofmainas they are: the exercise will also try other values. - 2
Add a method
spend(int amount): when the purse holds at leastamountgold, it takes the amount out, and otherwise it changes nothing and printsNot enough gold. Below the line that prints the gold, buy a sword forswordPriceand then a shield forshieldPrice, and print the gold again. The program printsGold: 80,Not enough goldandGold: 20.