#include #include #include "date.h" using namespace std; const int date::length[] = { 0, //dummy 31, //january 29, //february 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 < january || m > december) { 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 the current date. { const time_t t = time(0); if (t == static_cast(-1)) { cerr << "time failed\n"; exit(EXIT_FAILURE); } 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 > december) { month = january; ++year; } } return *this; } date& date::operator--() { if (--day < 1) { if (--month < january) { month = december; --year; } day = length[month]; } return *this; }