【正文】
ul termination} // end main類定義:include string // program uses C++ standard string classusing std::string。// Invoice class definitionclass 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類成員函數(shù):include iostreamusing std::cout。using std::endl。// include definition of class Invoice from include // Invoice constructor initializes the class39。s four data membersInvoice::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 numbervoid Invoice::setPartNumber( string number ){ partNumber = number。 // no validation needed} // end function setPartNumber// get part numberstring Invoice::getPartNumber(){ return partNumber。} // end function getPartNumber// set part descriptionvoid Invoice::setPartDescription( string description ){ partDescription = description。 // no validation needed} // end function setPartDescription// get part descriptionstring Invoice::getPartDescription(){ return partDescription。} // end function getPartDescription// set quantity。 if not positive, set to 0void Invoice::setQuantity( int count ){ if ( count 0 ) // if quantity is positive quantity = count。 // set quantity to count if ( count = 0 ) // if quantity is not positive { quantity = 0。 // set quantity to 0 cout \nquantity cannot be negative. quantity set to 0.\n。 } // end if} // end function setQuantity// get quantityint Invoice::getQuantity(){ return quantity。} // end function getQuantity// set price per item。 if not positive, set to 0void Invoice::setPricePerItem( int price ){ if ( price 0 ) // if price is positive pricePerItem = price。 // set pricePerItem to price if ( price = 0 ) // if price is not positive { pricePerItem = 0。 // set pricePerItem to 0 cout \npricePerItem cannot be negative. pricePerItem set to 0.\n。 } // end if} // end function setPricePerItem// get price per itemint Invoice::getPricePerItem(){ return pricePerItem。} // end function getPricePerItem// calulates invoice amount by multiplying quantity x price per itemint Invoice::getInvoiceAmount(){ return getQuantity() * getPricePerItem()。} // end function getInvoiceAmount測試函數(shù):include iostreamusing std::cout。using std::cin。using std::endl。// include definition of class Invoice from include // function main begins program executionint main(){ // create an Invoice object Invoice invoice( 12345, Hammer, 100, 5 )。 // display the invoice data members and calculate the amount cout Part number: () endl。 cout Part description: () endl。 cout Quantity: () endl。 cout Price per item: $ () endl。 cout Invoice amount: $ () endl。 // modify the invoice data members ( 123456 )。 ( Saw )。 ( 5 )。 //negative quantity,so quantity set to 0 ( 10 )。 cout \nInvoice data members modified.\n\n。 // display the modified invoice data members and calculate new amount cout Part number: () endl。 cout Part description: () endl。 cout Quantity: () endl。 cout Price per item: $ () endl。 cout Invoice amount: $ () endl。 return 0。 // indicate successful termination} // end main類定義:include string // program uses C++ standard string classusing std::string。// Employee class definitionclass Employee {public: Employee( string, string, int )。 // constructor sets data members void setFirstName( string )。 // set first name string getFirstName()。 // return first name void setLastName( string )。 // set last name string getLastName()。 // return last name void setMonthlySalary( int )。 // set weekly salary int getMonthlySalary()。 // return weekly salaryprivate: string firstName。 // Employee39。s first name string lastName。 // Employee39。s last name int monthlySalary。 // Employee39。s salary per month}。 // end class Employee類成員函數(shù):include iostreamusing std::cout。include // Employee class definition// Employee constructor initializes the three data membersEmployee::Employee( string first, string last, int salary ){ setFirstName( first )。 // store first name setLastName( last )。 // store last name setMonthlySalary( salary )。 // validate and store monthly salary} // end Employee constructor// set first namevoid Employee::setFirstName( string name ){ firstName = name。 // no validation needed} // end function setFirstName// return first namestring Employee::getFirstName(){ return firstName。} // end function getFirstName// set last namevoid Employee::setLastName( string name ){ lastName = name。 // no validation needed} // end function setLastName// return last