Modify the class date to include exception handling. The code should throw diffe
ID: 3884993 • Letter: M
Question
Modify the class date to include exception handling. The code should throw different exception types based on the initializer values for the data members. The inner classes you write inside the date class, must provide additional information to the catch statements using the member variables those classes.
#include <iostream>
#include <string>
using namespace std;
int validdate(int,int,int);
class Date
{
private: int month,day,year;
public:
Date(int m, int d, int y)
{int max=28;
bool good=true;
if (d<= 0)
good=false;;
switch(m)
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10:
case 12: if (d > 31)
good=false;
break;
case 4 :
case 6 :
case 9 :
case 11: if (d > 30)
good=false;
break;
case 2 : if((y%4==0&&y%100!=0)|| y%400==0)
max=29;
if (d >max) good=false;
}
if(good)
{
month=m;
day=d;
year=y;
}
else
{cout<<"invalid date-default date 1-1-2000 used ";
month=1;
day=d;
year=y;
}
}
void Date::nextDay()
{int max=28;
switch(month)
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10:
case 12: max=31;
break;
case 4 :
case 6 :
case 9 :
case 11: max=30;
break;
case 2 : if((year%4==0&&year%100!=0)|| year%400==0)
max=29;
}
day++;
if(day>max)
{month++;
day=1;
if(month>12)
{year++;
month=1;
}
}
}
void Date::printday()
{string mth[]={"January","February","March","April","May","June","July","August",
"September","October","November","December"};
cout<<day<<" "<<mth[month-1]<<" "<<year<<endl;
}
int Date::getmonth()
{
return month;
}
int getday()
{
return day;
}
int Date::getyear()
{
return year;
}
void Date::setmonth(int m)
{month=m;}
void Date::setday(int d)
{day=d;
}
void Date::setyear(int y)
{year=y;
}
};
int main()
{
int m,d,y,i;
cout<<"Enter month: ";
cin>>m;
cout<<"Enter day: ";
cin>>d;
cout<<"Enter year: ";
cin>>y;
cout<<endl;
Date dd(m,d,y);
dd.printday();
for(i=0;i<100;i++)
{dd.nextDay();
dd.printday();
}
system("pause");
return 0;
}
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
int validdate(int,int,int);
class Date
{
private: int month,day,year;
public:
Date(int m, int d, int y)
{int max=28;
bool good=true;
if (d<= 0)
good=false;;
switch(m)
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10:
case 12: if (d > 31)
good=false;
break;
case 4 :
case 6 :
case 9 :
case 11: if (d > 30)
good=false;
break;
case 2 : if((y%4==0&&y%100!=0)|| y%400==0)
max=29;
if (d >max) good=false;
}
if(good)
{
month=m;
day=d;
year=y;
}
else
{cout<<"invalid date-default date 1-1-2000 used ";
month=1;
day=d;
year=y;
}
}
void Date::nextDay()
{int max=28;
switch(month)
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10:
case 12: max=31;
break;
case 4 :
case 6 :
case 9 :
case 11: max=30;
break;
case 2 : if((year%4==0&&year%100!=0)|| year%400==0)
max=29;
}
day++;
if(day>max)
{month++;
day=1;
if(month>12)
{year++;
month=1;
}
}
}
void Date::printday()
{string mth[]={"January","February","March","April","May","June","July","August",
"September","October","November","December"};
cout<<day<<" "<<mth[month-1]<<" "<<year<<endl;
}
int Date::getmonth()
{
return month;
}
int getday()
{
return day;
}
int Date::getyear()
{
return year;
}
void Date::setmonth(int m)
{month=m;}
void Date::setday(int d)
{day=d;
}
void Date::setyear(int y)
{year=y;
}
};
int main()
{
int m,d,y,i;
cout<<"Enter month: ";
cin>>m;
cout<<"Enter day: ";
cin>>d;
cout<<"Enter year: ";
cin>>y;
cout<<endl;
Date dd(m,d,y);
dd.printday();
for(i=0;i<100;i++)
{dd.nextDay();
dd.printday();
}
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.