-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourse.cpp
More file actions
219 lines (188 loc) · 5.66 KB
/
Copy pathcourse.cpp
File metadata and controls
219 lines (188 loc) · 5.66 KB
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
#include "program3.h"
//Default Constructor
Course::Course()
{
szCourseID = "XXX0000";
szCourseName = "Test Course";
iMaxStudents = 10;
studentEnrolled = new Student[iMaxStudents];
iNumEnrolled = 0;
}
//Parameterized Constructor
Course::Course(string id, string name, int capacity)
{
szCourseID = id;
szCourseName = name;
iMaxStudents = capacity;
studentEnrolled = new Student[iMaxStudents];
iNumEnrolled = 0;
}
//Copy constructor
/*********************************************************************
Course(const Course& other);
Purpose:
Create new object by copying another object of same class.
Parameters:
I const Course& other -- the existing object
Return Value:
-
Notes:
Creates new memory and copies data
*********************************************************************/
Course::Course(const Course& other)
{
szCourseID = other.szCourseID;
szCourseName = other.szCourseName;
iMaxStudents = other.iMaxStudents;
iNumEnrolled = other.iNumEnrolled;
//create dynamic array
studentEnrolled = new Student[iMaxStudents];
//copy info over
for(int i=0; i<iNumEnrolled; i++)
{
studentEnrolled[i] = other.studentEnrolled[i];
}
}
//Assignment Operator
/*********************************************************************
Course& operator=(const Course& other);
Purpose:
Allocates new memory for the new object and deallocate the old memory
Parameters:
I const Course& other -- the existing object
Return Value:
Returns the new object
Notes:
Deletes old data and copy new data
*********************************************************************/
Course& Course::operator=(const Course& other)
{
//avoid self-assignment
if(this == &other)
{
return *this;
}
//free old memory
delete[] studentEnrolled;
//populate new info
szCourseID = other.szCourseID;
szCourseName = other.szCourseName;
iMaxStudents = other.iMaxStudents;
iNumEnrolled = other.iNumEnrolled;
studentEnrolled = new Student[iMaxStudents];
for(int i=0; i<iNumEnrolled; i++)
{
studentEnrolled[i] = other.studentEnrolled[i];
}
//return new obj
return *this;
}
//Destructor
/*********************************************************************
~Course()
Purpose:
Deletes the dynamically allocated array.
Parameters:
-
Return Value:
-
Notes:
-
*********************************************************************/
Course::~Course()
{
delete[] studentEnrolled;
}
/*********************************************************************
string getCourseInfo();
Purpose:
Return a string containing course ID and name, seperated by a colon with a space(: ) and capacity and enrollement.
Parameters:
-
Return Value:
Returns the formatted string
Notes:
String concatenation has been used for the return.
*********************************************************************/
string Course::getCourseInfo()
{
return szCourseID + ": " + szCourseName +
"\nCapacity: " + to_string(iMaxStudents) +
"\nEnrollment: " + to_string(iNumEnrolled) + "\n";
}
/*********************************************************************
bool enrollStudent(const Student& newStudent);
Purpose:
Enroll the new student to the course and increase the number of students enrolled.
Parameters:
I Student& newStudent -- has the information about the new student for enrollment.
Return Value:
Returns true if the student is added is successfully or else returns false.
Notes:
-
*********************************************************************/
bool Course::enrollStudent(const Student& newStudent)
{
if(iNumEnrolled >= iMaxStudents)
{
cout << "Max capacity reached. Please increase the course capacity before adding more students." << endl;
return false;
}
else
{
studentEnrolled[iNumEnrolled] = newStudent;
iNumEnrolled++;
return true;
}
}
/*********************************************************************
void displayStudents();
Purpose:
Prints the course infromation and loops through the students to print their information too.
Parameters:
-
Return Value:
N/A.
Notes:
Use the getCourseInfo() and getStudentInfor() function.
*********************************************************************/
void Course::displayStudents()
{
string szCourseInfo = getCourseInfo();
cout << "Students enrolled in " << endl;
cout << szCourseInfo << endl;
for(int i=0; i<iNumEnrolled; i++)
{
string szStudentInfo = studentEnrolled[i].getStudentInfo();
cout << "-" << endl;
cout << szStudentInfo << endl;
}
}
/*********************************************************************
void increaseMaxEnrollment(int additionalCapacity);
Purpose:
Increases the size of the array and delete the old array and put in the new array in the same pointer.
Parameters:
I int additionalCapacity -- take in the additioanl capacity for the array
Return Value:
-
Notes:
Delete the old dynamically allocated array.
*********************************************************************/
void Course::increaseMaxEnrollment(int additionalCapacity)
{
if (additionalCapacity <= 0) //if additional capacity is wrong
{
return;
}
int iNewCapacity = iMaxStudents + additionalCapacity;
Student* newArray = new Student[iNewCapacity];
for (int i = 0; i < iNumEnrolled; ++i)
{
newArray[i] = studentEnrolled[i];
}
delete[] studentEnrolled; //delete the old dynamically allocated array
//copy the new array to the same array pointer
studentEnrolled = newArray;
iMaxStudents = iNewCapacity;
}