#include #include #include using namespace std; class date { int year; int month; int day; static const int length[13]; public: date(int m, int d, int y); date(); void print() const; void next(int n); void next(); int dayOfYear() const; friend int distance(const date& d1, const date& d2); }; int main() { try { const date independenceDay {7, 4, 1776}; independenceDay.print(); cout << " is Independence Day.\n"; const date t; cout << "Today is "; t.print(); cout << ".\n"; const time_t now {time(nullptr)}; const tm *const p {localtime(&now)}; const date today {p->tm_mon + 1, p->tm_mday, p->tm_year + 1900}; today.print(); cout << " is today.\n"; const date xmas {12, 25, 2025}; cout << "Christmas is day number " << xmas.dayOfYear() << " of the year.\n"; const date summer {6, 20, 2025}; const int dist {distance(summer, t)}; cout << "Only " << dist << " days till summer.\n"; } catch (invalid_argument& e) { cerr << e.what() << "\n"; return EXIT_FAILURE; } return EXIT_SUCCESS; } const int date::length[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; date::date(int m, int d, int y) { year = y; if (m < 1 || m > 12) throw invalid_argument("bad month"); month = m; if (d < 1 || d > length[month]) throw invalid_argument("bad day of the month"); day = d; } date::date() { const time_t now = time(nullptr); const tm *const p = localtime(&now); month = p->tm_mon + 1; day = p->tm_mday; year = p->tm_year + 1900; } void date::print() const { cout << month << "/" << day << "/" << year; } void date::next(int n) { for (int i = 0; i < n; ++i) { next(); } } void date::next() { if (day < length[month]) { ++day; } else { day = 1; if (month < 12) { ++month; } else { month = 1; ++year; } } } int date::dayOfYear() const { int sum = day; for (int i = 1; i < month; ++i) { sum += length[i]; } return sum; } int distance(const date& d1, const date& d2) { const int days1 = d1.dayOfYear() + (d1.year * 365); const int days2 = d2.dayOfYear() + (d2.year * 365); return days1 - days2; }