Testing Class GradeBook

Figure 3.17 demonstrates the modified version of class GradeBook (Figs. 3.153.16) featuring validation. Line 12 creates a GradeBook object named gradeBook1. Recall that the GradeBook constructor calls setCourseName to initialize data member courseName. In previous versions of the class, the benefit of calling setCourseName in the constructor was not evident. Now, however, the constructor takes advantage of the validation provided by setCourseName. The constructor simply calls setCourseName, rather than duplicating its validation code. When line 12 of Fig. 3.17 passes an initial course name of "CS101 Introduction to Programming in C++" to the GradeBook constructor, the constructor passes this value to setCourseName, where the actual initialization occurs. Because this course name contains more than 25 characters, the body of the second if statement executes, causing courseName to be initialized to the truncated 25-character course name "CS101 Introduction to Pro" (the truncated part is highlighted in line 12). The output in Fig. 3.17 contains the warning message output by lines 26–27 of Fig. 3.16 in member function setCourseName. Line 13 creates another GradeBook object called gradeBook2—the valid course name passed to the constructor is exactly 25 characters.


 1   // Fig. 3.17: fig03_17.cpp
 2   // Create and manipulate a GradeBook object; illustrate validation.
 3   #include <iostream>
 4   #include "GradeBook.h" // include definition of class GradeBook
 5   using namespace std;
 6
 7   // function main begins program execution
 8   int main()
 9   {
10      // create two GradeBook objects;
11      // initial course name of gradeBook1 is too long
12      GradeBook gradeBook1( "CS101 Introduction to Pro gramming in C++" );
13      GradeBook gradeBook2( "CS102 C++ Data Structures" );
14
15      // display each GradeBook's courseName
16      cout << "gradeBook1's initial course name is: "
17         << gradeBook1.getCourseName()
18         << " gradeBook2's initial course name is: "
19         << gradeBook2.getCourseName() << endl;
20
21      // modify gradeBook1's courseName (with a valid-length string)
22      gradeBook1.setCourseName( "CS101 C++ Programming" );
23
24      // display each GradeBook's courseName
25      cout << " gradeBook1's course name is: "
26         << gradeBook1.getCourseName()
27         << " gradeBook2's course name is: "
28         << gradeBook2.getCourseName() << endl;
29   } // end main


Name "CS101 Introduction to Programming in C++" exceeds maximum length (25).
Limiting courseName to first 25 characters.

gradeBook1's initial course name is: CS101 Introduction to Pro
gradeBook2's initial course name is: CS102 C++ Data Structures

gradeBook1's course name is: CS101 C++ Programming
gradeBook2's course name is: CS102 C++ Data Structures


Fig. 3.17. Creating and manipulating a GradeBook object in which the course name is limited to 25 characters in length.

Lines 16–19 of Fig. 3.17 display the truncated course name for gradeBook1 (we highlight this in the program output) and the course name for gradeBook2. Line 22 calls gradeBook1’s setCourseName member function directly, to change the course name in the GradeBook object to a shorter name that does not need to be truncated. Then, lines 25–28 output the course names for the GradeBook objects again.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.132.214