C++ Case Study: The Course Class

This section designs a class for modeling courses.

Suppose you need to process course information. Each course students who take the course. You should be able to add/drop You can use a class to model the courses, as shown in Figure 11.5.

A Course object can be created using the constructor Course(string courseName, int capacity) by passing a course name and the maximum number of students allowed. You can add a student to the course using the addStudent(string name) function, drop a student from the course using the dropStudent(string name) function, and return all the students for the course using the getStudents() function.

Suppose the class is defined as shown in Listing 11.14. Listing 11.15 gives a test class that creates two courses and adds students to them.

Listing 11.14 Course.h

1 #ifndef COURSE_H
2
#define COURSE_H
3
#include <string>
4
using namespace std;
5
6
class Course
7 {
8
public:
9     Course(
const string& courseName, int capacity);
10    ~Course();
11    string getCourseName()
const;
12   
void addStudent(const string& name);
13   
void dropStudent(const string& name);
14    string* getStudents()
const;
15   
int getNumberOfStudents() const;
16
17
private:
18    string courseName;
19    string* students;
20   
int numberOfStudents;
21   
int capacity;
22 };
23
24
#endif

Listing 11.15 TestCourse.cpp

1 #include <iostream>
2
#include “Course.h”
3 using namespace std;
4
5 Course::Course(
const string& courseName, int capacity)
6 {
7    numberOfStudents =
0;
8   
this->courseName = courseName;
9   
this->capacity = capacity;
10   students =
new string[capacity];
11 }
12
13 Course::~Course()
14 {
15   
delete [] students;
16 }
17
18 string Course::getCourseName()
const
19 {
20   
return courseName;
21 }
22
23
void Course::addStudent(const string& name)
24 {
25    students[numberOfStudents] = name;
26    numberOfStudents++;
27 }
28
29
void Course::dropStudent(const string& name)
30 {
31   
// Left as an exercise
32 }
33
34 string* Course::getStudents()
const
35 {
36   
return students;
37 }
38
39
int Course::getNumberOfStudents() const
40 {
41   
return numberOfStudents;
42 }

The Course constructor initializes numberOfStudents to 0 (line 7), sets a new course name (line 8), sets a capacity (line 9), and creates a dynamic array (line 10).

The Course class uses an array to store the students for the course. The array is created when a Course object is constructed. The array size is the maximum number of students allowed for the course. So, the array is created using new string[capacity].

When a Course object is destroyed, the destructor is invoked to properly destroy the array (line 15).

The addStudent function adds a student to the array (line 23). This function does not check whether the number of students in the class exceeds the maximum capacity. In Chapter 16, you will learn how to revise this function to make your program more robust by throwing an exception if the number of students in the class exceeds the maximum capacity.

The getStudents function (lines 34-37) returns the address of the array for storing the students.

The dropStudent function (lines 29-32) removes a student from the array. The imple­mentation of this function is left as an exercise.

The user can create a Course and manipulate it through the public functions addStudent, dropStudent, getNumberOfStudents, and getStudents. However, the user doesn’t need to know how these functions are implemented. The Course class encapsulates the inter­nal implementation. This example uses an array to store students. You may use a different data structure to store students. The program that uses Course does not need to change as long as the contract of the public functions remains unchanged.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *