Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN C++ Design a class called Date that has integer data members to store month,

ID: 3807723 • Letter: I

Question

IN C++

Design a class called Date that has integer data members to store month, day, and year. The class should have a three-parameter (day-of-month, month, year) default constructor that allows the date to be set at the time a new Date object is created. If the user creates a Date object without passing any arguments , or if any of the values passed are invalid, the default  values of 1, 1, 2001 (i.e., January 1, 2001) should be used (see below for the definition of invalid values ). The class should have member functions to print the date in the following formats:

3/15/13 (printNumerical)

March 15, 2013 (printMonthFirst)

15 March 2013 (printDateFirst)

The following are invalid values :

For day of month: any value  less than 1 or greater than 31 (so February 30 or April 31 would be acceptable)

For month: any value  less than 1 or greater than 12

For year: any value  less than 0

Then,

Write a program that uses the Date class just defined. The program prompts "date information: " and then reads three integers : the day of month. the month and the year. The program then prints the date three times, each time on a line by itself. The first time the date is in numerical format, the second in month-first format, and the third time in date first format.

Here is one sample run:

Explanation / Answer

#include <iostream>

using namespace std;
class Date {
private:
int day, month, year;
public:
Date() {
day = 1;
month = 1;
year = 2001;
}
Date(int d, int m, int y){
day = d;
month = m;
year = y;
}
void printNumerical() {
cout<<day<<"/"<<month<<"/"<<year<<endl;
}
void printMonthFirst() {
cout<<getMonth(month)<<" "<<day<<", "<<year<<endl;
}
void printDateFirst() {
cout<<day<<" "<<getMonth(month)<<" "<<year<<endl;
}
string getMonth(int month){
       if(month == 1){
           return "January";
       }
       else if(month == 2){
           return "February";
       }
       else if(month == 3){
           return "March";
       }
       else if(month == 4){
           return "April";
       }
       else if(month == 5){
           return "May";
       }
       else if(month == 6){
           return "June";
       }
       else if(month == 7){
           return "July";
       }
       else if(month == 8){
           return "August";
       }
       else if(month == 9){
           return "September";
       }
       else if(month == 10){
           return "October";
       }
       else if(month == 11){
           return "November";
       }
       else if(month == 12){
           return "December";
       }
}
};
int main()
{
int day, month, year;
  
while(day < 1 || day > 31) {
cout<<"Enter the day: ";
cin >> day;
}
while(month < 1 || month > 31) {
cout<<"Enter the month: ";
cin >> month;
}
cout<<"Enter the year: ";
cin >> year;

Date d1;
d1.printNumerical();
d1.printMonthFirst();
d1.printDateFirst();
Date d2(day, month, year);
d2.printNumerical();
d2.printMonthFirst();
d2.printDateFirst();


return 0;
}

Output:

sh-4.2$ g++ -std=c++11 -o main *.cpp                                                                                                                                                                                                                                     

sh-4.2$ main                                                                                                                                                                                                                                                             

Enter the day: 15                                                                                                                                                                                                                                                        

Enter the month: 3                                                                                                                                                                                                                                                       

Enter the year: 2014                                                                                                                                                                                                                                                     

1/1/2001                                                                                                                                                                                                                                                                 

January 1, 2001                                                                                                                                                                                                                                                          

1 January 2001                                                                                                                                                                                                                                                           

15/3/2014                                                                                                                                                                                                                                                                

March 15, 2014                                                                                                                                                                                                                                                           

15 March 2014