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

C++ Calandar A default constructor that initializes a date object to 01-01-1900

ID: 3866180 • Letter: C

Question

C++ Calandar

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 integerparameters 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

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.

All of this done in Calandar.h, Calendar.cpp and TestCalendar.cpp

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;

}