Need help with Classes

#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;

class Tester
{
int health = 100;
public:
int damage();
int access();
private:
int value(int x);
};
int Tester::damage()
{
health;
health - 20;
return health;
}

int Tester::value(int x)
{
Tester::health = x;
return health;
}

int Tester::access()
{
return value(health);
}

int main()
{
Tester Play;
cout << "Your health is currently: " << Play.access() << endl;
cout << "You take 20 damage\n";
Play.damage();
cout << "Your health is now " << Play.access();
}

Everything works fine but one thing. When the health displays, it says 100. When it displays the second time, its till 100, but should be 80. Please help me with anything i did wrong.
Last edited on
Hi, the statements
1
2
health;
health - 20;

do not change any state or variable of your program - they don't do anything. To change your state, you must have an assignment (=) in there.

Please use code tags. Put [code] {code here} [/code] around your C++ code.

Change your damage member function to this:
1
2
3
4
5
int Tester::damage()
{
    health = health - 20;  // subtracts 20 from health,
                     // and assigns the new value back to health
}
Last edited on
Topic archived. No new replies allowed.