DOC PREVIEW
UVa-Wise COSC 181 - Foundations of Computer Programming

This preview shows page 1-2-20-21 out of 21 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21COSC 181 – Foundations of Computer ProgrammingClass 2026.4 Function Definitions with Multiple ParametersMultiple parametersFunctions often require more than one piece of information to perform their tasksSpecified in both the function prototype and the function header as a comma-separated list of parameters31 // Fig. 6.3: GradeBook.h2 / / Def in it io n of c lass GradeBook that f inds the max imum of three grades.3 / / Member funct ions are def ined in GradeBook.cpp4 #include <string> // program uses C++ standard string class5 us ing std::string;67 // GradeBook c lass def init ion8 class GradeBook9 {10 public:11 GradeBook( string ); // constructor initializes course name12 void setCourseName( string ); // function to set the course name13 string getCourseName(); // function to retrieve the course name14 void displayMessage(); // display a welcome message15 void inputGrades(); // input three grades from user16 void displayGradeReport(); // display a report based on the grades17 int maximum( int, int, int ); // determine max of 3 values18 private:19 string courseName; // course name for this GradeBook20 int maximumGrade; // maximum of three grades21 }; // end class GradeBook OutlineGradeBook.h (1 of 1)Prototype for a member function that takes three argumentsData member to store maximum grade41 // Fig. 6.4: GradeBook.cpp2 / / Member- funct ion def in i t io ns for class GradeBook that3 / / determines the max imum of three grades.4 #include <iostream>5 us ing std::cout;6 us ing std::cin;7 us ing std::endl;89 #include "GradeBook.h" // include definition of class GradeBook1011 // constructor initializes courseName with string supplied as argument;12 // initializes studentMaximum to 013 GradeBook::GradeBook( string name )14 {15 setCourseName( name ); // validate and store courseName16 maximumGrade = 0; // this value will be replaced by the maximum grade17 } // end GradeBook constructor1819 // function to set the course name; limits name to 25 or fewer characters20 void GradeBook::setCourseName( string name )21 {22 if ( name.length() <= 25 ) // if name has 25 or fewer characters23 courseName = name; // store the course name in the object24 else // if name is longer than 25 characters25 { // set courseName to first 25 characters of parameter name26 courseName = name.substr( 0, 25 ); // select first 25 characters27 cout << "Name \"" << name << "\" exceeds maximum length (25).\n"28 << "Limiting courseName to first 25 characters.\n" << endl;29 } // end if...else30 } // end function setCourseNameOutlineGradeBook.cpp (1 of 3)53132 / / funct ion to ret r ie ve the course name33 string GradeBook::getCourseName()34 {35 return courseName;36 } / / end funct ion getCourseName3738 / / disp lay a welcome message to the GradeBook user39 void GradeBook::displayMessage()40 {41 / / th is statement cal ls getCourseName to get the 42 / / name of the course this GradeBook represents43 cout << "Welcome to the grade book for \ n " << getCourseName() << "!\n" 44 << endl;45 } / / end funct ion displayMessage4647 / / input three grades from user; determ ine maximum48 void GradeBook::inputGrades()49 {50 int grade1; / / f ir s t grade entered by user51 int grade2; / / second grade entered by user52 int grade3; / / th ir d grade entered by user5354 cout << "Enter three integer grades: ";55 cin >> grade1 >> grade2 >> grade3;5657 / / store max imum in member studentMax imum58 maximumGrade = maximum( grade1, grade2, grade3 );59 } / / end funct ion inputGradesOutlineGradeBook.cpp(2 of 3)Call to function maximum passes three arguments66061 / / returns the max imum of it s three integer parameters 62 int GradeBook::maximum( int x, int y, int z ) 63 { 64 int maximumValue = x; // assume x is the largest to start65 66 // determine whether y is greater than maximumValue 67 if ( y > maximumValue ) 68 maximumValue = y; // make y the new maximumValue 69 70 // determine whether z is greater than maximumValue 71 if ( z > maximumValue ) 72 maximumValue = z; // make z the new maximumValue 73 74 return maximumValue; 75 } // end function maximum 7677 // display a report based on the grades entered by user78 void GradeBook::displayGradeReport()79 {80 // output maximum of grades entered81 cout << "Max imum of grades entered: " << maximumGrade << endl;82 } // end function displayGradeReportOutlineGradeBook.cpp (3 of 3)maximum member function headerComma-separated parameter listReturning a value to the caller71 // Fig. 6.5: f ig06_05.cpp2 / / Create GradeBook object , input grades and disp lay grade report .3 #inc lude "GradeBook.h" // include definition of class GradeBook45 int main()6 {7 // create GradeBook object8 GradeBook myGradeBook( "CS101 C++ Programming" );910 myGradeBook.displayMessage(); // display welcome message11 myGradeBook.inputGrades(); // read grades from user 12 myGradeBook.displayGradeReport(); // display report based on grades13 return 0; // indicate successful termination14 } // end mainWelcome to the grade book forCS101 C++ Programming!Enter three integer grades: 86 67 75Maximum of grades entered: 86Welcome to the grade book forCS101 C++ Programming!Enter three integer grades: 67 86 75Maximum of grades entered: 86Welcome to the grade book forCS101 C++ Programming!Enter three integer grades: 67 75 86Maximum of grades entered: 86Outlinefig06_05.cpp (1 of 1)8Software Engineering Observation 6.4 The commas used in line 58 of Fig. 6.4 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


View Full Document

UVa-Wise COSC 181 - Foundations of Computer Programming

Download Foundations of Computer Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Foundations of Computer Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Foundations of Computer Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?