Develop in C++ a class date to represent a calendar. The class should provide th
ID: 3794623 • 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.
Another run:
***Need a header file and implementation file***
Explanation / Answer
Date.h
#ifndef DATE_H
#define DATE_H
#include<fstream>
#include<string>
using namespace std;
class Date
{
int day;
int month;
int year;
public:
Date();
Date(int m, int d, int y);
string toString();
Date nextDate();
int compareDates(Date d);
};
#endif
Date.cpp
#include"Date.h"
#include<iostream>
#include<sstream>
using namespace std;
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 mm, int dd, int yy)
{
month = mm;
day = dd;
year = yy;
}
string Date::toString()
{
stringstream s;
s<<m[month-1]<<" "<<day<<","<<year; //converts date to stream
return s.str(); //converts stream to string and retuns
}
Date Date::nextDate()
{
int nxt_dd, nxt_mm, nxt_yy;
if ((month==2)&&(day==29)&&(year%4== 0))
{
nxt_dd=1;
nxt_mm=3;
nxt_yy=year;
}
else if((month==2)&&(day==28)){
nxt_dd=1;
nxt_mm=3;
nxt_yy=year;
}
else if((month==2)&&(day<28)){
nxt_dd=day+1;
nxt_mm=2;
nxt_yy=year;
}
else if((month==1||month==3||month==5||month==7||month==8||month==10) && (day==31))
{
nxt_mm=month+1;
nxt_dd=1;
nxt_yy=year;
}
else if((month==1||month==3||month==5||month==7||month==8||month==10) && (day<31))
{
nxt_mm=month;
nxt_dd=day+1;
nxt_yy=year;
}
else if((month==4||month==6||month==9||month==11) && (day==30))
{
nxt_mm=month+1;
nxt_dd=1;
nxt_yy=year;
}
else if((month==4||month==6||month==9||month==11) && (day<30))
{
nxt_mm=month;
nxt_dd=day+1;
nxt_yy=year;
}
else if((month==12) && (day==31))
{
nxt_mm=1;
nxt_dd=1;
nxt_yy=year;
}
else if((month==12) && (day<31))
{
nxt_mm=month;
nxt_dd=day+1;
nxt_yy=year+1;
}
return Date(nxt_mm,nxt_dd,nxt_yy);
}
int Date::compareDates(Date d)
{
if (d.year > year)
return -1;
if (d.year == year)
{
if (d.month > month)
return -1;
else if (d.month < month)
return 1;
else
{
if (d.day > day)
return -1;
else if (d.day < day)
return 1;
else
return 0;
}
}
}
main.cpp
#include"Date.h"
#include<iostream>
using namespace std;
int main(void)
{
Date date1,date2, nxt_date;
int d1,m1,y1,m2,d2,y2,i;
char t;
string ds;
cout << "Enter the first date using the format mm-dd-yyyy: ";
cin >> m1 >> t >> d1 >> t >> y1;
date1 = Date(m1,d1,y1);
cout << "Enter the second date using the format mm-dd-yyyy: ";
cin >> m2 >> t >> d2 >> t >> y2;
date2 = Date(m2,d2,y2);
ds = date1.toString();
cout << "The string version of the date is: " << ds << endl;
nxt_date = date1.nextDate();
cout << "The next date is: " << (nxt_date.toString()) << endl;
i = date1.compareDates(date2);
if(i==1)
cout << "Second date comes before first date" <<endl;
else if(i==-1)
cout << "First date comes before second date" <<endl;
else
cout << "First date is equal to Second date" <<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.