#include //for the object cout #include //for the functions time and localtime #include "date.h" using namespace std; //because invalid_argument and cout belong to namespace std const int date::length[] { 0, //dummy, so that January will have subscript 1 31, //January 28, //February. Pretend there are no leap years. 31, //March 30, //April 31, //May 30, //June 31, //July 31, //August 30, //September 31, //October 30, //November 31 //December }; //The constructor installs 1 valid value into a newborn date object, //or it throws an exception. date::date(int init_month, int init_day, int init_year) : day {365 * init_year + init_day - 1} { if (init_month < 1 || init_month > 12) { throw invalid_argument("bad month"); } if (init_day < 1 || init_day > length[init_month]) { throw invalid_argument("bad day of the month"); } //Add on all the days in all the months before month m. for (int month {1}; month < init_month; ++month) { day += length[month]; } } date::date() //Default constructor puts today's date into the newborn object. { const time_t t {time(nullptr)}; const tm *const p {localtime(&t)}; day = 365 * (p->tm_year + 1900) + p->tm_yday; } void date::print() const //Can't change the date object. { const int year {day / 365}; int dayOfYear {day % 365 + 1}; //in range 1 to 365 inclusive int m {1}; for (; dayOfYear > length[m]; ++m) { dayOfYear -= length[m]; } cout << m << "/" << dayOfYear << "/" << year; } void date::next(int n) //Move this date n days into the future. { day += n; //means day = day + n } void date::next() //Move this date one day into the future. { ++day; } void date::prev(int n) { day -= n; //means day = day - n } void date::prev() //Move this date one day into the past. { --day; }