C++ problem 11-2 In this chapter, the class dateType was designed to implement the date in a...

80.2K

Verified Solution

Question

Programming

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;

}

Answer & Explanation Solved by verified expert
3.7 Ratings (645 Votes)
Hey Note If you have any queries related the answer please do comment I would be very happy to    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students