(Combining Class Time and Class Date) Combine the modified Time class of Exercise 9.16 and the modified Date class of Exercise 9.17 into one class called DateAndTime. (In Chapter 12, we’ll discuss inheritance, which will enable us to accomplish this task quickly without modifying the ex- isting class definitions.) Modify the tick function to call the nextDay function if the time incre- ments

into the next day. Modify functions printStandard and printUniversal to output the date and time. Write a program to test the new class DateAndTime. Specifically, test incrementing the time into the next day.

What will be an ideal response?

```
#ifndef DATEANDTIME_H
#define DATEANDTIME_H

class DateAndTime
{
public:
DateAndTime( int = 1, int = 1, int = 2000,
int = 0, int = 0, int = 0 ); // default constructor
void setDate( int, int, int ); // set month, day, year
void setMonth( int ); // set month
void setDay( int ); // set day
void setYear( int ); // set year
void nextDay(); // next day
void setTime( int, int, int ); // set hour, minute, second
void setHour( int ); // set hour
void setMinute( int ); // set minute
void setSecond( int ); // set second
void tick(); // tick function
int getMonth(); // get month
int getDay(); // get day
int getYear(); // get year
int getHour(); // get hour
int getMinute(); // get minute
int getSecond(); // get second
void printStandard(); // print standard time
void printUniversal(); // print universal time
private:
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-59
bool leapYear(); // leap year
int monthDays(); // days in month
}; // end class DateAndTime

#endif
```
```
// Member function definitions for class DateAndTime.
#include
#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 function setMonth
setDay( dy ); // invokes function setday
setYear( yr ); // invokes function setYear
} // end function setDate

void DateAndTime::setDay( int d )
{
if ( month == 2 && leapYear() )
day = ( d <= 29 && d >= 1 ) ? d : 1;
else
day = ( d <= monthDays() && d >= 1 ) ? d : 1;
} // end function setDay

void DateAndTime::setMonth( int m )
{
month = m <= 12 && m >= 1 ? m : 1; // sets month
} // end function setMonth

void DateAndTime::setYear( int y )
{
year = y >= 2000 ? y : 2000; // sets year
} // end function setYear

void DateAndTime::nextDay()
{
setDay( day + 1 ); // increments day by 1

if ( day == 1 )
{
setMonth( month + 1 ); // increments month by 1

if ( month == 1 )
setYear( year + 1 ); // increments year by 1
} // end if statement
} //end function nextDay

void DateAndTime::setTime( int hr, int min, int sec )
{
setHour( hr ); // invokes function setHour
setMinute( min ); // invokes function setMinute
setSecond( sec ); // invokes function setSecond
} // end function setTime

void DateAndTime::setHour( int h )
{
hour = ( h >= 0 && h < 24 ) ? h : 0; // sets hour
} // end function setHour

void DateAndTime::setMinute( int m )
{
minute = ( m >= 0 && m < 60 ) ? m : 0; // sets minute
} // end function setMinute

void DateAndTime::setSecond( int s )
{
second = ( s >= 0 && s < 60 ) ? s : 0; // sets second
} // end function setSecond

void DateAndTime::tick()
{
setSecond( second + 1 ); // increments second by 1

if ( second == 0 )
{
setMinute( minute + 1 ); // increments minute by 1

if ( minute == 0 )
{
setHour( hour + 1 ); // increments hour by 1

if ( hour == 0 )
nextDay(); // increments day by 1
} // end if
} // end if
} // end function tick

int DateAndTime::getDay()
{
return day;
} // end function getDay

int DateAndTime::getMonth()
{
return month;
} // end function getMonth

int DateAndTime::getYear()
{
return year;
} // end function getYear

int DateAndTime::getHour()
{
return hour;
} // end function getHour

int DateAndTime::getMinute()
{
return minute;
} // end function getMinute

int DateAndTime::getSecond()
{
return second;
} // end function getSecond

void DateAndTime::printStandard()
{
cout << ( ( hour % 12 == 0 ) ? 12 : hour % 12 ) << ':'
<< ( minute < 10 ? "0" : "" ) << minute << ':'
<< ( second < 10 ? "0" : "" ) << second
<< ( hour < 12 ? " AM " : " PM " )
<< month << '-' << day << '-' << year << endl;
} // end function printStandard

void DateAndTime::printUniversal()
{
cout << ( hour < 10 ? "0" : "" ) << hour << ':'
<< ( minute < 10 ? "0" : "" ) << minute << ':'
<< ( second < 10 ? "0" : "" ) << second << " "
<< month << '-' << day << '-' << year << endl;
} // end function printUniversal

bool DateAndTime::leapYear()
{
if ( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) )
return true; // is a leap year
else
return false; // is not a leap year
} // end function leapYear

int DateAndTime::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 ) ];
} // end function monthDays
```
```
#include
#include "DateAndTime.h" // include definitions of class DateAndTime
using namespace std;

int main()
{
const int MAXTICKS = 30;
DateAndTime d( 12, 31, 2004, 23, 59, 57 ); // instantiates object d
// of class DateAndTime

for ( int ticks = 1; ticks <= MAXTICKS; ticks++ )
{
cout << "Universal time: ";
d.printUniversal(); // invokes function printUniversal
cout << "Standard time: ";
d.printStandard(); // invokes function printStandard
d.tick(); // invokes function tick
} // end for

cout << endl;
} // end main
```
Universal time: 23:59:57 12-31-2004
Standard time: 11:59:57 PM 12-31-2004
Universal time: 23:59:58 12-31-2004
Standard time: 11:59:58 PM 12-31-2004
Universal time: 23:59:59 12-31-2004
Standard time: 11:59:59 PM 12-31-2004
Universal time: 00:00:00 1-1-2005
Standard time: 12:00:00 AM 1-1-2005
Universal time: 00:00:01 1-1-2005
Standard time: 12:00:01 AM 1-1-2005
.
.
.

Computer Science & Information Technology

You might also like to view...

Rather than using shapes, ________ uses special effects based on text

A) a vector graphic B) WordArt C) a text box D) SmartArt

Computer Science & Information Technology

The ________ function will use average values to determine the rank of a number within a list if tied values are present

Fill in the blank(s) with correct word

Computer Science & Information Technology