#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 2 valid values into a newborn date object, //or it throws an exception. date::date(int init_month, int init_day, int init_year) : year {init_year}, day {init_day} { 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 to day the sum of 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)}; year = p->tm_year + 1900; day = p->tm_yday + 1; //in the range 1 to 365 inclusive } void date::print() const { int d {day}; //d is probably too big, ... int m {1}; for (; d > length[m]; ++m) { //... so cut d down to size. d -= length[m]; } cout << m << "/" << d << "/" << year; } void date::next(int n) //This mmeber function can change the date object. { for (int i {0}; i < n; ++i) { next(); //Call the other next function, the one with no argument } } void date::next() //Move this date one day into the future. { if (day < 365) { ++day; } else { day = 1; //Advance into the next year. ++year; } } void date::prev(int n) { for (int i {0}; i < n; ++i) { prev(); } } void date::prev() //Move this date object one day into the past. { if (day > 1) { --day; } else { day = 365; //Retreat into the previous year. --year; } }