#ifndef DATEANDTIME_H#define DATEANDTIME_H
class DateAndTime
{
public:
DateAndTime( int = 1, int = 1, int = 2000,
int = 0, int = 0, int = 0 ); // default constructorvoid setDate( int, int, int ); // set month, day, yearvoid setMonth( int ); // set monthvoid setDay( int ); // set day void setYear( int ); // set yearvoid nextDay(); // next dayvoid setTime( int, int, int ); // set hour, minute, secondvoid setHour( int ); // set hourvoid setMinute( int ); // set minute void setSecond( int ); // set secondvoid tick(); // tick function int getMonth(); // get monthint getDay(); // get dayint getYear(); // get yearint getHour(); // get hourint getMinute(); // get minuteint getSecond(); // get second void printStandard(); // print standard timevoid printUniversal(); // print universal timeprivate:
int month; // 1-12 int day; // 1-31 (except February(leap year), April, June, Sept, Nov)int year; // 2000+int hour; // 0-23 (24 hour clock format)int minute; // 0-59 int second; // 0-59bool leapYear(); // leap yearint monthDays(); // days in month
}; // end class DateAndTime#endif
DateAndTime.cpp
#include <iostream>
#include "DateAndTime.h" // include definition of class DateAndTime
using namespace std;
DateAndTime::DateAndTime(
int m, int d, int y, int hr, int min, int sec )
{
setDate( m, d, y ); // sets date
setTime( hr, min, sec ); // sets time
} // end DateAndTime constructor
void DateAndTime::setDate( int mo, int dy, int yr )
{
setMonth( mo ); // invokes functionsetMonthsetDay( dy ); // invokes functionsetdaysetYear( yr ); // invokes functionsetYear
} // endfunctionsetDatevoidDateAndTime::setDay( int d )
{
if ( month == 2 && leapYear() )
day = ( d <= 29 && d >= 1 ) ? d : 1;
else
day = ( d <= monthDays() && d >= 1 ) ? d : 1;
} // endfunctionsetDayvoidDateAndTime::setMonth( int m )
{
month = m <= 12 && m >= 1 ? m : 1; // sets month
} // endfunctionsetMonthvoidDateAndTime::setYear( int y )
{
year = y >= 2000 ? y : 2000; // sets year
} // endfunctionsetYearvoidDateAndTime::nextDay()
{
setDay( day + 1 ); // increments day by 1if ( day == 1 )
{
setMonth( month + 1 ); // increments month by 1if ( month == 1 )
setYear( year + 1 ); // increments year by 1
} // endif statement
} //endfunctionnextDayvoidDateAndTime::setTime( int hr, int min, int sec )
{
setHour( hr ); // invokes functionsetHoursetMinute( min ); // invokes functionsetMinutesetSecond( sec ); // invokes functionsetSecond
} // endfunctionsetTimevoidDateAndTime::setHour( int h )
{
hour = ( h >= 0 && h < 24 ) ? h : 0; // sets hour
} // endfunctionsetHourvoidDateAndTime::setMinute( int m )
{
minute = ( m >= 0 && m < 60 ) ? m : 0; // sets minute
} // endfunctionsetMinutevoidDateAndTime::setSecond( int s )
{
second = ( s >= 0 && s < 60 ) ? s : 0; // sets second
} // endfunctionsetSecondvoidDateAndTime::tick()
{
setSecond( second + 1 ); // increments second by 1if ( second == 0 )
{
setMinute( minute + 1 ); // increments minute by 1if ( minute == 0 )
{
setHour( hour + 1 ); // increments hour by 1if ( hour == 0 )
nextDay(); // increments day by 1
} // endif
} // endif
} // endfunctiontickintDateAndTime::getDay()
{
return day;
} // endfunctiongetDayintDateAndTime::getMonth()
{
return month;
} // endfunctiongetMonthintDateAndTime::getYear()
{
return year;
} // endfunctiongetYearintDateAndTime::getHour()
{
return hour;
} // endfunctiongetHourintDateAndTime::getMinute()
{
return minute;
} // endfunctiongetMinuteintDateAndTime::getSecond()
{
return second;
} // endfunctiongetSecondvoidDateAndTime::printStandard()
{
cout << ( ( hour % 12 == 0 ) ? 12 : hour % 12 ) << ':'
<< ( minute < 10 ? "0" : "" ) << minute << ':'
<< ( second < 10 ? "0" : "" ) << second
<< ( hour < 12 ? " AM " : " PM " )
<< month << '-' << day << '-' << year << endl;
} // endfunctionprintStandardvoidDateAndTime::printUniversal()
{
cout << ( hour < 10 ? "0" : "" ) << hour << ':'
<< ( minute < 10 ? "0" : "" ) << minute << ':'
<< ( second < 10 ? "0" : "" ) << second << " "
<< month << '-' << day << '-' << year << endl;
} // endfunctionprintUniversalboolDateAndTime::leapYear()
{
if ( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) )
returntrue; // is a leap year
elsereturnfalse; // is not a leap year
} // endfunctionleapYearintDateAndTime::monthDays()
{
const int days[ 12 ] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
return ( month == 2 && leapYear() ) ? 29 : days[ ( month - 1 ) ];
} // endfunctionmonthDays
Main
#include <iostream> #include "DateAndTime.h" // include definitions of class DateAndTimeusingnamespacestd;
int main()
{
constint MAXTICKS = 30;
DateAndTime d( 12, 31, 2004, 23, 59, 57 ); // instantiates object d // of class DateAndTimefor ( int ticks = 1; ticks <= MAXTICKS; ticks++ )
{
cout << "Universal time: ";
d.printUniversal(); // invokes function printUniversalcout << "Standard time: ";
d.printStandard(); // invokes function printStandard
d.tick(); // invokes function tick
} // end forcout << endl;
} // end main