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

Develop in C++ a class date to represent a calendar. The class should provide th

ID: 3791990 • 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.

Explanation / Answer

#include<iostream>
#include<sstream>
using namespace std;

char months[12][10]={"January", "February", "March","April","May","June","July","August","September","October","November","December"};
class Date{

int dd,mm,yy;

public :

Date(){
   dd=1;mm=1;yy=1900;
}

Date(int m,int d,int y){

   dd=d;mm=m;yy=y;
   bool check= checkdate(); //returns whether the give date is valid or not
   if(!check){
       cout<<"Invalid date give. Resetting to default"<<endl;
       dd=mm=1; yy=1900;
   }

}

bool checkdate(){
   bool leap=checkleapyear();
   if(mm>12 || mm <=0) //check months
       return false;

    //check date range is valid for particular months
   if((mm==1||mm==3||mm==5||mm==7||mm==8||mm==10||mm==12) && dd>31)
       return false;
   else if(leap && mm==2 && dd>29)
       return false;
   else if(mm==2 && dd>28)
       return false;
   else if(dd>30) //for rest of the months
       return false;
return true;  
}

bool checkleapyear(){
   if((yy%400==0) || (yy%4==0 && yy%100!=0 ))
       return true;
   else
       return false;
}
  
string toString(){


stringstream ss;

//used stringstream to convert date to string. Here we are appending the date info into stream
ss<<months[mm-1]<<" "<<dd<<","<<yy;


//Finally we will convert the stream to string and return it
return ss.str();


}

Date nextDate(){
   int y,d,m;

   if(mm==2){
       bool flag=checkleapyear();
       if(flag){
           if(dd>=29){
           d=1;
           m=3;
           }
           else
               d++;
       }
       else{
           if(dd>=28){d=1;m=3;}
           else
               d++;
       }

       y=yy;
       return Date(m,d,y);
   }

   if((mm==1||mm==3||mm==5||mm==7||mm==8||mm==10) && dd>31)
   {
       m++;
       d=1;
       y=yy;
   }
   else if(mm==12){m=1;y=yy++;d=1;}
   else if(dd>30){
       m++;
       d=1;
       y=yy;
   }
   else{
       d=dd+1;
       m=mm;
       y=yy;
   }

   return Date(m,d,y);
  
}

void compareDates(Date &d)
{
   if(d.yy>this->yy)
       cout<<"The first date comes before the second date"<<endl;
   else if(d.yy<this->yy)
       cout<<"The second date comes before the first date"<<endl;
   else if(d.yy == this->yy){
       if(d.mm>this->mm)
           cout<<"The first date comes before the second date"<<endl;
       else if(d.mm<this->mm)
           cout<<"The second date comes before the first date"<<endl;
       else{
          
           if(d.dd>this->dd)
               cout<<"The first date comes before the second date"<<endl;
           else if(d.dd<this->dd)
               cout<<"The second date comes before the first date"<<endl;
           else{
               cout<<"The two dates are equal"<<endl;
           }
       }
      
   }
}

};

int main(void)
{
int m,d,y;

cout<<"Enter first date in mm-dd-yyyy format:"<<endl;
cin>>m>>d>>y;

Date d1(m,d,y);

cout<<"The version of date is :"<<d1.toString()<<endl;

cout<<"Enter second date in mm-dd-yyyy format:"<<endl;
cin>>m>>d>>y;

Date d2(m,d,y);

d1.compareDates(d2);

return 0;

}

Output:

Enter first date in mm-dd-yyyy format:
18 08 1988
Invalid date give. Resetting to default
The version of date is :January 1,1900
Enter second date in mm-dd-yyyy format:
8 18 1988
The first date comes before the second date