#include #include #include //for the functions time and localtime #include 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 the 13) public: //private by default //Declarations for member and friend functions: date(int m, int d, int y); // constructor date(); // constructor (no arguments) void print() const; // const member function void next(int n); // non-const member function void next(); int dayOfYear() const; // returns an int indicating what day of the year it is friend int distance(const date& d1, const date& d2); // a friend function }; int main() { try { const date independenceDay {7, 4, 1776}; //month, day, year independenceDay.print(); cout << " is Independence Day.\n"; //Ask the operating system for the current date and time. const time_t t {time(nullptr)}; const tm *const p {localtime(&t)}; //Output today's date. const date today {p->tm_mon + 1, p->tm_mday, p->tm_year + 1900}; today.print(); cout << " is today.\n"; //Output tomorrow's date. date tomorrow {today}; tomorrow.next(); //Advance one day. tomorrow.print(); cout << " is tomorrow.\n"; // *** Week 4 HW additions are the remaining portion of this try{} block *** //Output the date of next Christmas, 321 days from today (Feb 6 2025). date christmas {today}; christmas.next(321); christmas.print(); cout << " is next Christmas.\n"; const date d; //Call the new constructor with no explicit arguments. (I named this date object d to avoid // a conflict with the time_t variable named t earlier in the code). cout << "Today is "; d.print(); //Make sure that t contains today's date. cout << ".\n"; 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. const date summer {6, 20, 2025}; // first day of summer const int dist {distance(summer, d)}; // d is the date object representing today created a few lines above 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, //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 3 valid values into a newborn date object, //or it throws an exception. 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; } // constructor with no arguments // initializes a date object to today's date date::date() { //Ask the operating system for the current date and time. const time_t t {time(nullptr)}; const tm *const p {localtime(&t)}; month = p->tm_mon + 1; day = p->tm_mday; year = p->tm_year + 1900; } void date::print() const //Can't change the date object. { cout << month << "/" << day << "/" << year; } void date::next(int n) //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 < length[month]) { ++day; } else { day = 1; //Advance into the next month. if (month < 12) { ++month; } else { month = 1; //Advance into the next year. ++year; } } } // returns an int indicating what day of the year it is int date::dayOfYear() const { int day_of_year {0}; for (int i {0}; i < month; i++) { day_of_year += length[i]; } day_of_year += day; return day_of_year; } // returns the distance measured in days between two date objects int distance(const date& d1, const date& d2) { return d1.dayOfYear() - d2.dayOfYear(); }