When adding a new student to the course, if the array capacity is exceeded, incr
ID: 3841389 • Letter: W
Question
When adding a new student to the course, if the array capacity is exceeded, increase the array size by creating a new larger array and copying the contents of the current array to it.
Implement the dropStudent function.
Add a new function named clear() that removes all students from the course.
Implement the destructor and copy constructor to perform a deep copy in the class.
Write a test program that creates a course, adds three students, removes one, and displays the students in the course.
#1 nclude O Stream 2 #include "Course.h" 3 using namespace std; 5 Course: Course Cconst string& courseName, int capacity) 6 7 numbe rofStudents 0 8 this courseName course Name 9 this capacity capacity 10 students new string capacity] 11 12 13 Course: Course C) 14 15 delete D] students 16 17 18 string Course: agetCounseNameo const 19 200 return course Name 21 22 23 void Course addstudent const string& name) 24 25 students Inumber0fStudents] name 26 number 0fStudents 27 28 29 void Course dropStudentCconst string& name) 30 1 31 Left as an exercise 32 33 34 string* Course: getStudents O const 35 1 36 return students 38 39 int Course getNumberofStudentsC) const 40 41 return numberofStudents 42Explanation / Answer
Hi Friend, since you have not posted Course.h file, so I can not test program.
But I have implemented all your required functions.
void Course:: addStudent(const string& name) {
if(numberOfStudents == capacity){
capacity = 2*capacity;
string *newArr = new string[capacity];
for(int i=0; i<numberOfStudents; i++)
newArr[i] = students[i];
newArr[numberOfStudents] = name;
numberOfStudents++;
students = newArr;
}else{
students[numberOfStudents] = name;
numberOfStudents++;
}
}
void Course::dropStudent(const string& name){
int pos = -1;
for(int i=0; i<numberOfStudents; i++){
if(students[i] == name){
pos = i;
break;
}
}
if(pos != -1){
for(int i=pos; i<numberOfStudents-1; i++)
students[i] = students[i+1];
numberOfStudents--;
}
}
void Course::clear(){
numberOfStudents = 0;
}
// copy constructor
Course::Course(const Course &other){
numberOfStudents = other.getNumberOfStudents();
courseName =other.getCourseName();
string *arr = other.getStudent();
delete [] students;
students = new string[numberOfStudents];
for(int i=0; i<numberOfStudents; i++)
students[i] = arr[i];
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.