C++ problem 11-2
In this chapter, the class dateType was designed to implementthe date in a program, but the member function setDate and theconstructor do not check whether the date is valid before storingthe date in the member variables. Rewrite the definitions of thefunction setDate and the constructor so that the values for themonth, day, and year are checked before storing the date into themember variables. Add a member function, isLeapYear, to checkwhether a year is a leap year. Moreover, write a test program totest your class.
Input should be format month day year with each separated by aspace.
for dateType.h
//dateType.h
#ifndef date_H
#define date_H
class dateType
{
public:
    void setDate(int month, int day, intyear);
      int getDay() const;
      int getMonth() const;
    int getYear() const;
    void printDate() const;
    bool isLeapYear();
dateType(int month = 1, int day = 1, int year = 1900);
private:
int dMonth;Â Â Â Â Â Â
    intdDay;       Â
    intdYear;      Â
};
#endif
---------------------------------------
For dateTypeImp.cpp
#include\"dateType.h\"
class dateType{
   private:
   int month;
   int day;
   int year;
 Â
    public:
  Â
   dateType(){
   }
  Â
   dateType(int month,int day,int year){
       intmaxNumberOfDays[]={31,28,31,30,31,30,31,31,30,31,30,31};
       if(month < 1 ||month > 12){
           cout<<\"Date:\"<           cout<<\"Month is not valid\"<       }
       if(day >= 1&& day <= maxNumberOfDays[month]){
       }
      Â
       else if(month == 2&& day == 29){
           if(isLeapYear()== 1){
             Â
           }
           else{
               cout<<\"Date:\"<               cout<<\"Day is not valid for this month\"<           }
       }
       else{
           cout<<\"Date:\"<           cout<<\"Day is not valid for this month\"<       }
       if(year < 0){
           cout<<\"Date:\"<           cout<<\"year is not valid\"<       }
       this->month =month;
       this->day =day;
       this->year =year;
   }
   int isLeapYear(){
       int year =this->year;
      Â
       if((year % 400) ==0){
           return1;
       }
     Â
     Â
       else if((year % 100)== 0){
           return0;
       }
       else if((year%4) ==0)
           return1;
       // else
       return 0;
   }
   void setDate(int month,int day,int year){
      Â
       intmaxNumberOfDays[]={31,28,31,30,31,30,31,31,30,31,30,31};
     Â
      Â
       if(month < 1 ||month > 12){
           cout<<\"Date:\"<           cout<<\"Month is not valid\"<       }
      Â
       if(day >= 1&& day <= maxNumberOfDays[month]){
       }
      Â
       else if(month == 2&& day == 29){
           if(isLeapYear()== 1){
              Â
           }
           else{
               cout<<\"Date:\"<               cout<<\"Day is not valid for this month\"<           }
       }
       else{
           cout<<\"Date:\"<           cout<<\"Day is not valid for month=\"<       }
       if(year < 0){
           cout<<\"Date:\"<           cout<<\"year is not valid\"<       }
       this->month =month;
       this->day =day;
       this->year =year; Â
   }
   string getDate(){
       return(to_string(this->month)+\"-\"+to_string(this->day)+\"-\"+to_string(this->year));
   }
};
--------------------------------------------------------
for main.cpp
#include\"dateTypeImp.cpp\"
int main(){
   int month;
   int day;
   int year;
   cin>>month>>day>>year;
   dateType Date1(month,day,year);
 Â
   cout<<\"Date1:\"<   int isLeap = Date1.isLeapYear();
   if(isLeap == 1){
       cout<<\" this isa Leap Year\"<   }
   else{
       cout<<\" this isnot a Leap Year\"<   }
   cin>>month>>day>>year;
   dateType Date2;
   Date2.setDate(month,day,year);
   cout<<\"Date2:\"<   isLeap = Date2.isLeapYear();
   if(isLeap == 1){
       cout<<\" this isa Leap Year\"<   }
   else{
       cout<<\" this isnot a Leap Year\"<   }
   cin>>month>>day>>year;
   dateType Date3(month,day,year);
   cin>>month>>day>>year;
   dateType Date4;
   Date4.setDate(month,day,year);
 Â
   cin>>month>>day>>year;
   dateType Date5(month,day,year);
 Â
   return 0;
}