【正文】
C++ 大學(xué)基礎(chǔ)教程 課后答案(DEITEL)版GradeBook類定義:include string // program uses C++ standard string classusing std::string。class GradeBook{public: // constructor initializes course name and instructor name GradeBook( string, string )。 void setCourseName( string )。 // function to set the course name string getCourseName()。 // function to retrieve the course name void setInstructorName( string )。 // function to set instructor name string getInstructorName()。 // function to retrieve instructor name void displayMessage()。 // display wele message and instructor nameprivate: string courseName。 // course name for this GradeBook string instructorName。 // instructor name for this GradeBook}。 // end class GradeBook類成員函數(shù):include iostreamusing std::cout。using std::endl。include // constructor initializes courseName and instructorName // with strings supplied as argumentsGradeBook::GradeBook( string course, string instructor ){ setCourseName( course )。 // initializes courseName setInstructorName( instructor )。 // initialiZes instructorName} // end GradeBook constructor// function to set the course namevoid GradeBook::setCourseName( string name ){ courseName = name。 // store the course name} // end function setCourseName// function to retrieve the course namestring GradeBook::getCourseName(){ return courseName。} // end function getCourseName// function to set the instructor namevoid GradeBook::setInstructorName( string name ){ instructorName = name。 // store the instructor name} // end function setInstructorName// function to retrieve the instructor namestring GradeBook::getInstructorName(){ return instructorName。} // end function getInstructorName// display a wele message and the instructor39。s namevoid GradeBook::displayMessage(){ // display a wele message containing the course name cout Wele to the grade book for\n getCourseName() ! endl。 // display the instructor39。s name cout This course is presented by: getInstructorName() endl。} // end function displayMessage測試文件:include iostreamusing std::cout。 using std::endl。// include definition of class GradeBook from include // function main begins program executionint main(){ // create a GradeBook object。 pass a course name and instructor name GradeBook gradeBook( CS101 Introduction to C++ Programming, Professor Smith )。 // display initial value of instructorName of GradeBook object cout gradeBook instructor name is: () \n\n。 // modify the instructorName using set function ( Assistant Professor Bates )。 // display new value of instructorName cout new gradeBook instructor name is: () \n\n。 // display wele message and instructor39。s name ()。 return 0。 // indicate successful termination} // end main類定義:class Account{public: Account( int )。 // constructor initializes balance void credit( int )。 // add an amount to the account balance void debit( int )。 // subtract an amount from the account balance int getBalance()。 // return the account balanceprivate: int balance。 // data member that stores the balance}。 // end class Account類成員函數(shù):include iostreamusing std::cout。using std::endl。include // include definition of class Account// Account constructor initializes data member balanceAccount::Account( int initialBalance ){ balance = 0。 // assume that the balance begins at 0 // if initialBalance is greater than 0, set this value as the // balance of the Account。 otherwise, balance remains 0 if ( initialBalance 0 ) balance = initialBalance。 // if initialBalance is negative, print error message if ( initialBalance 0 ) cout Error: Initial balance cannot be negative.\n endl。} // end Account constructor// credit (add) an amount to the account balancevoid Account::credit( int amount ){ balance = balance + amount。 // add amount to balance} // end function credit// debit (subtract) an amount from the account balancevoid Account::debit( int amount ){ if ( amount balance ) // debit amount exceeds balance cout Debit amount exceeded account balance.\n endl。 if ( amount = balance ) // debit amount does not exceed balance balance = balance amount。} // end function debit// return the account balanceint Account::getBalance(){ return balance。 // gives the value of balance to the calling function} // end function getBalance測試函數(shù):include iostreamusing std::cout。using std::cin。using std::endl。// include definition of class Account from include // function main begins program executionint main(){ Account account1( 50 )。 // create Account object Account account2( 25 )。 // create Account object // display initial balance of each object cout account1 balance: $ () endl。 cout account2 balance: $ () endl。 int withdrawalAmount。 // stores withdrawal amount read from user cout \nEnter withdrawal amount for account1: 。 // prompt cin withdrawalAmount。 // obtain user input cout \nattempting to subtract withdrawalAmount from account1 balance\n\n。 ( withdrawalAmount )。 // try to subtract from account1 // display balances cout account1 balance: $ () endl。 cout account2 balance: $ () endl。 cout \nEnter withdrawal amount for account2: 。 // prompt cin withdrawalAmount。 // obtain user input cout \nattempting to subtract withdrawalAmount from account2 balance\n\n。 ( withdrawalAmount )。 // try to subtract from account2 // display balances cout account1 balance: $ () endl。 cout account2 balance: $ () endl。 return 0。 // indicate successf