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

Private and Public Members

The rule in takeDamage holds only as long as everyone uses the method. Nothing stops a line such as aria.health -= 500; in main, and the hero suddenly has -430 health. In a small program you would notice. But a game has thousands of lines, often written by several people, and sooner or later somebody changes the health directly.

C++ can forbid that. Write class instead of struct, and divide the type into two parts:

class Hero {
private:
    std::string name = "Aria";
    int health = 100;

public:

    void takeDamage(int amount) {
        health -= amount;
        if (health < 0) {
            health = 0;
        }
    }

    void printStatus() {
        std::cout << name << ": health " << health << std::endl;
    }
};

Everything below private: is private: only the methods of the class itself can use it. Everything below public: is public: the whole program can use it. Note the colon after both words. Now main can call aria.takeDamage(500), but the line aria.health -= 500; doesn't compile. The error says error: 'health' is a private member of 'Hero', and a note below it, note: declared private here, points to where the member is. The compiler now makes sure the rule holds: the only way to change the health is through the methods, and they keep it at 0 or above.

A type made with class is a class, and its variables are objects, as with structs. For what this course does, struct and class differ only in what you get without private: and public:: in a struct, everything is public, and in a class, everything is private. Programmers use structs for simple data that anyone may change, and classes for types that have rules to protect. This course writes the private members first and the public methods after them.

Private members can't be filled in from outside, so braces don't work any more: Hero aria = {"Aria", 100}; ends with the error no matching constructor for initialization of 'Hero'. That's why this hero gets default values, and Hero aria; creates it with them. Soon you'll learn a better way, the constructor that the error message mentions.

Instructions

  1. 1

    The program prints Aria: health 70 and then Aria: health -430, because main changes the health directly. Replace the line aria.health -= 500; with a call of takeDamage that takes the same 500 health. The last line is now Aria: health 0. Keep the line with int health = 100; as it is: the exercise will also try other values.

  2. 2

    So that this can't happen again, turn Hero into a class: write class instead of struct, put the two members below private: and the two methods below public:. The program prints the same as before. Then try writing aria.health -= 500; back into main, look at the error, and delete the line again.

Back

3/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.