Write your answer in the space provided or on a separate sheet of paper. Date Co
ID: 3843283 • Letter: W
Question
Write your answer in the space provided or on a separate sheet of paper. Date Co Design a class called Date. The class should store a date in three integers: month, day, and year. There should be member functions to print the date in the following forms: 12/25/201 December 25, 201 25 December 2014 Demonstrate the class by writing a complete program implementing it Input Validation: Do not accept values for the day greater than 31 or less than 1. D not accept values for the month greater than 12 or less than 1 A-6Explanation / Answer
#include <iostream>
using namespace std;
class Date{
int day,month,year;
public:
void setDate(int d, int m,int y){
day = d;
month = m;
year = y;
}
void printDate(){
char * mon;
cout<<month<<'/'<<day<<'/'<<year<<endl;
switch(month){
case 1:
strcpy(mon,'JANUARY');
break;
case 2:
strcpy(mon,'FEBRUARY');
break;
case 3:
strcpy(mon,'MARCH');
break;
case 4:
strcpy(mon,'APRIL');
break;
case 5:
strcpy(mon,'MAY');
break;
case 6:
strcpy(mon,'JUNE');
break;
case 7:
strcpy(mon,'JULY');
break;
case 8:
strcpy(mon,'AUGUST');
break;
case 9:
strcpy(mon,'SEPTEMBER');
break;
case 10:
strcpy(mon,'OCTOBER');
break;
case 11:
strcpy(mon,'NOVEMBER');
break;
case 12:
strcpy(mon,'DECEMBER');
break;
}
cout<<mon<<" "<<day<<", "<<year<<endl;
cout<<day<<" "<<mon<<" "<<year<<endl;
}
}
int main() {
// your code goes here
int d,m,y;
cout<<"Enter Day : ";
cin>>d;
if(d<1 && d>31)
{
cout<<"Invalid input";
}
cout<<"Enter Month : ";
cin>>m;
if(m<1 && m>12)
{
cout<<"Invalid input";
}
cout<<"Enter Year : ";
cin>>y;
Date date;
date.setDate(d,m,y);
date.printDate();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.