Question: This is what I have. It must be able to be compiled and run in C++ // The following class represents a date // Write
This is what I have. It must be able to be compiled and run in C++
// The following class represents a date // Write the complete code to print the data in day month name year //Example: 10 February 2020
//write the validations for the date //The months should only allow 1 through 12 to be entered, any other number and should generate an error message //The day should only allow 1 through 31 to be entered, any other number should generate an error message //The year should only allow 2000 through 2021, any other number should generate an error message
//***********************************************************************
#include
class Date { public: void getDate(); void formatShort(); string getMonth(); // To get the month name void formatName(); void formatDayMonthYear(); // To print in the format shown above
private: int month; int day; int year; string monthname; };
//************************************************************************ // Class Implementation
void Date::getDate() { cout << "Enter the month (1-12): "; cin >> month; cout << "Enter the day (1-31): "; cin >> day; cout << "Enter the year: "; cin >> year; }
void Date::formatShort() { cout << month << '/' << day << '/' << year << endl; } //--------------------------------------------------------------------- void Date::formatName() { monthname = getMonth(); cout << monthname << ' ' << day << ", " << year << endl; } //-------------------------------------------------------------------- string Date::getMonth() { switch (month) { case 1: return "January"; break; case 2: return "February"; break; case 3: return "March"; break; case 4: return "April"; break; case 5: return "May"; break; case 6: return "June"; break; case 7: return "July"; break; case 8: return "August"; break; case 9: return "September"; break; case 10: return "October"; break; case 11: return "November"; break; case 12: return "December"; break; default: cout << "Not a valid month number"; } } //-------------------------------------------------------------------- //Write the code for the formatDayMonthYear function
//**********************************************************************
// The main function
#include
int main() { Date hold; hold.getDate(); hold.formatShort(); hold.formatName();
//Write the code to call the formatDayMonthYear function
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
