#include #include #include //for the functions time and localtime using namespace std; class date { public: //Data members: int year; int month; //in the range 1 to 12 inclusive int day; //in the range 1 to 31 inclusive //Member functions: void print() const; //const member function void next(int n); //non-const member function void next(); void prev(int n); //Go n days into the past. void prev(); //Go 1 day into the past. }; int main() { // take March 1, 2025 back one day with prev() function date march_1_2025 {2025, 3, 1}; date feb_28_2025 {march_1_2025}; feb_28_2025.prev(); feb_28_2025.print(); cout << " is one day before March 1, 2025." << endl; // Take March 1, 2025 back n days with prev(int n) function int n; while (true) { cout << "How many days before March 1, 2025 should we travel? Please enter an integer: "; cin >> n; if (cin.fail()) { // Check if input is valid cin.clear(); // Clear the error flag cin.ignore(numeric_limits::max(), '\n'); // Ignore invalid input cerr << "Invalid input. Please enter an integer." << endl; } else break; // Input is valid; break the loop } date march_1_2025_prev {march_1_2025}; march_1_2025_prev.prev(n); march_1_2025_prev.print(); cout << " is " << n << " days before March 1, 2025." << endl; 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 }; void date::print() const //Can't change the date object. { cout << month << "/" << day << "/" << year; } void date::next(int n) //Can change the date struct. { 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 < date_length[month]) { ++day; } else { day = 1; //Advance into the next month. if (month < 12) { ++month; } else { month = 1; //Advance into the next year. year++; } } } void date::prev(int n) { // Move this date n days into the past for (int i = 0; i < n; i++) { prev(); // call prev() function with no argument n times } } void date::prev() { // Move this date one day into the past if (day > 1) day--; else { if (month > 1) { month--; // update month day = date_length[month]; // update day } else { // month == 1 year--; // update year month = 12; // update month day = date_length[month]; // update day } } }