Question
this is the code in C++ i want to let the user be able to enter new date and whenever he enters month more than
this is the code in C++ i want to let the user be able to enter new date and whenever he enters month more than 12 like 13 or zero or negative it will default to 1 and display the new date result #include using namespace std; class Date { private: int month, day, year; public: // Constructor with three parameters Date(int m, int d, int y) { month = m; day = d; year = y; // Ensure that the month value is in the range 1-12 if (month < 1 || month > 12) { month = 1; //if the date is set above 12, it will automatically set it to 1 } } // Set function for month data member void setMonth(int m) { month = m; } // Get function for month data member int getMonth() { return month; } // Set function for day data member void setDay(int d) { day = d; } // Get function for day data member int getDay() { return day; } // Set function for year data member void setYear(int y) { year = y; } // Get function for year data member int getYear() { return year; } // Member function to display the date in the format: month/day/year void displayDate() { cout << month << "/" << day << "/" << year << endl; //formatting the output using "/" } }; int main() { // Create a Date object with the constructor and initialize the data members Date today(2, 2, 2023); // Display the date cout << "Today's date is: "; today.displayDate(); // Set a new month and display the date again today.setMonth(5); //setting the month as 5 cout << "Today's date after setting the month is: "; //printing again the output today.displayDate(); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started