In our next example, class GradeBook
(Fig. 3.5) maintains the course name as a data member so that it can be used or modified throughout a program’s execution. The class contains member functions setCourseName
, getCourseName
and displayMessage
. Member function setCourseName
stores a course name in a GradeBook
data member. Member function getCourseName
obtains the course name from that data member. Member function displayMessage
—which now specifies no parameters—still displays a welcome message that includes the course name. However, as you’ll see, the function now obtains the course name by calling another function in the same class—getCourseName
.
1 // Fig. 3.5: fig03_05.cpp
2 // Define class GradeBook that contains a courseName data member
3 // and member functions to set and get its value;
4 // Create and manipulate a GradeBook object with these functions.
5 #include <iostream>
6 #include <string> // program uses C++ standard string class
7 using namespace std;
8
9 // GradeBook class definition
10 class GradeBook
11 {
12 public:
13 // function that sets the course name
14 void setCourseName( string name )
15 {
16 courseName = name; // store the course name in the object
17 } // end function setCourseName
18
19 // function that gets the course name
20 string getCourseName() const
21 {
22 return courseName; // return the object's courseName
23 } // end function getCourseName
24
25 // function that displays a welcome message
26 void displayMessage() const
27 {
28 // this statement calls getCourseName to get the
29 // name of the course this GradeBook represents
30 cout << "Welcome to the grade book for
" << getCourseName() << "!"
31 << endl;
32 } // end function displayMessage
33 private:
34 string courseName; // course name for this GradeBook
35 }; // end class GradeBook
36
37 // function main begins program execution
38 int main()
39 {
40 string nameOfCourse; // string of characters to store the course name
41 GradeBook myGradeBook; // create a GradeBook object named myGradeBook
42
43 // display initial value of courseName
44 cout << "Initial course name is: " << myGradeBook.getCourseName()
45 << endl;
46
47 // prompt for, input and set course name
48 cout << "
Please enter the course name:" << endl;
49 getline( cin, nameOfCourse ); // read a course name with blanks
50 myGradeBook.setCourseName( nameOfCourse ); // set the course name
51
52 cout << endl; // outputs a blank line
53 myGradeBook.displayMessage(); // display message with new course name
54 } // end main
Initial course name is:
Please enter the course name:
CS101 Introduction to C++ Programming
Welcome to the grade book for
CS101 Introduction to C++ Programming!
A typical instructor teaches several courses, each with its own course name. Line 34 declares that courseName
is a variable of type string
. Because the variable is declared in the class definition (lines 10–35) but outside the bodies of the class’s member-function definitions (lines 14–17, 20–23 and 26–32), the variable is a data member. Every instance (i.e., object) of class GradeBook
contains each of the class’s data members—if there are two GradeBook
objects, each has its own courseName
(one per object), as you’ll see in the example of Fig. 3.7. A benefit of making courseName
a data member is that all the member functions of the class can manipulate any data members that appear in the class definition (in this case, courseName
).