// get part number
string Invoice::getPartNumber() {
return partNumber;
} // end function getPartNumber // set part description
void Invoice::setPartDescription( string description ) {
partDescription = description; // no validation needed } // end function setPartDescription // get part description
string Invoice::getPartDescription() {
return partDescription;
} // end function getPartDescription
// set quantity; if not positive, set to 0 void 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 << \; } // end if
} // end function setQuantity // get quantity
int Invoice::getQuantity() {
return quantity;
} // end function getQuantity
// set price per item; if not positive, set to 0 void 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 << \ << \; } // end if
} // end function setPricePerItem // get price per item
int Invoice::getPricePerItem() {
return pricePerItem;
} // end function getPricePerItem
// calulates invoice amount by multiplying quantity x price per item int Invoice::getInvoiceAmount() {
return getQuantity() * getPricePerItem(); } // end function getInvoiceAmount 测试函数:
#include
// include definition of class Invoice from Invoice.h #include \
// function main begins program execution int main() {
// create an Invoice object
Invoice invoice( \, \, 100, 5 );
// display the invoice data members and calculate the amount cout << \ << invoice.getPartNumber() << endl; cout << \description: \ << invoice.getPartDescription() << endl; cout << \ << invoice.getQuantity() << endl;
cout << \ << invoice.getPricePerItem() << endl; cout << \ << invoice.getInvoiceAmount() << endl; // modify the invoice data members invoice.setPartNumber( \ ); invoice.setPartDescription( \ );
invoice.setQuantity( -5 ); //negative quantity,so quantity set to 0 invoice.setPricePerItem( 10 );
cout << \;
// display the modified invoice data members and calculate new amount cout << \ << invoice.getPartNumber() << endl; cout << \description: \ << invoice.getPartDescription() << endl; cout << \ << invoice.getQuantity() << endl;
cout << \ << invoice.getPricePerItem() << endl; cout << \ << invoice.getInvoiceAmount() << endl; return 0; // indicate successful termination } // end main 3.14 类定义:
#include
using std::string;
// Employee class definition class 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 salary private:
string firstName; // Employee's first name string lastName; // Employee's last name
int monthlySalary; // Employee's salary per month }; // end class Employee 类成员函数:
#include
#include \ // Employee class definition
// Employee constructor initializes the three data members Employee::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 name
void Employee::setFirstName( string name ) {
firstName = name; // no validation needed } // end function setFirstName // return first name
string Employee::getFirstName() {
return firstName;
} // end function getFirstName // set last name
void Employee::setLastName( string name ) {
lastName = name; // no validation needed } // end function setLastName // return last name
string Employee::getLastName() {
return lastName;
} // end function getLastName
// set monthly salary; if not positive, set to 0.0 void Employee::setMonthlySalary( int salary ) {
if ( salary > 0 ) // if salary is positive
monthlySalary = salary; // set monthlySalary to salary if ( salary <= 0 ) // if salary is not positive monthlySalary = 0; // set monthlySalary to 0.0 } // end function setMonthlySalary // return monthly salary
int Employee::getMonthlySalary() {
return monthlySalary;
} // end function getMonthlySalary 测试函数:
#include
#include \ // include definition of class Employee // function main begins program execution int main() {
// create two Employee objects
Employee employee1( \, \, 4500 ); Employee employee2( \, \, 4000 ); // display each Employee's yearly salary
cout << \ << endl;
// retrieve and display employee1's monthly salary multiplied by 12 int monthlySalary1 = employee1.getMonthlySalary();
cout << employee1.getFirstName() << \ << employee1.getLastName() << \ << monthlySalary1 * 12 << endl;
// retrieve and display employee2's monthly salary multiplied by 12 int monthlySalary2 = employee2.getMonthlySalary();
cout << employee2.getFirstName() << \ << employee2.getLastName() << \ << monthlySalary2 * 12 << endl; // give each Employee a 10% raise
employee1.setMonthlySalary( monthlySalary1 * 1.1 ); employee2.setMonthlySalary( monthlySalary2 * 1.1 ); // display each Employee's yearly salary again
cout << \ << endl; // retrieve and display employee1's monthly salary multiplied by 12
monthlySalary1 = employee1.getMonthlySalary();
cout << employee1.getFirstName() << \ << employee1.getLastName() << \ << monthlySalary1 * 12 << endl;
monthlySalary2 = employee2.getMonthlySalary();
cout << employee2.getFirstName() << \ << employee2.getLastName() << \ << monthlySalary2 * 12 << endl; return 0; // indicate successful termination } // end main 3.15 类定义: class Date {
public:
Date( int, int, int ); // constructor initializes data members void setMonth( int ); // set month int getMonth(); // return month void setDay( int ); // set day int getDay(); // return day
void setYear( int ); // set year int getYear(); // return year
void displayDate(); // displays date in mm/dd/yyyy format private:
int month; // the month of the date int day; // the day of the date int year; // the year of the date }; // end class Date 类成员函数:
#include
#include \ // include definition of class Date from Date.h // Date constructor that initializes the three data members; // assume values provided are correct (really should validate) Date::Date( int m, int d, int y ) {
setMonth( m ); setDay( d ); setYear( y );
} // end Date constructor // set month
void Date::setMonth( int m ) {
month = m;
if ( month < 1 )