Design a class called Date. The class should store a specific date in three inte
ID: 660946 • Letter: D
Question
Design a class called Date. The class should store a specific date in three integers: month, day, and year. There should be member functions to print the date in the following four forms, if it is valid:
12/25/2012 (US)
December 25, 2012 (US expanded)
25 December 2012 (US Military)
2012-12-25 (International)
There should be member functions to get and/or set value of month, day, and year. There should be member functions to check for invalid date entered, such as 4/31/2012, or 2/29/2011, which will be rejected with an error message, and the program should ask for another date entered. Write a complete C++ program to demonstrate the class. Allow your program to run continuously as long as the user wishes.
Input validation:
Explanation / Answer
#include <iostream>
using namespace std;
class date
{
int month;
int day;
int year;
public:
date (int month = 1, int day = 1, int year = 2001)
{
date::month = month;
date::day = day;
date::year = year;
};
void showDate();
~date(){}
};
void date::showDate(){
cout << month << "/" << day << "/" << year << " (US) " << endl;
cout << year << "/" << month << "/" << day << " (International) " << endl;
}
int main()
{
int month;
int day;
int year;
string monthName[12] = {"January","February","March","April","May","June","July",
"August","September","October","November","December"};
cout << "enter month (between 1 and 12)" << endl;
cin >> month;
if (month > 12 || month < 1)
{
month = 1;
}
cout << "enter day (between 1 and 31)" << endl;
cin >> day;
if (day > 31 || day < 1)
{
day = 1;
}
cout << "enter year (between 1900 and 2015)"<< endl;
cin >> year;
if (year > 2015 || year < 1900)
{
year = 2015;
}
date newDate(month, day, year);
newDate.showDate();
cout << monthName[month-1] << " " << day << ", " << year << " (US Expanded)" << endl;
cout << day << " " << monthName[month-1] << " " << year << " (US Military)" << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.