Question about class access functions

I'm trying to make a relatively simple game, and i'm working on how the inventory system will work. The "loot" in this game is actually paintings, and I have my class set up like this (there's a lot of code that I cut out to limit confusion):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class painting
{
	public:
		void addAbuse();
		void setOverallCondition();

	private:
		int damageFade;
		int damageMedium;
		int damageCanvas;

		int paintingFragility;

		int overallCondition;
};


I want to make a function that essentially increases one of the damage stats (at random between the 2) by the painting's fragility rating. Right now it looks like this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void painting::addAbuse()
{
	int n = (1+ (rand() % 2))

	switch (n)
	{
		case 1: damageMedium += paintingFragility;
			break;

		case 2: damageCanvas += paintingFragility;
			break;

		default: cout << "Error applying damage to stats" << endl;
			break;
	};
}


I have a lot to work out before I can compile and test this (I'm also doing this during my lunch break at work, so I'm using nothing but notepad and can't really compile until later), but I wanted to know if this technique would work, or if I'm missing something. Also, what would be the best way to make the overallCondition dependent on the other damage stats? Right now I have a function that looks like this:

1
2
3
4
5
void painting::setOverallCondition()
{
	overallCondition = damageFade + damageMedium + damageCanvas;

}


With this, I'll have to run target.setOverallCondition() every time the painting takes damage. Is there a way to have this happen automatically whenever the damage stats are increased?

Thanks in advance.
Write your code so that the only way to increase a damage stat is via a function call. The last thing that function does will be to call setOverallCondition().
so i can embed setOverallCondition() to run at the end of addAbuse()? wasn't sure if that was legal in c++
Topic archived. No new replies allowed.