Class interaction

Okay, I have this character class and I want to know about creating a monseter class that can access the character level and base it's stats off of character level. Is this legal/recommended?

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#ifndef H_character
#define H_character
#include <iostream>
#include <string>
#include <iomanip>
#include <ctime>
#include <cstdlib>

using namespace std;

class character
{
	friend ostream& operator<<(ostream&, const character&);
	friend istream& operator>>(istream&, character&);
public:
	character& setName(string);
	void takeDamage(int);
	bool isDead();
	bool isLevelUp();
	void levelUp();
	void addExperience(int);
	void addMoney(int);
	void takeMoney(int);
	string getName() const;
	int getMoney() const;
	int getLevel() const;
	void replenishHealth();
	void addStrength(int);
	void addAgility(int);
	void addPotion();
	int damageRoll();
	character();
private:
	int health;
	int strength;
	int money;
	int level;
	int agility;
	string name;
	int experience;
	int healingPotions;
};

character& character::setName(string input)
{
	name = input;
	return *this;
}

ostream& operator<<(ostream& out, const character& print)
{
	out<<left<<endl;
	out<<setw(19)<<"Stats for "<<print.name<<endl;
	out<<setw(19)<<"Strength:"<<print.strength<<endl;
	out<<setw(19)<<"Agility:"<<print.agility<<endl;
	out<<setw(19)<<"Experience:"<<print.experience<<endl;
	out<<setw(19)<<"Level"<<print.level<<endl;
	out<<setw(19)<<"Health:"<<print.health<<endl;
	out<<setw(19)<<"Gold:"<<print.money<<endl;
	out<<setw(19)<<"Healing Potions:"<<print.healingPotions<<endl;
	return out;
}

istream& operator>>(istream& in, character& input)
{
	in>>input.name;
	return in;
}

void character::takeDamage(int num1)
{
	health -= num1;
}

bool character::isDead()
{
	return (health <= 0);
}

character::character()
{
	name = "";
	health = 100;
	strength = 5;
	agility = 5;
	money = 100;
	experience = 0;
	level = 1;
	healingPotions = 0;
}

bool character::isLevelUp()
{
	if(experience >= 100)
	{
		levelUp();
		return true;
	}
	return false;
}

void character::levelUp()
{
		cout<<"Congratulation! "<<name<<" has gained a level!"<<endl;
		experience = 0;
		strength++;
		agility++;
		level++;
		health = 100;
}

void character::addExperience(int num1)
{
	experience += num1;
}

void character::addMoney(int num1)
{
	money += num1;
}

string character::getName() const
{
	return name;
}

int character::getLevel() const
{
	return level;
}

int character::getMoney() const
{
	return money;
}

void character::takeMoney(int num1)
{
	money -= num1;
}

int character::damageRoll()
{
	int num = (rand() + time(0)) % 5;
	return (strength + (agility / 2) + num);
}

void character::replenishHealth()
{
	health = 100;
}

void character::addAgility(int num1)
{
	agility += num1;
}

void character::addStrength(int num1)
{
	strength += num1;
}

void character::addPotion()
{
	healingPotions++;
}
#endif




i.e. I want to create a seperate class that increases its base stats as the character object grows in level. Is this feasible and should I do it or approach this in another way?
I think that's reasonable in the object-oriented sense. "A monster is a type of character", right? In this case inheritance should work well. Having a quick look at the attributes of Character, I can't see any that wouldn't really be appropriate for a monster, so I say go for it (although, what do monsters do with cash haha?).

For the Monster class you would need to override the relevant methods to get the appropriate behaviour (e.g. the << operator for printing etc).

Hope that helps.
I was actually thinking of creating a completely seperate class because the monster would use almost none of the functions or variables. And instead of moving character level to a public or protected member, I am thinking of creating a global variable in main that increases with the level and have the monster class access that variable.
Hit me with some guidelines and opinions, I know I'm going to get beat up for giving some of my functions too much responsibility heh. Please let me know what you all think. I also wasn't thinking when I first started coding this or I would have derived character from monster.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#ifndef H_character
#define H_character
#include <iostream>
#include <string>
#include <iomanip>
#include <ctime>
#include <cstdlib>

using namespace std;

class character
{
	friend ostream& operator<<(ostream&, const character&);
	friend istream& operator>>(istream&, character&);
public:
	character& setName(string);
	void takeDamage(int);
	bool isDead();
	bool isLevelUp();
	void levelUp();
	void addExperience(int);
	void addMoney(int);
	void takeMoney(int);
	void takePotion();
	string getName() const;
	int getMoney() const;
	int getHealingPotions() const;
	int getHealth() const;
	int getLevel() const;
	void replenishHealth();
	void addStrength(int);
	void addAgility(int);
	void addPotion();
	void addHealth(int);
	int damageRoll();
	character();
private:
	int health;
	int strength;
	int money;
	int level;
	int agility;
	string name;
	int experience;
	int healingPotions;
};

character& character::setName(string input)
{
	name = input;
	return *this;
}

ostream& operator<<(ostream& out, const character& print)
{
	out<<left<<endl;
	out<<setw(19)<<"Stats for "<<print.name<<endl;
	out<<setw(19)<<"Strength:"<<print.strength<<endl;
	out<<setw(19)<<"Agility:"<<print.agility<<endl;
	out<<setw(19)<<"Experience:"<<print.experience<<endl;
	out<<setw(19)<<"Level"<<print.level<<endl;
	out<<setw(19)<<"Health:"<<print.health<<endl;
	out<<setw(19)<<"Gold:"<<print.money<<endl;
	out<<setw(19)<<"Healing Potions:"<<print.healingPotions<<endl;
	return out;
}

istream& operator>>(istream& in, character& input)
{
	in>>input.name;
	return in;
}

void character::takeDamage(int num1)
{
	health -= num1;
}

bool character::isDead()
{
	return (health <= 0);
}

character::character()
{
	name = "";
	health = 100;
	strength = 5;
	agility = 5;
	money = 100;
	experience = 0;
	level = 1;
	healingPotions = 0;
}

bool character::isLevelUp()
{
	return (experience >= 100);
}

void character::levelUp()
{
		cout<<"Congratulation! "<<name<<" has gained a level!"<<endl;
		experience = 0;
		strength++;
		agility++;
		level++;
		health = 100;
}

void character::addExperience(int num1)
{
	experience += num1;
}

void character::addMoney(int num1)
{
	money += num1;
}

string character::getName() const
{
	return name;
}

int character::getHealingPotions() const
{
	return healingPotions;
}

int character::getLevel() const
{
	return level;
}

int character::getMoney() const
{
	return money;
}

void character::takeMoney(int num1)
{
	money -= num1;
}

int character::damageRoll()
{
	int num = (rand() + time(0)) % 5;
	return (strength + (agility / 2) + num);
}

void character::replenishHealth()
{
	health = 100;
}

void character::addAgility(int num1)
{
	agility += num1;
}

void character::addStrength(int num1)
{
	strength += num1;
}

void character::addPotion()
{
	healingPotions++;
}

void character::takePotion()
{
	healingPotions--;
}

int character::getHealth() const
{
	return health;
}

void character::addHealth(int num1)
{
	health += num1;
}
#endif 



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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifndef H_monster
#define H_monster
#include <iostream>
#include <string>
#include <iomanip>
#include <ctime>
#include <cstdlib>

using namespace std;

class monster
{
	friend ostream& operator<<(ostream&, const monster&);
public:
	void setName(string);
	void setStrength(int);
	void setAgility(int);
	void setHealth(int);
	bool isDead();
	void takeDamage(int);
	int damageRoll();
	string getName() const;
	monster();
private:
	string name;
	int strength;
	int agility;
	int health;
};

ostream& operator<<(ostream& out, const monster& print)
{
	out<<left<<endl;
	out<<setw(15)<<"Type:"<<print.name<<endl;
	out<<setw(15)<<"Strength:"<<print.strength<<endl;
	out<<setw(15)<<"Agility:"<<print.agility<<endl;
	out<<setw(15)<<"Health:"<<print.health<<endl;
	return out;
}

void monster::setName(std::string input)
{
	name = input;
}

void monster::setStrength(int num1)
{
	strength = num1;
}

void monster::setAgility(int num1)
{
	agility = num1;
}

void monster::setHealth(int num1)
{
	health = num1;
}

monster::monster()
{
	name = "";
	strength = 0;
	agility = 0;
	health = 1;
}

bool monster::isDead()
{
	return (health <= 0);
}

void monster::takeDamage(int num1)
{
	health -= num1;
}

int monster::damageRoll()
{
	int num = (rand() + time(0)) % 3;
	return ((strength / 2) + (agility / 2) + num);
}

string monster::getName() const
{
	return name;
}
#endif



Main follows:
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <iostream>
#include "character.h"
#include "monster.h"
#include <vector>

using namespace std;

void instructions();
void createNew(character&);
void itemShop(character&);
void strengthPotions(character&);
void agilityPotions(character&);
void healingPotions(character&);
void combat(character&);
void fight(character&);

int main()
{
	int choice;
	instructions();
	character player1;
	do
	{
		cout<<"\nTo create a new character, press 1.";
		cout<<"\nTo venture into the wilderness to battle monsters, press 2.";
		cout<<"\nTo visit the shop for upgrades and potions, press 3.";
		cout<<"\nTo print your character's stats, press 4.";
		cout<<"\nTo exit, press 9."<<endl;
		cin>>choice;
		switch(choice)
		{
		case 1:
			createNew(player1);
			break;
		case 2:
			combat(player1);
			break;
		case 3:
			itemShop(player1);
			break;
		case 4:
			if(player1.getName() == "")
				cout<<"\nA character has not been created yet.\n";
			else
				cout<<player1;
			break;
		default:
			cout<<"\nInvalid input.\n"<<endl;
		}
	}while (choice != 9);
	return 0;
}

void instructions()
{
	cout<<"Welcome to the character combat system.";
	cout<<"\nA player may create a character and progress"; 
	cout<<"\nthrough levels and earn money for power ups";
	cout<<"\nand stat increasing potions."<<endl;
}

void createNew(character& newPlayer)
{
	char check;
	if(newPlayer.getName() == "")
	{
		cout<<"\nEnter a name for the new character: "<<endl;
		cin>>newPlayer;
	}
	else if(newPlayer.getName() != "")
	{
		cout<<"\nA player already exists. Creating a new one";
		cout<<"\nwill delete the old one. Proceed? y/n "<<endl;
		cin>>check;
		if(check == 'y' || check == 'Y')
		{
			cout<<"\nEnter a name for the new character: "<<endl;
			cin>>newPlayer;
		}
		else if(check == 'n' || check == 'N')
			cout<<"\nReturning to main menu.\n";
	}
}

void itemShop(character& buyer)
{
	int choice;
	do
	{
	    if(buyer.getName() == "")
		{
		    cout<<"\nCharacter has not been created yet.";
		    cout<<"\nReturning to main menu.\n";
		    break;
		}
		cout<<"\nFor strength potions, press 1.";
		cout<<"\nFor agility potions, press 2.";
		cout<<"\nFor healing potions, press 3.";
		cout<<"\nTo heal yourself at the tavern for 25 gold, press 4.";
		cout<<"\nTo exit the shop, press 5."<<endl;
		cin>>choice;
		switch(choice)
		{
		case 1:
			strengthPotions(buyer);
			break;
		case 2:
			agilityPotions(buyer);
			break;
		case 3:
			healingPotions(buyer);
			break;
		case 4:
			if(buyer.getMoney() < 25)
				cout<<"You don't have enough money!"<<endl;
			else
			{
				buyer.replenishHealth();
				buyer.takeMoney(25);
				cout<<buyer.getName()<<"'s health is replenished."<<endl;
			}
			break;
		}
	}while(choice != 5);
}

void strengthPotions(character& player)
{
	int choice;
	do
	{
		cout<<"\nTo purchase a strength +1 potion for 75 gold, press 1.";
		cout<<"\nTo purchase a strength +5 potion for 250 gold, press 2.";
		cout<<"\nTo purchase a strength +10 potion for 450 gold, press 3.";
		cout<<"\nTo purchase a strength +20 potion for 700 gold, press 4.";
		cout<<"\nTo print stats, press 5.";
		cout<<"\nTo exit strength potion selection, press 9.";
		cin>>choice;
		switch(choice)
		{
		case 1:
			if(player.getMoney() < 75)
				cout<<"\nYou don't have enough gold!"<<endl;
			else
			{
				player.addStrength(1);
				player.takeMoney(75);
				cout<<"\nStrength increased by 1."<<endl;
			}
			break;
		case 2:
			if(player.getMoney() < 250)
				cout<<"\nYou don't have enough gold!"<<endl;
			else
			{
				player.addStrength(5);
				player.takeMoney(250);
				cout<<"\nStrength increased by 5."<<endl;
			}
			break;
		case 3:
			if(player.getMoney() < 450)
				cout<<"\nYou don't have enough gold!"<<endl;
			else
			{
				player.addStrength(10);
				player.takeMoney(450);
				cout<<"\nStrength increased by 10."<<endl;
			}
			break;
		case 4:
			if(player.getMoney() < 700)
				cout<<"\nYou don't have enough gold!"<<endl;
			else
			{
				player.addStrength(20);
				player.takeMoney(700);
				cout<<"\nStrength increased by 20."<<endl;
			}
			break;
		case 5:
			cout<<player;
			break;
		}
	}while(choice != 9);
}
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
void agilityPotions(character& player)
{
	int choice;
	do
	{
		cout<<"\nTo purchase an agility +3 potion for 50 gold, press 1.";
		cout<<"\nTo purchase an agility +10 potion for 220 gold, press 2.";
		cout<<"\nTo purchase an agility +20 potion for 400 gold, press 3.";
		cout<<"\nTo purchase an agility +50 potion for 700 gold, press 4.";
		cout<<"\nTo print stats, press 5.";
		cout<<"\nTo exit agility potion selection, press 9.";
		cin>>choice;
		switch(choice)
		{
		case 1:
			if(player.getMoney() < 50)
				cout<<"\nYou don't have enough gold!"<<endl;
			else
			{
				player.addAgility(3);
				player.takeMoney(50);
				cout<<"\nAgility increased by 3."<<endl;
			}
			break;
		case 2:
			if(player.getMoney() < 220)
				cout<<"\nYou don't have enough gold!"<<endl;
			else
			{
				player.addAgility(10);
				player.takeMoney(220);
				cout<<"\nAgility increased by 10."<<endl;
			}
			break;
		case 3:
			if(player.getMoney() < 400)
				cout<<"\nYou don't have enough gold!"<<endl;
			else
			{
				player.addAgility(20);
				player.takeMoney(400);
				cout<<"\nAgility increased by 20."<<endl;
			}
			break;
		case 4:
			if(player.getMoney() < 700)
				cout<<"\nYou don't have enough gold!"<<endl;
			else
			{
				player.addAgility(50);
				player.takeMoney(700);
				cout<<"\nAgility increased by 50."<<endl;
			}
			break;
		case 5:
			cout<<player;
			break;
		}
	}while(choice != 9);
}

void healingPotions(character& player)
{
	int choice;
	do
	{
		cout<<"To purchase potions for the road at 50 gold each, press 1.";
		cout<<"\nTo exit potion selection, press 9."<<endl;
		cin>>choice;
		switch(choice)
		{
		case 1:
			if(player.getMoney() < 50)
				cout<<"n\You don't have enough gold!"<<endl;
			else
			{
				player.addPotion();
				player.takeMoney(50);
				cout<<"Added healing potion to your inventory."<<endl;
			}
			break;
		}
	}while(choice != 9);
}

void combat(character& player)
{
	int choice;
	do
	{
		if(player.getName() == "")
		{
			cout<<"\nA character has not been created.";
			cout<<"\nReturning to the main menu.\n";
			break;
		}
		cout<<"\nTo search for a monster, press 1.";
		cout<<"\nTo drink a potion, press 2.";
		cout<<"\nTo print character stats, press 3.";
		cout<<"\nTo return to town (the main menu), press 9."<<endl;
		cin>>choice;
		switch(choice)
		{
		case 1:
			fight(player);
			break;
		case 2:
			if(player.getHealingPotions() <= 0)
				cout<<player.getName()<<" is out of healing potions."<<endl;
			else
			{
				player.takePotion();
				player.replenishHealth();
				cout<<player.getName()<<"'s health is now full."<<endl;
			}
			break;
		case 3:
			cout<<player;
			break;
		}
	}while(choice != 9);
}

void fight(character& player)
{
	vector<string> mobList;
	vector<string> terrain;
	monster baddie;
	mobList.push_back("Skeleton");
	mobList.push_back("Kobold");
	mobList.push_back("Zombie");
	mobList.push_back("Ghoul");
	mobList.push_back("Beholder");
	mobList.push_back("Dragon");
	terrain.push_back("Forest");
	terrain.push_back("Plains");
	terrain.push_back("Mountains");
	terrain.push_back("Swamp");
	terrain.push_back("Island");
	terrain.push_back("Castle");
	int choice;
	int mob = (rand() + time(0)) % 5;
	int area = (rand() + time(0)) % 5;
	int statMultiplier = player.getLevel() * 3;
    int healthMultiplier = player.getLevel() * 20;
	int goldMultiplier = player.getLevel() * 10;
	baddie.setName(mobList[mob]);
	baddie.setAgility(statMultiplier);
	baddie.setStrength(statMultiplier);
	baddie.setHealth(healthMultiplier);
	cout<<endl<<player.getName()<<" encounters a "<<baddie.getName()<<" while traveling through the "<<terrain[area]<<"!";
	while(player.isDead() == false && baddie.isDead() == false)
	{
		cout<<"\nTo attack, press 1.";
		cout<<"\nTo defend, press 2.";
		cout<<"\nTo flee, press 3.";
		cout<<"\nTo drink a potion, press 4.";
		cout<<"\nTo print character and monster stats, press 5."<<endl;
		cin>>choice;
		int playerHit, baddieHit, playerDamage, baddieDamage;
		switch(choice)
		{
		case 1:
			playerHit = (rand() + time(0)) % 101;
			if(playerHit > 40)
			{
				cout<<"\nSuccessful hit!\n";
				baddieDamage = player.damageRoll();
				baddie.takeDamage(baddieDamage);
				if(baddie.isDead() == true)
				{
					cout<<endl<<baddie.getName()<<" has been slain.\n";
					player.addExperience(20);
					player.addMoney(goldMultiplier);
					if(player.isLevelUp() == true)
						player.levelUp();
					break;
				}
			}
			else
			{
				cout<<"\nYou missed!\n";
				baddieHit = (rand() + time(0)) % 101;
				if(baddieHit > 50)
				{
					cout<<endl<<baddie.getName()<<" has damaged you!\n";
					playerDamage = baddie.damageRoll();
					player.takeDamage(playerDamage);
					if(player.isDead() == true)
					{
						cout<<endl<<player.getName()<<" has been slain.\n";
						break;
					}
				}
				else
					cout<<endl<<baddie.getName()<<" missed!\n";
			}
			break;
		case 2:
			baddieHit = (rand() + time(0)) % 101;
			if(baddieHit > 75)
			{
				cout<<endl<<baddie.getName()<<" got a lucky hit.\n";
				playerDamage = baddie.damageRoll();
				player.takeDamage(playerDamage);
				if(player.isDead() == true)
				{
					cout<<endl<<player.getName()<<" has been slain.\n";
					break;
				}
			}
			else
			{
				cout<<endl<<baddie.getName()<<" missed; you recover minimal health.\n";
				if(player.getHealth() > 95)
					player.replenishHealth();
				else
					player.addHealth(5);
			}
			break;
		case 3:
			baddieHit = (rand() + time(0)) % 101;
			if(baddieHit > 25)
			{
				cout<<endl<<baddie.getName()<<" strikes as you flee.\n";
				playerDamage = baddie.damageRoll();
				player.takeDamage(playerDamage);
				if(player.isDead() == true)
				{
					cout<<endl<<player.getName()<<" has been slain.\n";
					break;
				}
			}
			break;
		case 4:
			if(player.getHealingPotions() <= 0)
				cout<<endl<<player.getName()<<" is out of potions!\n";
			else
			{
				player.takePotion();
				player.replenishHealth();
				cout<<endl<<player.getName()<<"'s health is now full.\n";
			}
			break;
		case 5:
			cout<<player<<endl;
			cout<<baddie<<endl;
			break;
		}
	}
}
Topic archived. No new replies allowed.