Let’s consider functions with multiple parameters. Figures 6.2–6.4 modify class GradeBook
by including a user-defined function called maximum
that determines and returns the largest of three int
grades. When the application executes, the main
function (lines 5–13 of Fig. 6.4) creates one GradeBook
object (line 8) and calls its inputGrades
member function (line 11) to read three integer grades from the user. In class GradeBook
’s implementation file (Fig. 6.3), lines 52–53 of member function inputGrades
prompt the user to enter three integer values and read them from the user. Line 56 calls member function maximum
(defined in lines 60–73). Function maximum
determines the largest value, then the return
statement (line 72) returns that value to the point at which function inputGrades
invoked maximum
(line 56). Member function inputGrades
then stores maximum
’s return value in data member maximumGrade
. This value is then output by calling function displayGradeReport
(line 12 of Fig. 6.4). [Note: We named this function displayGradeReport
because subsequent versions of class GradeBook
will use this function to display a complete grade report, including the maximum and minimum grades.] In Chapter 7, we’ll enhance class GradeBook
to process sets of grades.
1 // Fig. 6.2: GradeBook.h
2 // Definition of class GradeBook that finds the maximum of three 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 ); // initializes 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 three grades from user
15 void displayGradeReport() const; // display report based on the grades
16 int maximum( int, int, int ) const; // determine max of 3 values
17 private:
18 std::string courseName; // course name for this GradeBook
19 int maximumGrade; // maximum of three grades
20 }; // end class GradeBook
1 // Fig. 6.3: GradeBook.cpp
2 // Member-function definitions for class GradeBook that
3 // determines the maximum of three grades.
4 #include <iostream>
5 using namespace std;
6
7 #include "GradeBook.h" // include definition of class GradeBook
8
9 // constructor initializes courseName with string supplied as argument;
10 // initializes maximumGrade to 0
11 GradeBook::GradeBook( string name )
12 : maximumGrade( 0 ) // this value will be replaced by the maximum grade
13 {
14 setCourseName( name ); // validate and store courseName
15 } // end GradeBook constructor
16
17 // function to set the course name; limits name to 25 or fewer characters
18 void GradeBook::setCourseName( string name )
19 {
20 if ( name.size() <= 25 ) // if name has 25 or fewer characters
21 courseName = name; // store the course name in the object
22 else // if name is longer than 25 characters
23 { // set courseName to first 25 characters of parameter name
24 courseName = name.substr( 0, 25 ); // select first 25 characters
25 cerr << "Name "" << name << "" exceeds maximum length (25).
"
26 << "Limiting courseName to first 25 characters.
" << endl;
27 } // end if...else
28 } // end function setCourseName
29
30 // function to retrieve the course name
31 string GradeBook::getCourseName() const
32 {
33 return courseName;
34 } // end function getCourseName
35
36 // display a welcome message to the GradeBook user
37 void GradeBook::displayMessage() const
38 {
39 // this statement calls getCourseName to get the
40 // name of the course this GradeBook represents
41 cout << "Welcome to the grade book for
" << getCourseName() << "!
"
42 << endl;
43 } // end function displayMessage
44
45 // input three grades from user; determine maximum
46 void GradeBook::inputGrades()
47 {
48 int grade1; // first grade entered by user
49 int grade2; // second grade entered by user
50 int grade3; // third grade entered by user
51
52 cout << "Enter three integer grades: ";
53 cin >> grade1 >> grade2 >> grade3;
54
55 // store maximum in member maximumGrade
56 maximumGrade = maximum( grade1, grade2, grade3 );
57 } // end function inputGrades
58
59 // returns the maximum of its three integer parameters
60 int GradeBook::maximum( int x, int y, int z ) const
61 {
62 int maximumValue = x; // assume x is the largest to start
63
64 // determine whether y is greater than maximumValue
65 if ( y > maximumValue )
66 maximumValue = y; // make y the new maximumValue
67
68 // determine whether z is greater than maximumValue
69 if ( z > maximumValue )
70 maximumValue = z; // make z the new maximumValue
71
72 return maximumValue;
73 } // end function maximum
74
75 // display a report based on the grades entered by user
76 void GradeBook::displayGradeReport() const
77 {
78 // output maximum of grades entered
79 cout << "Maximum of grades entered: " << maximumGrade << endl;
80 } // end function displayGradeReport
1 // Fig. 6.4: fig06_04.cpp
2 // Create GradeBook object, input grades and display grade report.
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 three integer grades: 86 67 75
Maximum of grades entered: 86
Welcome to the grade book for
CS101 C++ Programming!
Enter three integer grades: 67 86 75
Maximum of grades entered: 86
Welcome to the grade book for
CS101 C++ Programming!
Enter three integer grades: 67 75 86
Maximum of grades entered: 86
Software Engineering Observation 6.1
The commas used in line 56 of Fig. 6.3 to separate the arguments to function maximum are not comma operators as discussed in Section 5.3. The comma operator guarantees that its operands are evaluated left to right. The order of evaluation of a function’s arguments, however, is not specified by the C++ standard. Thus, different compilers can evaluate function arguments in different orders. The C++ standard does guarantee that all arguments in a function call are evaluated before the called function executes.
Portability Tip 6.1
Sometimes when a function’s arguments are expressions, such as those with calls to other functions, the order in which the compiler evaluates the arguments could affect the values of one or more of the arguments. If the evaluation order changes between compilers, the argument values passed to the function could vary, causing subtle logic errors.
Error-Prevention Tip 6.2
If you have doubts about the order of evaluation of a function’s arguments and whether the order would affect the values passed to the function, evaluate the arguments in separate assignment statements before the function call, assign the result of each expression to a local variable, then pass those variables as arguments to the function.