#include #include #include //for the functions time and localtime using namespace std; class date { // Declarations for data members: int year; int month; // in the range 1 to 12 inclusive int day; // in the range 1 to 31 inclusive static const int length[13]; // Static data member (needs 13 elements) public: // Declarations for member functions: date(int m, int d, int y); // Constructor with 3 arguments date(); // No-argument constructor void print() const; // Const member function void next(int n); // Non-const member function void next(); int dayOfYear() const; // Returns day number of the year // Friend function to calculate distance between two dates friend int distance(const date& d1, const date& d2); }; int main() { try { const date independenceDay {7, 4, 1776}; // Month, day, year independenceDay.print(); cout << " is Independence Day.\n"; // Test the new no-arg constructor const date t; // Calls the new constructor cout << "Today is "; t.print(); cout << ".\n"; // Test the dayOfYear function const date xmas {12, 25, 2025}; // Call constructor with 3 explicit args. cout << "Christmas is day number " << xmas.dayOfYear() << " of the year.\n"; // Make sure it's 359. // Test the distance function const date summer {6, 20, 2025}; // First day of summer 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; } // Initialize static array const int date::length[] { 0, // Dummy, so that January will have subscript 1 31, // January 28, // February (ignoring leap years) 31, // March 30, // April 31, // May 30, // June 31, // July 31, // August 30, // September 31, // October 30, // November 31 // December }; // Constructor with 3 arguments 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; } // No-argument constructor: Initializes to today's date date::date() { time_t t = time(nullptr); tm *p = localtime(&t); year = p->tm_year + 1900; month = p->tm_mon + 1; day = p->tm_mday; } // Prints the date in MM/DD/YYYY format void date::print() const { cout << month << "/" << day << "/" << year; } // Advances the date by n days void date::next(int n) { for (int i {0}; i < n; ++i) { next(); } } // Advances the date by one day void date::next() { if (day < length[month]) { ++day; } else { day = 1; // Advance to the next month if (month < 12) { ++month; } else { month = 1; // Advance to the next year ++year; } } } // Returns the day number of the year (1 to 365) int date::dayOfYear() const { int dayNum = day; for (int i = 1; i < month; ++i) { dayNum += length[i]; } return dayNum; } // Computes the difference in days between two dates int distance(const date& d1, const date& d2) { int yearDiff = d1.year - d2.year; int dayDiff = d1.dayOfYear() - d2.dayOfYear(); return yearDiff * 365 + dayDiff; }