Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

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 below public: 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() const lets the rest of the program read a private member, and a method such as bool isAlive() const answers a question about the object.
  • A const method, with const after the parentheses, promises not to change its object, and the compiler checks it. Through a const Hero&, and in a loop over const Monster&, only const methods can be called. Mark every method that only reads as const.
  • 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 with Monster& to change them, and with const 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. 1

    Let's practice. Above main, write a class Purse for the hero's gold: a private member gold, a constructor that takes the starting gold, a method add(int amount) that adds gold, and a const getter getGold(). In main, create a purse with startGold, add the reward to it, and print Gold: 80. Keep the four lines at the top of main as they are: the exercise will also try other values.

  2. 2

    Add a method spend(int amount): when the purse holds at least amount gold, it takes the amount out, and otherwise it changes nothing and prints Not enough gold. Below the line that prints the gold, buy a sword for swordPrice and then a shield for shieldPrice, and print the gold again. The program prints Gold: 80, Not enough gold and Gold: 20.

Back

11/11

  1. 1. Methods
  2. 2. Methods That Change the Object
  3. 3. Private and Public Members
  4. 4. Getters and const Methods
  5. 5. Constructors
  6. 6. Quiz: Constructors
  7. 7. Rules in One Place
  8. 8. Objects as Parameters
  9. 9. A Vector of Objects
  10. 10. Quiz: Damage in a Loop
  11. 11. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.