Inheritance Game Class

So I need to make an inheritance between the USER class and the Player class; I have more classes to add but I believe that if i'm able of at least understanding this one first, i'll be able to do the others. I have an idea of what I have to do, but the problem is I don't exactly now of what i'm supposed to inherit from. So the Player class is supposed to inherit from the user class. Now as I stated before, i don't really know exactly what is supposed to be inherit. If you have the time to read all this and suggest something that could help it would be appreciated. Thanks.

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
252
253
254
255
256
257
258
259
260
//Base/parent class
#ifndef USER_H
#define USER_H
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class USER {
public:
	USER(); //default constructor
	USER(string anewuser, string anewdrink, string anewcolor); //overload constructor
	~USER(); //Destructor
	void setuser(string anewuser);//mutators
	void setdrink(string anewdrink);
	void setcolor(string anewcolor);
	string getuser()const; //accessors
	string getdrink()const;
	string getcolor()const;

private:
	string newuser;
	string newdrink;
	string newcolor;
};
#endif

//Base/Parent cpp
#include"USER.h"
USER::USER() {
	setuser("");
	setdrink("");
	setcolor("");
}

USER::USER(string anewuser, string anewdrink, string anewcolor) {
	setuser(anewuser);
	setdrink(anewdrink);
	setcolor(anewcolor);
}

USER::~USER() {

}

void USER::setuser(string anewuser) {
	newuser = anewuser;
}

void USER::setdrink(string anewdrink) {
	newdrink = anewdrink;
}

void USER::setcolor(string anewcolor) {
	newcolor = anewcolor;
}

string USER::getuser()const {
	return newuser;
}

string USER::getdrink()const {
	return newdrink;
}

string USER::getcolor()const {
	return newcolor;
}

//Derived/child class
#ifndef PLAYER_H
#define PLAYER_H
#include<iostream>
#include"USER.h"
#include"Weapon.h"
using namespace std;
class Player : public USER {
public:

	/*Weapon weaponX;*/ //don't know exactly how to use it

	Player();
	Player(int newPlaHealth, int newPlaDistance, int newPlaSkill);
	~Player();
	void setplahealth(int newPlaHealth);
	void setpladistance(int newPlaDistance);
	void setplaskill(int newPlaSkill);
	int getplahealth()const;
	int getpladistance()const;
	int getplaskill()const;


private:
	int plaHealth;
	int plaDistance;
	int plaSkill;

};
#endif

//Derived/child cpp
#include"Player.h"
Player::Player() {
	setplahealth(0);
	setpladistance(0);
	setplaskill(0);
}

Player::Player(int newPlaHealth, int newPlaDistance, int newPlaSkill) {
	setplahealth(newPlaHealth);
	setpladistance(newPlaDistance);
	setplaskill(newPlaSkill);
}

Player::~Player() {

}

void Player::setplahealth(int newPlaHealth) {
	plaHealth = newPlaHealth;
}

void Player::setpladistance(int newPlaDistance) {
	plaDistance = newPlaDistance;
}

void Player::setplaskill(int newPlaSkill) {
	plaSkill = newPlaSkill;
}

int Player::getplahealth()const {
	return plaHealth;
}

int Player::getpladistance()const {
	return plaDistance;
}

int Player::getplaskill()const {
	return plaSkill;
}

//MAIN
#include<iostream>
#include<string>
#include<vector>
#include"USER.h"
#include"Player.h"
#include"Zombie.h"
#include"Weapon.h"
using namespace std;

void enterNewInfo(vector<USER>&);
void showInfo(const vector<USER>&);
void display();

int main() {
	int option;

	vector<USER> myVector;

	do {cout << "\t\t\t\t\t ______________________\n"
			<< "\t\t\t\t\t|:::::GAME CONSOLE:::::|"
			<< "\n\t\t\t\t\t|:::1) Create a User:::|"
			<< "\n\t\t\t\t\t|:::2) View All Users::|"
			<< "\n\t\t\t\t\t|:::3) Play a Game:::::|"
			<< "\n\t\t\t\t\t|:::4) Exit Game:::::::|"
			<< "\n\t\t\t\t\t|::::::::::::::::::::::|"
			<< "\n\t\t\t\t\t|::Select an option::: ";
		cin >> option;
		while (option > 4 || option < 1) {
			cout << "\n\t\t\t\t\tInvalid Option!"
				<< "\n\t\t\t\t\tPlease select 1-4"
				<< "\n\n\t\t\t\t\tSelect an option: ";
			cin >> option;
		}
		switch (option) {

		case 1:
			system("cls");
			enterNewInfo(myVector);
			system("pause");
			system("cls");
			break;
		case 2:system("cls");
			showInfo(myVector);
			system("pause");
			system("cls");
			break;
		case 3:
			system("cls");
			display();
			system("pause");
			system("cls");
			break;
		case 4:
			break;
		}
	} while (option != 4);
}

//USER CLASS: FILL USERNAME INFO
void enterNewInfo(vector<USER>& newMyVector) {
	string newuser;
	string newdrink;
	string newcolor;
	cout << "\t\t\t\t\t ________________________\n"
		<< "\t\t\t\t\t|************************|\n"
		<< "\t\t\t\t\t|***CHARACTER CREATION***|\n"
		<< "\t\t\t\t\t|************************|\n"
		<< "\t\t\t\t\t|------------------------|" << endl;
	cout << "\n\n\t\t\t\t\tHow many characters do you "
		<< "\n\t\t\t\t\twant?: ";
	int charSize;
	cin >> charSize;
	for (int x = 0; x < charSize; ++x) {
		cout << "\n\t\t\t\t\tEnter Username: ";
		cin >> newuser;
		cout << "\t\t\t\t\tEnter Fav. Drink: ";
		cin >> newdrink;
		cout << "\t\t\t\t\tEnter Fav. Color: ";
		cin >> newcolor;
		cout << "\n\t\t\t\t\t|***CHARACTER " << x << " CREATED***|" << endl;

		USER something(newuser, newdrink, newcolor);
		newMyVector.push_back(something);
		cout << endl;
	}
	cout << endl;
}

//USER CLASS: SHOW USERNAME INFO
void showInfo(const vector<USER>& newMyVector) {
	unsigned int size = newMyVector.size();
	cout << "\t\t\t\t\t _____________________________" << endl;
	cout << "\t\t\t\t\t|*****************************|" << endl;
	cout << "\t\t\t\t\t|******USERS INFORMATION******|" << endl;
	cout << "\t\t\t\t\t|*****************************|" << endl;
	cout << "\t\t\t\t\t|-----------------------------|" << endl;
	for (unsigned int x = 0; x < size; ++x) {
		cout << "\t\t\t\t\t _____________________________" << endl;
		cout << "\t\t\t\t\t|******USERNAME "<<x<<"*************|" << endl;
		cout << "\t\t\t\t\t       "<<newMyVector[x].getuser() << endl;
		cout << "\t\t\t\t\t|******FAV.DRINK " << x <<"************|"<< endl;
		cout << "\t\t\t\t\t       "<<newMyVector[x].getdrink() << endl;
		cout << "\t\t\t\t\t|******FAV.COLOR " <<x<<"************|"<< endl;
		cout << "\t\t\t\t\t       "<<newMyVector[x].getcolor() << endl;
		cout << "\t\t\t\t\t|_____________________________|" << endl;
		cout << endl;
	}
}

//USER CLASS: PLAY GAME OPTION
void display() {
	cout << "\t\t\t\t\t __________________________________ \n"
		<< "\t\t\t\t\t|**********************************|\n"
		<< "\t\t\t\t\t|******OPTION BEING DEVELOPED******|\n"
		<< "\t\t\t\t\t|**********************************|\n"
		<< "\t\t\t\t\t|----------------------------------|" << endl;
	
}
Last edited on
Since a player has all the characteristics of a user, plus a few others, it makes sense to derive Player from User as you've done it.

When figuring out class structure, it's better to start by thinking of the characteristics of each class and then decided which (if any) should inherit from which. In other words, don't start with the inheritance structure and try to fit your data to match the model. Start with the data and then choose a model that fits it.
I think I get what you're, saying, let me see if I can do this. Thank You for the idea.
Topic archived. No new replies allowed.