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

Getters and const Methods

Now that the health is private, main can't even read it: std::cout << aria.health ends with the same error as changing it. But the rest of the game needs to know the health, for example to draw a health bar. So the class gets a public method that returns it:

int getHealth() const {
    return health;
}

A method that returns the value of a private member is called a getter, and its name usually starts with get. A method that answers a yes-or-no question about the object returns a bool, and its name starts with is:

bool isAlive() const {
    return health > 0;
}

With them, main can read the health with aria.getHealth() and ask if (aria.isAlive()), but it still has no way to set the health to -430.

The word const after the parentheses makes the method a const method: it promises not to change its object, and the compiler checks the promise. If getHealth stored something in a member, for example health = 0;, compiling would end with the error cannot assign to non-static data member within const member function 'getHealth'. A non-static data member is an ordinary member such as health, and a member function is a method.

Why promise anything? Remember const references: a function that only reads a hero takes const Hero& hero. Through a const reference, you can call only const methods, because only they promise to leave the hero as it is. A function void report(const Hero& hero) that calls hero.takeDamage(5) doesn't compile: 'this' argument to member function 'takeDamage' has type 'const Hero', but function is not marked const. The 'this' argument is the object before the dot. Here it is const, and takeDamage isn't a const method. That's right, because it changes the hero. But if printStatus weren't marked const, the function couldn't call it either, even though it only reads. So mark every method that doesn't change its object as const.

Instructions

  1. 1

    Add a public const method getHealth() that returns the hero's health. In main, below the line with printStatus, print the text Health: and what aria.getHealth() returns. The program prints Aria: health 65 and then Health: 65. Keep the line with aria.takeDamage(35); as it is: the exercise will also try other values.

  2. 2

    Add a public const method isAlive() that returns true when the hero has more than 0 health. At the end of main, a swarm of bats attacks: in a while loop with the condition aria.isAlive(), Aria takes 30 damage in each iteration. Count the hits, and after the loop, print Hits taken: 3.

  3. 3

    The method printStatus only reads the hero too, so mark it const. Check that all three methods that don't change the hero are const. The program prints the same as before.

Back

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