Class GradeBook
(Figs. 4.6–4.7) contains a constructor (declared in line 10 of Fig. 4.6 and defined in lines 9–12 of Fig. 4.7) that assigns a value to the class’s data member courseName
(declared in line 16 of Fig. 4.6). Lines 16–26, 29–32 and 35–39 of Fig. 4.7 define member functions setCourseName
, getCourseName
and displayMessage
, respectively. Lines 42–64 define member function determineClassAverage
.
Because the gradeCounter
variable (Fig. 4.7, line 46) is used to count from 1 to 10 in this program (all positive values), we declared the variable as an unsigned int, which can store only non-negative values (that is, 0 and higher). Local variables total
(Fig. 4.7, line 45), grade
(line 52) and average
(line 59) to be of type int
. Variable grade
stores the user input. Notice that the preceding declarations appear in the body of member function determineClassAverage
. Also, variable grade
is declared in the while
statement’s body because it’s used only in the loop—in general, variables should be declared just before they’re used. We initialize grade
to 0
(line 52) as a good practice, even though a new value is immediately input for grade in line 53.
In this chapter’s versions of class GradeBook
, we simply read and process a set of grades. The averaging calculation is performed in member function determineClassAverage
using local variables—we do not preserve any information about student grades in the class’s data members. In Chapter 7, we modify class GradeBook
to maintain the grades in memory using a data member that refers to an array. This allows a GradeBook
object to perform various calculations on a set of grades without requiring the user to enter the grades multiple times.
Lines 45–46 initialize total
to 0
and gradeCounter
to 1
before they’re used in calculations. Line 49 indicates that the while
statement should continue looping (also called iterating) as long as gradeCounter
’s value is less than or equal to 10
. While this condition remains true, the while
statement repeatedly executes the statements between the braces that delimit its body (lines 49–56).
Line 51 displays the prompt "Enter grade: "
. Line 53 reads the grade entered by the user and assigns it to variable grade
. Line 54 adds the new grade entered by the user to the total
and assigns the result to total
, which replaces its previous value.
Line 55 adds 1
to gradeCounter
to indicate that the program has processed the current grade and is ready to input the next grade from the user. Incrementing gradeCounter
eventually causes gradeCounter
to exceed 10
. At that point the while
loop terminates because its condition (line 49) becomes false.
When the loop terminates, line 59 performs the averaging calculation and assigns its result to the variable average
. Line 62 displays the text "Total of all 10 grades is "
followed by variable total
’s value. Line 63 then displays the text "Class average is "
followed by variable average
’s value. Member function determineClassAverage
then returns control to the calling function (i.e., main
in Fig. 4.8).
3.147.44.182