} // end function reduction 测试函数:
#include
#include \ // include definition of class Rational int main() {
Rational c( 2, 6 ), d( 7, 8 ), x; // creates three rational objects c.printRational(); // prints rational object c cout << \;
d.printRational(); // prints rational object d
x = c.addition( d ); // adds object c and d; sets the value to x cout << \;
x.printRational(); // prints rational object x cout << '\\n';
x.printRational(); // prints rational object x cout << \;
x.printRationalAsDouble(); // prints rational object x as double cout << \;
c.printRational(); // prints rational object c cout << \;
d.printRational(); // prints rational object d
x = c.subtraction( d ); // subtracts object c and d cout << \;
x.printRational(); // prints rational object x cout << '\\n';
x.printRational(); // prints rational object x cout << \;
x.printRationalAsDouble(); // prints rational object x as double cout << \;
c.printRational(); // prints rational object c cout << \;
d.printRational(); // prints rational object d
x = c.multiplication( d ); // multiplies object c and d cout << \;
x.printRational(); // prints rational object x cout << '\\n';
x.printRational(); // prints rational object x cout << \;
x.printRationalAsDouble(); // prints rational object x as double cout << \;
c.printRational(); // prints rational object c cout << \;
d.printRational(); // prints rational object d x = c.division( d ); // divides object c and d cout << \;
x.printRational(); // prints rational object x cout << '\\n';
x.printRational(); // prints rational object x cout << \;
x.printRationalAsDouble(); // prints rational object x as double cout << endl; return 0; } // end main 9.07 类定义:
#ifndef TIME_H #define TIME_H class Time {
public: public:
Time( int = 0, int = 0, int = 0 ); // default constructor // set functions
void setTime( int, int, int ); // set hour, minute, second void setHour( int ); // set hour (after validation)
void setMinute( int ); // set minute (after validation) void setSecond( int ); // set second (after validation) // get functions
int getHour(); // return hour
int getMinute(); // return minute int getSecond(); // return second void tick(); // increment one second
void printUniversal(); // output time in universal-time format void printStandard(); // output time in standard-time format private:
int hour; // 0 - 23 (24-hour clock format) int minute; // 0 - 59 int second; // 0 - 59 }; // end class Time #endif
类成员函数:
#include
#include \ // include definition of class Time from Time.h // Time constructor initializes each data member to zero; // ensures that Time objects start in a consistent state Time::Time( int hr, int min, int sec ) { setTime( hr, min, sec ); // validate and set time } // end Time constructor // set new Time value using universal time; ensure that
// the data remains consistent by setting invalid values to zero void Time::setTime( int h, int m, int s ) {
setHour( h ); // set private field hour
setMinute( m ); // set private field minute setSecond( s ); // set private field second } // end function setTime // set hour value
void Time::setHour( int h ) {
hour = ( h >= 0 && h < 24 ) ? h : 0; // validate hour } // end function setHour // set minute value
void Time::setMinute( int m ) {
minute = ( m >= 0 && m < 60 ) ? m : 0; // validate minute } // end function setMinute // set second value
void Time::setSecond( int s ) {
second = ( s >= 0 && s < 60 ) ? s : 0; // validate second } // end function setSecond // return hour value int Time::getHour() {
return hour;
} // end function getHour // return minute value int Time::getMinute() {
return minute;
} // end function getMinute // return second value int Time::getSecond() {
return second;
} // end function getSecond // increment one second void Time::tick() {
setSecond( getSecond() + 1 ); // increment second by 1 if ( getSecond() == 0 ) {
setMinute( getMinute() + 1 ); // increment minute by 1 if ( getMinute() == 0 )
setHour( getHour() + 1 ); // increment hour by 1 } // end if
} // end function tick
// print Time in universal-time format (HH:MM:SS) void Time::printUniversal() {
cout << setfill( '0' ) << setw( 2 ) << getHour() << \
<< setw( 2 ) << getMinute() << \ << setw( 2 ) << getSecond(); } // end function printUniversal
// print Time in standard-time format (HH:MM:SS AM or PM) void Time::printStandard() {
cout << (( getHour() == 0 || getHour() == 12 ) ? 12 : getHour() % 12 ) << \ << setfill( '0' ) << setw( 2 ) << getMinute()
<< \ << setw( 2 ) << getSecond() << ( hour < 12 ? \AM\ : \PM\ ); } // end function printStandard 测试函数:
#include
#include \ // include definition of class Time const int MAX_TICKS = 30; int main() {
Time t; // instantiate object t of class Time t.setTime( 23, 59, 57 ); // set time // output Time object t's values
for ( int ticks = 1; ticks < MAX_TICKS; ++ticks ) {
t.printStandard(); // invokes function printStandard cout << endl;
t.tick(); // invokes function tick } // end for return 0;
} // end main 9.08 类定义:
#ifndef DATE_H #define DATE_H
class Date {
public:
Date( int = 1, int = 1, int = 1900 ); // default constructor void print(); // print function
void setDate( int, int, int ); // set month, day, year void setMonth( int ); // set month void setDay( int ); // set day void setYear( int ); // set year int getMonth(); // get month int getDay(); // get day int getYear(); // get year void nextDay(); // next day private:
int month; // 1-12
int day; // 1-31 (except February(leap year), April, June, Sept, Nov) int year; // 1900+
bool leapYear(); // leap year
int monthDays(); // days in month }; // end class Date
#endif
类成员函数:
#include
#include \ // include definition of class Date
Date::Date( int m, int d, int y ) {
setDate( m, d, y ); // sets date } // end Date constructor
void Date::setDate( int mo, int dy, int yr ) {
setMonth( mo ); // invokes function setMonth setDay( dy ); // invokes function setDay setYear( yr ); // invokes function setYear
相关推荐: