Our next example (Fig. 3.3) redefines class GradeBook
(lines 9–18) with a displayMessage
member function (lines 13–17) that displays the course name as part of the welcome message. The new version of displayMessage
requires a parameter (courseName
in line 13) that represents the course name to output.
1 // Fig. 3.3: fig03_03.cpp
2 // Define class GradeBook with a member function that takes a parameter,
3 // create a GradeBook object and call its displayMessage function.
4 #include <iostream>
5 #include <string> // program uses C++ standard string class
6 using namespace std;
7
8 // GradeBook class definition
9 class GradeBook
10 {
11 public:
12 // function that displays a welcome message to the GradeBook user
13 void displayMessage( string courseName ) const
14 {
15 cout << "Welcome to the grade book for
" << courseName << "!"
16 << endl;
17 } // end function displayMessage
18 }; // end class GradeBook
19
20 // function main begins program execution
21 int main()
22 {
23 string nameOfCourse; // string of characters to store the course name
24 GradeBook myGradeBook; // create a GradeBook object named myGradeBook
25
26 // prompt for and input course name
27 cout << "Please enter the course name:" << endl;
28 getline( cin, nameOfCourse ); // read a course name with blanks
29 cout << endl; // output a blank line
30
31 // call myGradeBook's displayMessage function
32 // and pass nameOfCourse as an argument
33 myGradeBook.displayMessage( nameOfCourse );
34 } // end main
Please enter the course name:
CS101 Introduction to C++ Programming
Welcome to the grade book for
CS101 Introduction to C++ Programming!
Before discussing the new features of class GradeBook
, let’s see how the new class is used in main
(lines 21–34). Line 23 creates a variable of type string called nameOfCourse
that will be used to store the course name entered by the user. A variable of type string
represents a string of characters such as "CS101 Introduction to C++ Programming"
. A string is actually an object of the C++ Standard Library class string
. This class is defined in header <string>, and the name string
, like cout
, belongs to namespace std
. To enable lines 13 and 23 to compile, line 5 includes the <string> header. The using
directive in line 6 allows us to simply write string
in line 23 rather than std::string
. For now, you can think of string
variables like variables of other types such as int
. You’ll learn additional string
capabilities in Section 3.8 and in Chapter 19.
Line 24 creates an object of class GradeBook
named myGradeBook
. Line 27 prompts the user to enter a course name. Line 28 reads the name from the user and assigns it to the nameOfCourse
variable, using the library function getline to perform the input. Before we explain this line of code, let’s explain why we cannot simply write
cin >> nameOfCourse;
to obtain the course name.
In our sample program execution, we use the course name “CS101 Introduction to C++ Programming
,” which contains multiple words separated by blanks. (Recall that we highlight user-entered data in bold.) When reading a string
with the stream extraction operator, cin
reads characters until the first white-space character is reached. Thus, only “CS101
” would be read by the preceding statement. The rest of the course name would have to be read by subsequent input operations.
In this example, we’d like the user to type the complete course name and press Enter to submit it to the program, and we’d like to store the entire course name in the string
variable nameOfCourse
. The function call getline( cin, nameOfCourse )
in line 28 reads characters (including the space characters that separate the words in the input) from the standard input stream object cin
(i.e., the keyboard) until the newline character is encountered, places the characters in the string
variable nameOfCourse
and discards the newline character. When you press Enter while entering data, a newline is inserted in the input stream. The <string>
header must be included in the program to use function getline
, which belongs to namespace std
.
Line 33 calls myGradeBook
’s displayMessage
member function. The nameOfCourse
variable in parentheses is the argument that’s passed to member function displayMessage
so that it can perform its task. The value of variable nameOfCourse
in main
is copied to member function displayMessage
’s parameter courseName
in line 13. When you execute this program, member function displayMessage
outputs as part of the welcome message the course name you type (in our sample execution, CS101 Introduction to C++ Programming)
.