Modify the Time class(attached) to be able to work with Date class. The Time object should
always remain in a consistent state.
Modify the Date class(attached) to include a Time class object as a composition, a tick member
function that increments the time stored in a Date object by one second, and increaseADay
function to increase day, month and year when it is proper. Please use CISP400V10A4.cpp that
tests the tick member function in a loop that prints the time in standard format during iteration of
the loop to illustrate that the tick member function works correctly. Be aware that we are testing
the following cases:
a) Incrementing into the next minute.
b) Incrementing into the next hour.
c) Incrementing into the next day (i.e., 11:59:59 PM to 12:00:00 AM).
d) Incrementing into the next month and next year.
You can adjust only programs (Date.cpp, Date.h, Time.cpp and Time.h) to generate the
required result but not the code in CISP400V10A4.cpp file.
Expecting results:
// Date.cpp
// Date class member-function definitions.
#include <array>
#include <string>
#include <iostream>
#include <stdexcept>
#include "Date.h" // include Date class definition
using namespace std;
// constructor confirms proper value for month; calls
// utility function checkDay to confirm proper value for day
Date::Date(int mn, int dy, int yr, Time time)
: time01(time)
{
if (mn > 0 && mn <= monthsPerYear) // validate the month
month = mn;
else
throw invalid_argument("month must be 1-12");
year = yr; // could validate yr
day = checkDay(dy); // validate the day
// output Date object to show when its constructor is called
cout << "Date object constructor for date ";
print();
cout << endl;
} // end Date constructor
// print Date object in form month/day/year
void Date::print() const
{
cout << month << '/' << day << '/' << year;
cout << "t";
time01.printStandard();
cout << "t";
time01.printUniversal();
cout << "n";
} // end function print
// output Date object to show when its destructor is called
Date::~Date()
{
cout << "Date object destructor for date ";
print();
cout << endl;
} // end ~Date destructor
// utility function to confirm proper day value based on
// month and year; handles leap years, too
unsigned int Date::checkDay(int testDay) const
{
static const array< int, monthsPerYear + 1 > daysPerMonth =
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// determine whether testDay is valid for specified month
if (testDay > 0 && testDay <= daysPerMonth[month])
{
return testDay;
} // end if
// February 29 check for leap year
if (month == 2 && testDay == 29 && (year % 400 == 0 || (year % 4 == 0 && year
% 100 != 0)))
{
return testDay;
} // end if
cout << "day (" << testDay << ") set to 1." << endl;
return 1;
} // end function checkDay
// adjust data if day is not proper
void Date::increaseADay()
{
day = checkDay(day + 1);
if (day == 1) // if day wasn't accurate, its value is one
{
month = month + 1; // increase month by 1
if (month > 0 && month >= monthsPerYear) // if.
Modify the Time classattached to be able to work with Date.pdf
1. Modify the Time class(attached) to be able to work with Date class. The Time object should
always remain in a consistent state.
Modify the Date class(attached) to include a Time class object as a composition, a tick member
function that increments the time stored in a Date object by one second, and increaseADay
function to increase day, month and year when it is proper. Please use CISP400V10A4.cpp that
tests the tick member function in a loop that prints the time in standard format during iteration of
the loop to illustrate that the tick member function works correctly. Be aware that we are testing
the following cases:
a) Incrementing into the next minute.
b) Incrementing into the next hour.
c) Incrementing into the next day (i.e., 11:59:59 PM to 12:00:00 AM).
d) Incrementing into the next month and next year.
You can adjust only programs (Date.cpp, Date.h, Time.cpp and Time.h) to generate the
required result but not the code in CISP400V10A4.cpp file.
Expecting results:
// Date.cpp
// Date class member-function definitions.
#include <array>
#include <string>
#include <iostream>
#include <stdexcept>
#include "Date.h" // include Date class definition
using namespace std;
// constructor confirms proper value for month; calls
// utility function checkDay to confirm proper value for day
Date::Date(int mn, int dy, int yr, Time time)
: time01(time)
{
if (mn > 0 && mn <= monthsPerYear) // validate the month
month = mn;
else
throw invalid_argument("month must be 1-12");
year = yr; // could validate yr
day = checkDay(dy); // validate the day
// output Date object to show when its constructor is called
cout << "Date object constructor for date ";
print();
cout << endl;
} // end Date constructor
// print Date object in form month/day/year
void Date::print() const
{
2. cout << month << '/' << day << '/' << year;
cout << "t";
time01.printStandard();
cout << "t";
time01.printUniversal();
cout << "n";
} // end function print
// output Date object to show when its destructor is called
Date::~Date()
{
cout << "Date object destructor for date ";
print();
cout << endl;
} // end ~Date destructor
// utility function to confirm proper day value based on
// month and year; handles leap years, too
unsigned int Date::checkDay(int testDay) const
{
static const array< int, monthsPerYear + 1 > daysPerMonth =
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// determine whether testDay is valid for specified month
if (testDay > 0 && testDay <= daysPerMonth[month])
{
return testDay;
} // end if
// February 29 check for leap year
if (month == 2 && testDay == 29 && (year % 400 == 0 || (year % 4 == 0 && year
% 100 != 0)))
{
return testDay;
} // end if
cout << "day (" << testDay << ") set to 1." << endl;
return 1;
} // end function checkDay
// adjust data if day is not proper
void Date::increaseADay()
{
day = checkDay(day + 1);
if (day == 1) // if day wasn't accurate, its value is one
{
month = month + 1; // increase month by 1
if (month > 0 && month >= monthsPerYear) // if month is > 12
3. {
month = 1;
if (month == 1) // if month wasn't accurate, its value is one
{
year = year + 1; // increment year by 1
} // end if
} // end if
} // end if
} // end function increaseADay
// adjust data if second is not proper
void Date::tick()
{
time01.setSecond(time01.getSecond() + 1); // increment second by 1
if (time01.getSecond() == 0) // if second went over boundary, its value is 0
{
time01.setMinute(time01.getMinute() + 1); // increment minute by 1
if (time01.getMinute() == 0) // if minute went over boundary, its value is 0
{
time01.setHour(time01.getHour() + 1); // increment hour by 1
if (time01.getHour() == 0) // if hour went over boundary, its value is 0
{
increaseADay(); // change the date
} // end if
} // end if
} // end if
} // end function tick
// Date.h
// Date class definition; Member functions defined in Date.cpp
#ifndef DATE_H
#define DATE_H
#include "Time.h" // include Time class definition
class Date
{
public:
static const unsigned int monthsPerYear = 12; // months in a year
Date(int, int, int, Time); // default constructor
void print() const; // print date in month/day/year format
~Date(); // provided to confirm destruction order
void increaseADay(); // increases private data member day by one
void tick(); // increases one second to the Time object
private:
unsigned int month; // 1-12 (January-December)
4. unsigned int day; // 1-31 based on month
unsigned int year; // any year
// utility function to check if day is proper for month and year
unsigned int checkDay(int) const;
// Time object
Time time01;
}; // end class Date
#endif
// Time.cpp
// Member-function definitions for class Time.
#include <iostream>
#include <iomanip>
#include <stdexcept>
#include "Time.h" // include definition of class Time from Time.h
using namespace std;
// Time constructor initializes each data member
Time::Time(int hour, int minute, int second)
{
setTime(hour, minute, second); // validate and set time
cout << "Time object constructor is called ";
printStandard();
cout << "t";
printUniversal();
cout << "n";
} // end Time constructor
// set new Time value using universal time
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;
} // end function setHour
// set minute value
void Time::setMinute(int m)
{
minute = (m >= 0 && m < 60) ? m : 0;
} // end function setMinute
5. // set second value
void Time::setSecond(int s)
{
second = (s >= 0 && s < 60) ? s : 0;
} // end function setSecond
// return hour value
unsigned int Time::getHour() const
{
return hour;
} // end function getHour
// return minute value
unsigned int Time::getMinute() const
{
return minute;
} // end function getMinute
// return second value
unsigned int Time::getSecond() const
{
return second;
} // end function getSecond
// print Time in universal-time format (HH:MM:SS)
void Time::printUniversal() const
{
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() const
{
cout << ((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12)
<< ":" << setfill('0') << setw(2) << getMinute()
<< ":" << setw(2) << getSecond() << (hour < 12 ? " AM" : " PM");
} // end function printStandard
// Time destructor displays message
Time::~Time()
{
cout << "Time object destructor is called ";
printStandard();
cout << "t";
printUniversal();
cout << "n";
} // end Time destructor
6. // Time.h
// Time class containing a constructor with default arguments.
// Member functions defined in Time.cpp.
// prevent multiple inclusions of header
#ifndef TIME_H
#define TIME_H
// Time class definition
class Time
{
public:
explicit Time(int = 0, int = 0, int = 0); // default constructor
~Time(); // destructor
// 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
unsigned int getHour() const; // return hour
unsigned int getMinute() const; // return minute
unsigned int getSecond() const; // return second
void printUniversal() const; // output time in universal-time format
void printStandard() const; // output time in standard-time format
private:
unsigned int hour; // 0 - 23 (24-hour clock format)
unsigned int minute; // 0 - 59
unsigned int second; // 0 - 59
}; // end class Time
#endif
// CISP400V10A4.cpp
#include <iostream>
using std::cout;
using std::endl;
#include "Time.h" // include Time class definition
#include "Date.h" // include Date class definition
const int MAX_TICKS = 30000;
int main()
{
Time t(23, 59, 58);// time object
Date d(12, 31, 2017, t); // date object
// output Time object t's values
for ( int ticks = 1; ticks < MAX_TICKS; ++ticks )
7. {
d.print(); // invokes print
cout << endl;
d.tick(); // invokes function tick
} // end for
d.~Date();// call Date destructor
system("PAUSE");
return 0;
} // end main