6. Complete the following tasks: a. Create a DateException that descends from th
ID: 3716347 • Letter: 6
Question
6. Complete the following tasks: a. Create a DateException that descends from the built-in runtime error exception class. Include fields that hold a month, day, and year and include get functions for each of the values. b. Create a Date class. Data fields include month, day, and year. Include an exception function that prompts for and accepts values for each of the data fields, and that throws a DateException if the month is less than 1 or more than 12 and if the day is out of range for the entered month. (Assume February can have 29 days.) Allow any value to be accepted for the year. Also include an overloaded insertion operator to display a Date's data values. c. Write a main () function that instantiates at least five Date objects and accepts data for each. When you run the program, provide data that tests that each type of exception is being recognized. The main () function should catch the exceptions and display an appro- priate error message about each including what the entered value was. If an exception is caught, the data for the Date object should be set to the default date 1/1/2010. Save the file as DateExceptions.cpp.Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
#include<string.h>
Const int DAY_MONTH = {31,28,31,30,31,30,31,31,30,31,30,31};
class DateException : public runtime_error
{
private : int year;
int month;
int day;
public : DateException(int year1, int mon, int day1, const string& msg = " ") : runtime_error(msg)
{
year = year1;
month = mon;
day = day1;
}
int getYear()
{
return year;
}
int getMonth()
{
return month;
}
int getDay()
{
return day;
}
};
class Date
{
private : int year;
int month;
int day;
public : Date()
{
year =0;
month =0;
day = 0;
}
void input()
{
bool leaporNot = false;
cout<<"ENter year : ";
cin>> year;
if(year < 1)
{
throw DateException(year, month, day, "Error caught! Year must be greater than 0);
}
}
if(year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)
leaporNot = true;
else
leaporNot = false;
cout<< "Enter month";
cin>>month;
if (month == 2 && leaporNot)
{
if(day < 0 || day > 29)
{
throw DateException( year, month, day, "Error! Day is out of range");
}
else
{
if (day < 0 || day > DAY_MONTH [ month-1]
{
throw DateException( year,month, day, "Error ! Day is out of range!");
}
}
void SetDate()
{
year = 2010;
month = 1;
day = 1;
}
friend ostream &operator << (ostream& stream, const Date& , ob)
{
stream<<"................................ "<<endl;
stream<<ob.day<<" " <<ob.month<< "/" <<ob,year<<endl<<endl;
stream<<"........................ "<endl;
return stream;
}
};
int main()
{
Date d[5];
for(int i=0; i<5; i++)
{
try
{
date[i].input();
}
catch(Exception& e)
{
cout<<"Exception caught.."<<endl;
cout<<"e.what()<<endl;
cout<<"Date will set by default"<<endl;
d[i].setDate();
}
}
cout<<"Date : "<<endl;
for( int i=0;i<5;i++)
{
cout<<d[i];
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.