Question: The programs source code (Course . h, Course. cpp, TestCourse . cpp) has been partly completed there are missing lines in the files (marked as
The programs source code (Course . h, Course. cpp, TestCourse . cpp) has been partly completed there are missing lines in the files (marked as comments). Fill in the missing blocks of code to complete the program and test it.
#ifndef COURSE_H #define COURSE_H #include
class Course { public: Course(const string& courseName, int capacity); // a missing line here: destructor string getCourseName() const; void addStudent(const string& name); void dropStudent(const string& name); string* getStudents() const; int getNumberOfStudents() const;
// a missing line: make all data fields accessible only inside the class string courseName; string* students; int numberOfStudents; int capacity; };
#endif
#include
Course::Course(const string& courseName, int capacity) { numberOfStudents = 0; this->courseName = courseName; //a missing line: initiate capacity //a missing line: initiate students using new }
Course::~Course() { //a missing line: release dynamic variable students }
string Course::getCourseName() const { return courseName; }
void Course::addStudent(const string& name) { //missing line(s): left as an exercise }
void Course::dropStudent(const string& name) { //missing line(s): left as an exercise }
string* Course::getStudents() const { return students; }
int Course::getNumberOfStudents() const { return numberOfStudents; }
#include
int main(int argc, char *argv[]) { Course course1("Data Structures", 10); Course course2("Database Systems", 15);
cout << "Number of students in " << course1.getCourseName() << ": " << course1.getNumberOfStudents() << " "; //a missing line: output the initial number of students in course 2
course1.addStudent("Peter Jones"); course1.addStudent("Brian Smith"); course1.addStudent("Anne Kennedy");
//a missing line: add student Peter Jones to course2 //a missing line: add student Steven Smith to course2
cout << "Number of students in " << course1.getCourseName() << ": " << course1.getNumberOfStudents() << " "; string* students = course1.getStudents(); for (int i = 0; i < course1.getNumberOfStudents() - 1; i++) cout << students[i] << ", "; cout << students[course1.getNumberOfStudents()-1] << endl;
cout << "Number of students in " << course2.getCourseName() << ": " << course2.getNumberOfStudents() << " "; //a missing line: assign couse2s students to students variable declared in line 25 for (int i = 0; i < course2.getNumberOfStudents() - 1; i++) //missing line(s), print all students name of course2 using pointer declared in line 25;
course1.dropStudent("Peter Jones"); cout << "Number of students in " << course1.getCourseName() << ": " << course1.getNumberOfStudents() << " "; students = course1.getStudents(); for (int i = 0; i < course1.getNumberOfStudents() - 1; i++) cout << students[i] << ", "; cout << students[course1.getNumberOfStudents()-1] << endl;
//a missing line: try drop the student "Anne Kennedy" from course2. Your code should tell //the user that this student is not enrolled in course2 (your code in Course.cpp should implement this)
//system("PAUSE"); return EXIT_SUCCESS; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
