This next version of the GradeBook
class asks the user to enter a set of letter grades, then displays a summary of the number of students who received each grade. The class uses a switch
to determine whether each grade entered is an A, B, C, D or F and to increment the appropriate grade counter. Class GradeBook
is defined in Fig. 5.9, and its member-function definitions appear in Fig. 5.10. Figure 5.11 shows sample inputs and outputs of the main
program that uses class GradeBook
to process a set of grades.
1 // Fig. 5.9: GradeBook.h
2 // GradeBook class definition that counts letter grades.
3 // Member functions are defined in GradeBook.cpp
4 #include <string> // program uses C++ standard string class
5
6 // GradeBook class definition
7 class GradeBook
8 {
9 public:
10 explicit GradeBook( std::string ); // initialize course name
11 void setCourseName( std::string ); // set the course name
12 std::string getCourseName() const; // retrieve the course name
13 void displayMessage() const; // display a welcome message
14 void inputGrades(); // input arbitrary number of grades from user
15 void displayGradeReport() const; // display report based on user input
16 private:
17 std::string courseName; // course name for this GradeBook
18 unsigned int aCount; // count of A grades
19 unsigned int bCount; // count of B grades
20 unsigned int cCount; // count of C grades
21 unsigned int dCount; // count of D grades
22 unsigned int fCount; // count of F grades
23 }; // end class GradeBook
1 // Fig. 5.10: GradeBook.cpp
2 // Member-function definitions for class GradeBook that
3 // uses a switch statement to count A, B, C, D and F grades.
4 #include <iostream>
5 #include "GradeBook.h" // include definition of class GradeBook
6 using namespace std;
7
8 // constructor initializes courseName with string supplied as argument;
9 // initializes counter data members to 0
10 GradeBook::GradeBook( string name )
11 : aCount( 0 ), // initialize count of A grades to 0
12 bCount( 0 ), // initialize count of B grades to 0
13 cCount( 0 ), // initialize count of C grades to 0
14 dCount( 0 ), // initialize count of D grades to 0
15 fCount( 0 ) // initialize count of F grades to 0
16 {
17 setCourseName( name );
18 } // end GradeBook constructor
19
20 // function to set the course name; limits name to 25 or fewer characters
21 void GradeBook::setCourseName( string name )
22 {
23 if ( name.size() <= 25 ) // if name has 25 or fewer characters
24 courseName = name; // store the course name in the object
25 else // if name is longer than 25 characters
26 { // set courseName to first 25 characters of parameter name
27 courseName = name.substr( 0, 25 ); // select first 25 characters
28 cerr << "Name "" << name << "" exceeds maximum length (25).
"
29 << "Limiting courseName to first 25 characters.
" << endl;
30 } // end if...else
31 } // end function setCourseName
32
33 // function to retrieve the course name
34 string GradeBook::getCourseName() const
35 {
36 return courseName;
37 } // end function getCourseName
38
39 // display a welcome message to the GradeBook user
40 void GradeBook::displayMessage() const
41 {
42 // this statement calls getCourseName to get the
43 // name of the course this GradeBook represents
44 cout << "Welcome to the grade book for
" << getCourseName() << "!
"
45 << endl;
46 } // end function displayMessage
47
48 // input arbitrary number of grades from user; update grade counter
49 void GradeBook::inputGrades()
50 {
51 int grade; // grade entered by user
52
53 cout << "Enter the letter grades." << endl
54 << "Enter the EOF character to end input." << endl;
55
56 // loop until user types end-of-file key sequence
57 while ( ( grade = cin.get() ) != EOF
)
58 {
59 // determine which grade was entered
60 switch ( grade ) // switch statement nested in while
61 {
62 case 'A': // grade was uppercase A
63 case 'a': // or lowercase a
64 ++aCount; // increment aCount
65 break; // necessary to exit switch
66
67 case 'B': // grade was uppercase B
68 case 'b': // or lowercase b
69 ++bCount; // increment bCount
70 break; // exit switch
71
72 case 'C': // grade was uppercase C
73 case 'c': // or lowercase c
74 ++cCount; // increment cCount
75 break; // exit switch
76
77 case 'D': // grade was uppercase D
78 case 'd': // or lowercase d
79 ++dCount; // increment dCount
80 break; // exit switch
81
82 case 'F': // grade was uppercase F
83 case 'f': // or lowercase f
84 ++fCount; // increment fCount
85 break; // exit switch
86
87 case '
': // ignore newlines,
88 case ' ': // tabs,
89 case ' ': // and spaces in input
90 break; // exit switch
91
92 default: // catch all other characters
93 cout << "Incorrect letter grade entered."
94 << " Enter a new grade." << endl;
95 break; // optional; will exit switch anyway
96 } // end switch
97 } // end while
98 } // end function inputGrades
99
100 // display a report based on the grades entered by user
101 void GradeBook::displayGradeReport() const
102 {
103 // output summary of results
104 cout << "
Number of students who received each letter grade:"
105 << "
A: " << aCount // display number of A grades
106 << "
B: " << bCount // display number of B grades
107 << "
C: " << cCount // display number of C grades
108 << "
D: " << dCount // display number of D grades
109 << "
F: " << fCount // display number of F grades
110 << endl;
111 } // end function displayGradeReport
1 // Fig. 5.11: fig05_11.cpp
2 // Creating a GradeBook object and calling its member functions.
3 #include "GradeBook.h" // include definition of class GradeBook
4
5 int main()
6 {
7 // create GradeBook object
8 GradeBook myGradeBook( "CS101 C++ Programming" );
9
10 myGradeBook.displayMessage(); // display welcome message
11 myGradeBook.inputGrades(); // read grades from user
12 myGradeBook.displayGradeReport(); // display report based on grades
13 } // end main
Welcome to the grade book for
CS101 C++ Programming!
Enter the letter grades.
Enter the EOF character to end input.
a
B
c
C
A
d
f
C
E
Incorrect letter grade entered. Enter a new grade.
D
A
b
^Z
Number of students who received each letter grade:
A: 3
B: 2
C: 3
D: 2
F: 1
Like earlier versions of the class definition, the GradeBook
class definition (Fig. 5.9) contains function prototypes for member functions setCourseName
(line 11), getCourseName
(line 12) and displayMessage
(line 13), as well as the class’s constructor (line 10). The class definition also declares private
data member courseName
(line 17).