class question

I would pls like to know how to use one class in a nother
for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class player
{
public:
	player():
	  strength(5),
	  health(100),
	  experience(0)
	{
	
	}
	void info();
	void attack();
	void heal();
	void experience();

private:
	int age;
	string name;
	int strength;
	int health;
	int experience;
	int level;
};s


then there's another class:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class enemy
{
public:
	enemy(int enemyStrength, int enemyHealth):
	  strength(enemyStrength),
	  health(enemyHealth)
	{

	}

	void attack();

private:
	int strength;
	int health;
};  


now I would like to use the class enemy with the class player but the thing about that is, I don't know how :( pls help me with that, sorry if I sound like a noob...
Last edited on
Well if you don't know the relation you want between an ennemy and a player, we can't help much.
Can a player be an ennemy? Does a player have one ennemy? Or multiple ennemies? Do you need to access the ennemy's data from the player, or the opposite?
the player is not an enemy, he's supposed to fight the enemy, there are going to be multiple enemies, and I need to access the player from the enemy and the enemy from the player, because I have to somehow make the enemy's health drain once he get's hit by the player and the same the other way around, it would be great if I could get an answer to this.
I think this is what you want.

http://www.cplusplus.com/doc/tutorial/inheritance/

Hope this helps. :)
Last edited on
1
2
3
4
5
6
7
8
9
10
11
class enemy;

class player{
friend class enemy;
/*rest of the code*/
};

class enemy {
friend class player;
/*rest of the code*/
};


to make a friend only for one function:
1
2
3
4
5
6
7
8
class enemy {
//...
};

class player{
friend void enemy::attack();
/*rest of the code*/
};
Last edited on
will that also work if I put enemy in enemy.h and player in player.h?
I tried using #include "player.h" and #include "enemy.h" in both .h files but it didn't seem to work out :(
the rule says so:

1.declaration of the function member must be known before you set it as a friend of another class.
to be able to do that include that class into header of another class.

2.also declaration of the class must be known before you set it as a friend of another class.

3.structure of the class must be known for member function which want to access class members

I hope you understand my buggy english :D

it depends what your functins do acctualy.
Last edited on
codekiddy said this like a golden trophy that shown the world who is first. (He said it excellent). And now if you follow what he said, bubp! You fixed yourself a problem!
so, lol, do I use the #include statement to include the enemy class from enemy.h into player.h?
Or do I just include the enemy class into player.h with copy and paste?
sorry for so many questions.
I don't think the player and enemy should be friends. You need to provide the required public functions to get it to work.

Maybe your attack() method should take a parameter? What you're attacking?
Last edited on
The player is supposed to attack the enemy, once the enemy gets hit his health drains, let's say the player has 12 strength, then I want the enemy's health to drain by 12 every time he get's hit
and when the player gets hit, his health drains...
that's about as good as I could explain it.
Last edited on
You have great stuff here, truly informative. Very well written I shall be bookmarking your blog and subscribing to your feed so i can regularly read posts of this quality.
You have great stuff here, truly informative. Very well written I shall be bookmarking your blog and subscribing to your feed so i can regularly read posts of this quality.


what?
what?

haha :D
it looks your problem has become world wide popular lol.
haha :D
it looks your problem has become world wide popular lol.


of course it has! :D

but again back to the real problem, pls I'm really trying hard to solve it.
Thanks for the help guys, I finally solved the problem!

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
#include <iostream>
#include <string>
#include "enemy.h"
using namespace std;

class player
{
public:
	player();
	void info();
	void attack();
	void heal();
	void levelup();
	void playerhit();

private:
	int age;
	string name;
	int strength;
	int health;
	int experiencePoints;
	int level;
	int healStrength;
	int maxHealth;
};


then the enemy class:

1
2
3
4
5
6
7
8
9
10
11
12
13
class enemy
{
public:
	enemy(int enemyStrength, int enemyHealth);
	void attack(int playerHealth);
	void enemyhit(int playerStrength);
	void strengthenemy();
	void healthenemy();

private:
	int strength;
	int health;
};



then the main:

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
#include "stdafx.h"
#include "player.h"

int main()
{
	cout << "hello, and welcome to the Arena stranger!\n";
	player me;
	me.info();


	do
	{
		
		cout << "a - attack\n";
		cout << "h - heal\n";
		cout << "p - playerstats\n";
		cout << "x - quit\n";
		char x;
		cin >> x;
		if(x == 'a')
		{
			me.attack();
		}

	}
	while(5);

	return 0;
}



I first included enemy.h into player.h, then I included player.h into the main .cpp file and the other class .cpp files but that's not important now and now I am going to add parameters to both the enemy and the player classes, thanks for the help! :D

(I added the do{} while(5) just so that I wouldn't have to give it anything that could end the game in an inconvenient time :))
Last edited on
Topic archived. No new replies allowed.