Classes - function that will access another class?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <vector>
using namespace std;

class NPC
{
protected:
	int hp, mana, level, damage;
public:
	void DealDamage(NPC, int);
	void SetLevel(int);
	NPC();
};

NPC::NPC()
{
	level = 1;
	hp = level*10;
	mana = level*5;
	damage = level*2;
}

void NPC::DealDamage(NPC npc, int dmg)
{
}

void NPC::SetLevel(int i)
{
	level = i;
	hp = level*10;
	mana = level*5;
	damage = level*2;
}

int main()
{
	vector<NPC> npc(2);
	// DealDamage() with npc[0] to npc[1]
	system("pause");
	return 0;
}


how should I write my DealDamage function, that will actually deal damage to the second NPC?
1
2
3
4
5
6
7
8
9
10
11
void NPC::DealDamage(NPC& npc, int dmg)
{
    npc.hp -= dmg;
}

int main()
{
    vector<NPC> npc(2);
    // DealDamage() with npc[0] to npc[1]
    npc[0].DealDamage(npc[1], 10);
}
but hp is protected, not public :X

nvm, got a way around that, thanks
Last edited on
but hp is protected, not public :X
And? I am accessing it from inside class.
Topic archived. No new replies allowed.