#include #include #include //for the functions time and localtime using namespace std; class date { private: //Members are private by default, so we don't need this label. //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[]; //declaration for static data member public: //Declarations for member functions: date(int init_month, int init_day, int init_year); //constructor date(const date& another); //the "copy constructor" void print() const; //const member function void next(int n); //non-const member function void next(); }; int main() { try { //Output the date of Independence Day. 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}; //Call the "copy constructor". tomorrow.next(); //Advance one day. tomorrow.print(); cout << " is tomorrow.\n"; //Output the date that is one week from today. date nextWeek {today}; //Call the "copy constructor". nextWeek.next(7); nextWeek.print(); cout << " is next week.\n"; } catch (invalid_argument e) { cerr << e.what() << "\n"; return EXIT_FAILURE; } return EXIT_SUCCESS; } const int date::length[] { //definition of static data member 0, //dummy, so that January will have subscript 1 31, //January 28, //February. We'll do leap years later in the course. 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 init_month, int init_day, int init_year) { year = init_year; if (init_month < 1 || init_month > 12) { throw invalid_argument("bad month"); } month = init_month; if (init_day < 1 || init_day > length[month]) { throw invalid_argument("bad day of the month"); } day = init_day; } date::date(const date& another) //the "copy constructor" { year = another.year; month = another.month; day = another.day; } void date::print() const //This member function can't change the date object. { cout << month << "/" << day << "/" << year; } void date::next(int n) //This member function 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 object 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; } } }