Develop in C++ a class date to represent a calendar. The class should provide th
ID: 3792606 • Letter: D
Question
Develop in C++ a class date to represent a calendar. The class should provide the following operations:
• A default constructor that initializes a date object to 01-01-1900.
• A class constructor that initializes a date object to a correct value using three integer parameters corresponding to the desired month, day and year.
• The function toString() that returns the string version of a date object. For example, applying toString() to the date 12-01-2000 produces "December 1st, 2000".
• The function nextDate() that returns the successive date i.e. the new value of the date object. For example, applying nextDate() to the date 12-31-2000 produces a new date: 01-01-2001. You should take into account if the year is a leap year or not. A leap year is: (1) divisible by 400 or (2) divisible by 4 and not divisible by 100.
• The function compareDates() that checks if the date of interest is before, after or equal to the argument date.
A simple run of the driver program follows.
Enter the first date using the format mm-dd-yyyy: 12-32-2000 Incorrect day!
Enter the first date using the format mm-dd-yyyy: 12-31-2000
The string version of the date is: December 31st, 2000 The next date in string version is: January 1st, 2001
Enter the second date using the format mm-dd-yyyy: 12-01-2001
The first date comes before the second one.
Another run: Enter the first date using the format mm-dd-yyyy: 02-28-2005
The string version of the date is: February 28th, 2005
The next date in string version is: March 1st, 2005
Enter the second date using the format mm-dd-yyyy: 01-10-2005
The first date comes after the second one.
-
-
-
Need a header file and implementation file
Explanation / Answer
//date.h
#include<fstream>
#include<string>
using namespace std;
class Date
{
int day;
int month;
int year;
int check_date(int m, int d, int y);
public:
Date();
Date(int m, int d, int y);
string toString(Date d);
Date nextDate();
int compareDates(Date d);
};
----------------------------------
//Date.cpp
#include"Feb_15_Date.h"
char m[12][12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
Date::Date()
{
day = 1;
month = 1;
year = 1900;
}
Date::Date(int m, int d, int y)
{
if (check_date(m, d, y) >= 0)
{
day = d;
month = m;
year = y;
}
else
return;
}
string Date::toString(Date d)
{
string s;
if (check_date(d.month, d.day, d.year) < 0)
return "";
s += m[d.month-1];
s += " ";
s += to_string(d.day);
s += "th, " + to_string(d.year);
return s;
}
Date Date::nextDate()
{
Date d;
int td, tm, ty;
int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (year < 0){
cerr << year << " is not a valid year" << endl;
exit(1);
}
if (month < 1 || month > 12 || day > days[month - 1]){
if (month == 2 && day == 29){
if (!(year%4)== 0){
cerr << year << " is not a leap year, so Feb doesn't have 29 days" << endl;
exit(1);
}
}
else
{
cerr << "Invalid month!! " << endl;
exit(1);
}
}
// calculate tommorow
td = day;
tm = month;
ty = year;
td++;
if (td > days[month - 1]){
td = 1;
tm++;
if (tm > 12)
{
ty++; tm = 1;
}
}
d.day = td;
d.month = tm;
d.year = ty;
return d;
}
int Date::compareDates(Date d)
{
if (d.year > year)
return -1;
if (d.year == year)
{
if (d.month > month)
return -1;
if (d.month < month)
{
return 1;
}
else //months are equal
{
//check days
if (d.day > day)
{
return -1;
}
else if (d.day < day)
return 1;
else
return 0;
}
}
return 0;
}
int Date::check_date(int m, int d, int y)
{
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
{
//if day is correct return 0 else return -1
if (d >= 0 && d <= 31)
return 0;
else
{
cout << " Incorrect day!" << endl;
return -1;
}
}
//check if day is less than 30 for particular months
if (m == 4 || m == 6 || m == 9 || m == 11)
{
if (d <= 30)
return 0;
else
{
cout << " Incorrect day!" << endl;
return -1;
}
}
//check if day is less than 29 for leap year for feb month otherwise 28 for feb
if ((y % 4) == 0)
{
if (d <= 29)
return 0;
else
{
cout << " Incorrect day!" << endl;
return -1;
}
}
else
{
if (d <= 28)
return 0;
else
{
cout << " Incorrect day!" << endl;
return -1;
}
}
//check month
/*check if month is greater than 0 and less than equal to 12*/
if (m > 0 && m <= 12)
return 0;
else
{
cout << " Incorrect month!" << endl;
return -1;
}
//check year
/*if (m == feb && d == 29 && !leapyear(y + n)) { // beware of leap years!
m = (Month)m; // use March 1 instead of February 29
d = 1;
}*/
}
------------------------------------
//main.cpp
#include"Feb_15_Date.h"
using namespace std;
int main()
{
Date d1;
int d,m,y;
char t;
cout << "Enter the first date using the format mm-dd-yyyy: ";
cin >> m >> t >> d >> t >> y;
Date d2(m,d, y);
cout << "The string version of the date is: " << d2.toString(d1);
cout << "The next date in string version is: " << d2.toString(d2.nextDate()) << endl;
}
--------------------------------------------------------------------
//output
Enter the first date using the format mm-dd-yyyy: 12-32-2000
Incorrect day!
Enter the first date using the format mm-dd-yyyy: 2-28-2005
The string version of the date is: February 28th, 2005
Enter the first date using the format mm-dd-yyyy: 2-28-2005
The string version of the date is: January 1th, 1900The next date in string vers
ion is: March 1th, 2005
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.