how to inherit attributes from one class to another?

im having some trouble inheriting attributes from a class. my code:

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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <iostream>
#include <vector>
#include <cstdlib>
#include <set>
#include <algorithm>
using namespace std;

class student
{
private:
	string name, dummy, status;
	char gender;
	int ID;
public:
	student() : name("UNKNOWN"), ID(0), gender('-'), status("UNASSIGNED"){}
	student (string Name, int ID, char Gender)
	{
		this->name = Name;
		this->ID = ID;
		this->gender = Gender;
	}
	student(const student& s): name(s.name), ID(s.ID), gender(s.gender){}
	void setstudent();
	void setstatus(string status) {this->status = status;}
	void print(int i) const;
	int getid(){return ID;}
};

void student::setstudent()
{
	cout << "Enter student's ID: ";
	cin >> ID;
	cout << "Enter student's gender (M/F): ";
	cin >> gender;
	gender = toupper(gender);
	getline(cin, dummy);
	cout << "Enter student's name: ";
	getline(cin, name);
}

void student::print(int i) const
{
	cout << i+1 << ". ID: " << ID << " | NAME: " << name << 
	" l SEX: " << gender << " l ROOM: " << status << endl;
}

class room
{	
private:
	vector<student*> students;
	vector<student*>::iterator it;
public:
	void addstudent(student* s);
	void delstudent (int target);
	void printALL();;
	int studentsize(){return students.size();}
};

void room::addstudent(student* s)
{
	students.push_back(s);
}

void room::delstudent(int target)
{
	students.erase(students.begin()+target);
}

void room::printALL()
{	
	for(int i = 0; i < students.size(); i++)
		students[i]->print(i);
}

class MA:public student, public room
{
	vector<student*> MA1;
	vector<student*> MA2;
	vector<student*> MA3;
	vector<student*> MA4;
	vector<student*> MA5;
	vector<student*> MA6;
	vector<student*> MA7;
	vector<student*> MA8;	
	
public:
	void addMA1(int target);
	void addMA2(int target);
	void addMA3(int target);
	void addMA4(int target);
	void addMA5(int target);
	void addMA6(int target);
	void addMA7(int target);
	void addMA8(int target);
	void delMA1(int target);
	void printMA1();
	int roomMA1size(){return MA1.size();}
};

void MA::addMA1(int target)
{
	MA1.push_back(students[target]);
}

void MA::delMA1(int target)
{
	MA1[target]->setstatus("UNASSIGNED");
	students.push_back(MA1[target]);
	MA1.erase(MA1.begin()+target);
}

void MA::addMA2(int target)
{
	MA2.push_back(students[target]);
	students[target]->setstatus("MA1");
	students.erase(students.begin()+target);
}

void MA::addMA3(int target)
{
	MA3.push_back(students[target]);
	students[target]->setstatus("MA1");
	students.erase(students.begin()+target);
}

void MA::addMA4(int target)
{
	MA4.push_back(students[target]);
	students[target]->setstatus("MA1");
	students.erase(students.begin()+target);
}

void MA::addMA5(int target)
{
	MA5.push_back(students[target]);
	students[target]->setstatus("MA1");
	students.erase(students.begin()+target);
}

void MA::addMA6(int target)
{
	MA6.push_back(students[target]);
	students[target]->setstatus("MA1");
	students.erase(students.begin()+target);
}

void MA::addMA7(int target)
{
	MA7.push_back(students[target]);
	students[target]->setstatus("MA1");
	students.erase(students.begin()+target);
}

void MA::addMA8(int target)
{
	MA8.push_back(students[target]);
	students[target]->setstatus("MA1");
	students.erase(students.begin()+target);
}

void MA::printMA1()
{
	for(int i = 0; i < MA1.size(); i++)
		MA1[i]->print(i);
}


void mainmenu()
{
	cout << "*===========================================*" << endl <<
	"|[1] add students                           |" << endl <<
	"|[2] delete student                         |" << endl <<
	"|[3] assign rooms                           |" << endl <<
	"|[4] empty rooms                            |" << endl <<
	"|[5] List students                          |" << endl <<
	"|[6] Exit                                   |" << endl <<
	"*===========================================+" << endl << endl << endl;
}

void MAmenu()
{
    cout << "*===========================================*" << endl <<
	"|[1] MA1                                            |" << endl <<
	"|[2] MA2                                            |" << endl <<
	"|[3] MA3                                            |" << endl <<
	"|[4] MA4                                            |" << endl <<
	"|[5] MA5                                            |" << endl <<
	"|[6] MA6                                            |" << endl <<
	"|[7] MA7                                            |" << endl <<
	"|[8] MA8                                            |" << endl <<
	"|[9] back                                           |" << endl <<
	"*===========================================+" << endl << endl << endl;
}

	room r1;
	MA a;
	student* s1 = NULL;
	int target;
	int num;
	bool done = false;
	char choice, option;
	
int main()
{
	do
	{
		mainmenu();
		cout << "Select option (1-6): ";
		cin >> option;
		option = toupper(option);
		switch (option)
		{
			case '1' : 
			cout << "Add student? (Y/N):";
			cin >> choice;
	
			while (toupper(choice) == 'Y')
			{
				s1 = new student;		
				s1->setstudent();
				r1.addstudent(s1);		
				cout << "add more students?(Y/N): ";
				cin >> choice;
			}
			r1.printALL();
			break;
			
			case '2' : 
			cout << "Delete student? (Y/N):";
			cin >> choice;
			r1.printALL();
	
			while (toupper(choice) == 'Y')
			{
				cout << "select a student(1-" << r1.studentsize() << "): ";
				cin >> target;
				target--;
							
				r1.delstudent(target);
							
				cout << "Delete more students?(Y/N): ";
				cin >> choice;
				r1.printALL();
			};
			 break;
			 
			case '3' : ;
			cout << "Assign to room? (Y/N):";
			cin >> choice;
			r1.printALL();
			num = 2;
						
			while (toupper(choice) == 'Y' && num > a.roomMA1size())
			{
				cout << "choose student(1-" << r1.studentsize() << "): ";
				cin >> target;
				target--;
						
				a.addMA1(target);
						
				cout << "Assign more?(Y/N): ";
				cin >> choice;
				r1.printALL();
				a.printMA1();
			};
				cout << "capacity reached\n\n";
				system("pause");
				break;
				
			case '4' : 
			cout << "Unassign room? (Y/N):";
			cin >> choice;
			a.printMA1();
						
			while (toupper(choice) == 'Y' && num > 0)
			{
				cout << "choose student: ";
				cin >> target;
				target--;
						
				a.delMA1(target);
						
				cout << "Assign more?(Y/N): ";
				cin >> choice;
				a.printMA1();
					
				num--;
			}
				if (num == 0)
				cout << "room empty\n\n";
				system("pause");	
				break;
			
			case '5' : 
			r1.printALL();
			break;		
		}
	}
	while(!done);

	return 0;
}



whenever a.addMA1() is ran, the program crashed.. im guessing it's because students[target] does not exist in class MA. so how do i make it so that it exist for all class?
You should make members you want accessible in derived classes protected instead of private.
Topic archived. No new replies allowed.