3.11
GradeBook类定义:
#include
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 welcome message and instructor name private:
string courseName; // course name for this GradeBook
string instructorName; // instructor name for this GradeBook }; // end class GradeBook 类成员函数:
#include
#include \
// constructor initializes courseName and instructorName // with strings supplied as arguments
GradeBook::GradeBook( string course, string instructor ) {
setCourseName( course ); // initializes courseName
setInstructorName( instructor ); // initialiZes instructorName } // end GradeBook constructor
// function to set the course name
void GradeBook::setCourseName( string name ) {
courseName = name; // store the course name } // end function setCourseName
// function to retrieve the course name string GradeBook::getCourseName() {
return courseName;
} // end function getCourseName
// function to set the instructor name
void GradeBook::setInstructorName( string name ) {
instructorName = name; // store the instructor name
} // end function setInstructorName
// function to retrieve the instructor name string GradeBook::getInstructorName() {
return instructorName;
} // end function getInstructorName
// display a welcome message and the instructor's name void GradeBook::displayMessage() {
// display a welcome message containing the course name
cout << \ << getCourseName() << \ << endl;
// display the instructor's name cout << \course is presented by: \ << getInstructorName() << endl; } // end function displayMessage 测试文件:
#include
// include definition of class GradeBook from GradeBook.h #include \
// function main begins program execution int main() {
// create a GradeBook object; pass a course name and instructor name GradeBook gradeBook(
\, \ ); // display initial value of instructorName of GradeBook object cout << \
<< gradeBook.getInstructorName() << \; // modify the instructorName using set function
gradeBook.setInstructorName( \ ); // display new value of instructorName
cout << \ << gradeBook.getInstructorName() << \; // display welcome message and instructor's name gradeBook.displayMessage();
return 0; // indicate successful termination } // end main 3.12 类定义:
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 balance private:
int balance; // data member that stores the balance }; // end class Account 类成员函数:
#include
#include \ // include definition of class Account // Account constructor initializes data member balance Account::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 << \ << endl; } // end Account constructor
// credit (add) an amount to the account balance void Account::credit( int amount ) {
balance = balance + amount; // add amount to balance } // end function credit
// debit (subtract) an amount from the account balance void Account::debit( int amount ) {
if ( amount > balance ) // debit amount exceeds balance
cout << \ << endl; if ( amount <= balance ) // debit amount does not exceed balance balance = balance - amount; } // end function debit
// return the account balance int Account::getBalance() {
return balance; // gives the value of balance to the calling function } // end function getBalance 测试函数:
#include
using std::cout; using std::cin; using std::endl;
// include definition of class Account from Account.h #include \
// function main begins program execution int main() {
Account account1( 50 ); // create Account object Account account2( 25 ); // create Account object
// display initial balance of each object
cout << \ << account1.getBalance() << endl; cout << \ << account2.getBalance() << endl;
int withdrawalAmount; // stores withdrawal amount read from user
cout << \; // prompt cin >> withdrawalAmount; // obtain user input
cout << \ << withdrawalAmount << \;
account1.debit( withdrawalAmount ); // try to subtract from account1
// display balances
cout << \ << account1.getBalance() << endl; cout << \ << account2.getBalance() << endl;
cout << \; // prompt cin >> withdrawalAmount; // obtain user input
cout << \ << withdrawalAmount << \;
account2.debit( withdrawalAmount ); // try to subtract from account2
// display balances
cout << \ << account1.getBalance() << endl; cout << \ << account2.getBalance() << endl; return 0; // indicate successful termination } // end main 3.13 类定义:
#include
// Invoice class definition class Invoice {
public:
// constructor initializes the four data members Invoice( string, string, int, int );
// set and get functions for the four data members void setPartNumber( string ); // part number string getPartNumber();
void setPartDescription( string ); // part description string getPartDescription();
void setQuantity( int ); // quantity int getQuantity();
void setPricePerItem( int ); // price per item int getPricePerItem();
// calculates invoice amount by multiplying quantity x price per item int getInvoiceAmount(); private:
string partNumber; // the number of the part being sold
string partDescription; // description of the part being sold int quantity; // how many of the items are being sold int pricePerItem; // price per item }; // end class Invoice 类成员函数:
#include
// include definition of class Invoice from Invoice.h #include \
// Invoice constructor initializes the class's four data members Invoice::Invoice( string number, string description, int count, int price ) {
setPartNumber( number ); // store partNumber
setPartDescription( description ); // store partDescription setQuantity( count ); // validate and store quantity
setPricePerItem( price ); // validate and store pricePerItem } // end Invoice constructor
// set part number
void Invoice::setPartNumber( string number ) {
partNumber = number; // no validation needed } // end function setPartNumber