#include #include #include "date.h" using namespace std; const int date::length[] { 0, //dummy 31, //January 29, //February: assume that every year is leap 31, //March 30, //April 31, //May 30, //June 31, //July 31, //August 30, //September 31, //October 30, //November 31 //December }; void date::install(int m, int d, int y) //called by each constructor { year = y; if (m < 1 || m > 12) { cerr << "bad month " << m << "/" << d << "/" << y << "\n"; exit(EXIT_FAILURE); } month = m; if (d < 1 || d > length[month]) { cerr << "bad day " << m << "/" << d << "/" << y << "\n"; exit(EXIT_FAILURE); } day = d; } date::date() //Initialize to today's date. { const time_t t {time(0)}; const tm *const s {localtime(&t)}; install(s->tm_mon + 1, s->tm_mday, s->tm_year + 1900); } date& date::operator++() { if (++day > length[month]) { day = 1; if (++month > 12) { month = 1; ++year; } } return *this; } date& date::operator--() { if (--day < 1) { if (--month < 1) { month = 12; --year; } day = length[month]; } return *this; }