【文章內(nèi)容簡介】
namestring Employee::getLastName(){ return lastName。} // end function getLastName// set monthly salary。 if not positive, set to 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 } // end function setMonthlySalary// return monthly salaryint Employee::getMonthlySalary(){ return monthlySalary。} // end function getMonthlySalary測試函數(shù):include iostreamusing std::cout。using std::endl。include // include definition of class Employee// function main begins program executionint main(){ // create two Employee objects Employee employee1( Lisa, Roberts, 4500 )。 Employee employee2( Mark, Stein, 4000 )。 // display each Employee39。s yearly salary cout Employees39。 yearly salaries: endl。 // retrieve and display employee139。s monthly salary multiplied by 12 int monthlySalary1 = ()。 cout () () : $ monthlySalary1 * 12 endl。 // retrieve and display employee239。s monthly salary multiplied by 12 int monthlySalary2 = ()。 cout () () : $ monthlySalary2 * 12 endl。 // give each Employee a 10% raise ( monthlySalary1 * )。 ( monthlySalary2 * )。 // display each Employee39。s yearly salary again cout \nEmployees39。 yearly salaries after 10% raise: endl。 // retrieve and display employee139。s monthly salary multiplied by 12 monthlySalary1 = ()。 cout () () : $ monthlySalary1 * 12 endl。 monthlySalary2 = ()。 cout () () : $ monthlySalary2 * 12 endl。 return 0。 // indicate successful termination} // end main類定義: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 formatprivate: int month。 // the month of the date int day。 // the day of the date int year。 // the year of the date}。 // end class Date類成員函數(shù):include iostreamusing std::cout。using std::endl。include // include definition of class Date from // 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 monthvoid Date::setMonth( int m ){ month = m。 if ( month 1 ) month = 1。 if ( month 12 ) month = 1。} // end function setMonth// return monthint Date::getMonth(){ return month。} // end function getMonth// set dayvoid Date::setDay( int d ){ day = d。} // end function setDay// return dayint Date::getDay(){ return day。} // end function getDay// set yearvoid Date::setYear( int y ){ year = y。} // end function setYear// return yearint Date::getYear(){ return year。} // end function getYear// print Date in the format mm/dd/yyyyvoid Date::displayDate() { cout month 39。/39。 day 39。/39。 year endl。 } // end function displayDate測試函數(shù):include iostreamusing std::cout。using std::endl。include // include definition of class Date from // function main begins program executionint main(){ Date date( 5, 6, 1981 )。 // create a Date object for May 6, 1981 // display the values of the three Date data members cout Month: () endl。 cout Day: () endl。 cout Year: () endl。 cout \nOriginal date: endl。 ()。 // output the Date as 5/6/1981 // modify the Date ( 13 )。 // invalid month ( 1 )。 ( 2005 )。 cout \nNew date: endl。 ()。 // output the modified date (1/1/2005) return 0。 // indicate successful termination} // end main類定義:ifndef COMPLEX_Hdefine COMPLEX_Hclass Complex {public: Complex( double = , double = )。 // default constructor Complex add( const Complex amp。 )。 // function add Complex subtract( const Complex amp。 )。 // function subtract void printComplex()。 // print plex number format void setComplexNumber( double, double )。 // set plex number private: double realPart。 double imaginaryPart。}。 // end class Complex endif類成員函數(shù):include iostream using std::cout。 include Complex::Complex( double real, double imaginary ){ setComplexNumber( real, imaginary )。 } // end Complex constructorComplex Complex::add( const Complex amp。right ){ return Complex( realPart + , imaginaryPart + )。} // end function addComplex Complex::subtract( const Complex amp。right ){ return Complex( realPart , imaginaryPart )。} // end function subtractvoid Complex::printComplex(){ cout 39。(39。 realPart , imaginaryPart 39。)39。} // end function printComplexvoid Complex::setComplexNumber( double rp, double ip ) { realPart = rp。 imaginaryPart = ip。} // end function setComplexNumber測試函數(shù):include iostream using std::cout。 using std::endl。 include int main(){ Complex a( 1, 7 ), b( 9, 2 ), c。 // create three Complex objects ()。 // output object a cout + 。 ()。 // output object b cout = 。 c = ( b )。 // invoke add function and assign to object c ()。 // output object c cout 39。\n39。 ( 10, 1 )。 // reset realPart and ( 11, 5 )。 // and imaginaryPart ()。 // output object a cout 。 ()。 // output object b cout = 。 c = ( b )。 // invoke add function and assign to object c ()。 // output object c