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

IN c++ Using Namespace std Only bold question needs answered Design and implemen

ID: 3668344 • Letter: I

Question

IN c++

Using Namespace std

Only bold question needs answered

Design and implement a class Date that implements the date in a program. The class has three integers’ data fields: day, month, and year. The program should be able to perform the following operations on an object of type Date:

Set the day, month and year.
Get the day, month and year.
Print the date object by overloading the operator <<.
Write a function that returns the next day- Date next_Day(Date). For example, if today’s date is 31/12/2016, your next_Day function should return 1/1/2017.

Note that January, March, May, July, August, October, and December are 31 days the others are 30. Assume that February is 30 days for simplicity. Also remember that we have 12 months in a year.

Add the appropriate constructors.
Add to your class an exception handler (try/catch/throw) that throws an error message (e.g. “value out of range”) if any of the data fields is negative or 0. Also, an exception should be thrown if the month is greater than 13 or a day is greater than 31. Make sure to include the exception handler in the appropriate member functions.
Implement your class and test all member functions.

Explanation / Answer

#include<iostream>

#define TRUE 1
#define FALSE 0
//defining values for TRUE,FALSE variables
using namespace std;

class Nextdate{
int day;
int month;
int year;
public:
//declaring functions
void readdate();
int isLeapYear();
string month2string();
int exceedsDaysInMonth();
void tommorow();
};

// Input date from user and returns nothing
void Nextdate::readdate(){
cout << "Enter day (dd - 16) : ";
cin >> day;
cout << "Enter Month (mm - 02 ) : ";
cin >> month;
cout << "Enter year (yyyy - 2016 ) : ";
cin >> year;
}
// make sure month days are correct
// return TRUE if days exceeds in a month
int Nextdate::exceedsDaysInMonth(){
int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
if ( month < 1 || month > 12 || day > days[month-1])
return TRUE;
else
return FALSE;
}
// convert numeric month into string
string Nextdate::month2string(){
char *months[] = ""Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"";
if ( exceedsDaysInMonth() ) {
if ( ! (month < 1 || month > 12) )
return months[month-1];
else
return "Unknown month";
}
else
return "Unknown month";
}
// return TRUE if a year is leap
int Nextdate::isLeapYear(){
if ((year % 4) != 0 ){
return FALSE;
}
else if ( (year % 400) != 0 ){
return TRUE;
}
else if ( (year % 100) == 0 ){
return FALSE;
}
else
{
return FALSE;
}
}
// validate and calculate tommorows date
void Nextdate::tommorow(){
int td, tm, ty;
int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
//Array contains last dates of respective 12 months in serious.e jan,feb..
if ( year < 0 ){
cerr << year << " is not a valid year" << endl;
exit(1);
}
if ( exceedsDaysInMonth() ){
if ( month == 2 && day == 29 ){
if ( ! isLeapYear() ){
cerr << year << " is not a leap year, so Feb doesn't have 29 days" << endl;
exit(1);
}
}
else
{
cerr << "Bad day value month - " << month << " (" << month2string();
cerr << ") doesn't have " << day << " days "<< endl;
exit(1);
}
}
// calculate tomorrow date initializing day, month and year
td=day;
tm=month;
ty=year;
td++;
if ( td > days[month-1]){
td=1;
tm++;
if ( tm > 12 )
{ ty++; tm=1; }
}
cout << "Valid date, tomorrow date is " << td << "/" << tm << "/" << ty << endl;
}

// main
int main(){
Nextdate date;
date.readdate();
date.tommorow();
return 0;
}