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

In this assignment, you are going to enhance a Date class to add functions to ov

ID: 3688528 • Letter: I

Question

In this assignment, you are going to enhance a Date class to add functions to overload several operators: 1. Increment (++) and decrement (--) operators, to increase the date by one day or decrease the date by one day, respectively. Include both pre and post increment and decrement. 2. Assignment operator (=) to copy date to another. 3. Relational operators (==, !=, <=,>=), to compare two dates. 4. Stream operators (<<, >>) for easy input and output. (Assume that the date is input and output in the format MM-DD-YYYY.) 5. Test all the functionalities in your main program. The files for class Date.h and Date.cpp are provided to you with the instructions. You can include these two files in your project. Then start to modify the code in both header file and implementation file. Test your class in your main program.

/*

* Date class implementation

*/

#include <iostream>

#include "Date.h"

using namespace std;

void Date::setDate(int month, int day, int year)

{

if (year >= 1)

dYear = year;

else

dYear = 1900;

if (1 <= month && month <= 12)

dMonth = month;

else

dMonth = 1;

switch (dMonth)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

if (1 <= day && day <= 31)

dDay = day;

else

dDay = 1;

break;

case 4:

case 6:

case 9:

case 11:

if (1 <= day && day <= 30)

dDay = day;

else

dDay = 1;

break;

case 2:

if (isLeapYear())

{

if (1 <= day && day <= 29)

dDay = day;

else

dDay = 1;

}

else

{

if (1 <= day && day <= 28)

dDay = day;

else

dDay = 1;

}

}

}

void Date::setMonth(int m)

{

dMonth = m;

}

void Date::setDay(int d)

{

dDay = d;

}

void Date::setYear(int y)

{

dYear = y;

}

int Date::getMonth() const

{

return dMonth;

}

int Date::getDay() const

{

return dDay;

}

int Date::getYear() const

{

return dYear;

}

int Date::getDaysInMonth()

{

int noOfDays;

switch (dMonth)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

noOfDays = 31;

break;

case 4:

case 6:

case 9:

case 11:

noOfDays = 30;

break;

case 2:

if (isLeapYear())

noOfDays = 29;

else

noOfDays = 28;

}

return noOfDays;

}

bool Date::isLeapYear()

{

if (((dYear % 4 == 0) && (dYear % 100 != 0)) || dYear % 400 == 0)

return true;

else

return false;

}

Date::Date()

{

setDate(1, 1, 1900);

}

Date::Date(int month, int day, int year)

{

setDate(month, day, year);

}

Date::~Date(){}

/*

* Date class definition

*/

#ifndef DATE_H

#define DATE_H

#include <iostream>

using namespace std;

class Date

{

public:

Date();

Date(int, int, int);

~Date();

void setDate(int, int, int);

void setMonth(int);

void setDay(int);

void setYear(int);

int getMonth() const;

int getDay() const;

int getYear() const;

bool isLeapYear();

int getDaysInMonth();

private:

int dMonth;

int dDay;

int dYear;

};

#endif

Explanation / Answer

Answered problems 1 and 2. Part -1 contains two for each operation - both post and pre increment and decremtns

/*

* Date class definition

*/

#ifndef DATE_H

#define DATE_H

#include <iostream>

using namespace std;

class Date

{

public:

Date();

Date(int, int, int);

~Date();

void setDate(int, int, int);

void setMonth(int);

void setDay(int);

void setYear(int);

int getMonth() const;

int getDay() const;

int getYear() const;

bool isLeapYear();

int getDaysInMonth();
Date operator++ ();
Date operator++(int );
Date operator-- ();
Date operator--(int );
Date operator=(Date date );
void display();

private:

int dMonth;

int dDay;

int dYear;

};

#endif

/*

* Date class implementation

*/

#include <iostream>

#include "Date.h"

using namespace std;

void Date::setDate(int month, int day, int year)

{

   if (year >= 1)

       dYear = year;

   else

       dYear = 1900;

   if (1 <= month && month <= 12)

       dMonth = month;

   else

       dMonth = 1;

   switch (dMonth)

   {

   case 1:

   case 3:

   case 5:

   case 7:

   case 8:

   case 10:

   case 12:

       if (1 <= day && day <= 31)

           dDay = day;

       else

           dDay = 1;

       break;

   case 4:

   case 6:

   case 9:

   case 11:

       if (1 <= day && day <= 30)

           dDay = day;

       else

           dDay = 1;

       break;

   case 2:

       if (isLeapYear())

       {

           if (1 <= day && day <= 29)

               dDay = day;

           else

               dDay = 1;

       }

       else

       {

           if (1 <= day && day <= 28)

               dDay = day;

           else

               dDay = 1;

       }

   }

}

void Date::setMonth(int m)

{

   dMonth = m;

}

void Date::setDay(int d)

{

   dDay = d;

}

void Date::setYear(int y)

{

   dYear = y;

}

int Date::getMonth() const

{

   return dMonth;

}

int Date::getDay() const

{

   return dDay;

}

int Date::getYear() const

{

   return dYear;

}

int Date::getDaysInMonth()

{

   int noOfDays;

   switch (dMonth)

   {

   case 1:

   case 3:

   case 5:

   case 7:

   case 8:

   case 10:

   case 12:

       noOfDays = 31;

       break;

   case 4:

   case 6:

   case 9:

   case 11:

       noOfDays = 30;

       break;

   case 2:

       if (isLeapYear())

           noOfDays = 29;

       else

           noOfDays = 28;

   }

   return noOfDays;

}

bool Date::isLeapYear()

{

   if (((dYear % 4 == 0) && (dYear % 100 != 0)) || dYear % 400 == 0)

       return true;

   else

       return false;

}

Date::Date()

{

   setDate(1, 1, 1900);

}

Date::Date(int month, int day, int year)

{

   setDate(month, day, year);

}

Date::~Date() {
}

void Date::display() {
   cout << dMonth <<"-" <<dDay<<"-"<<dYear<<endl;
}
// overloaded prefix ++ operator
Date Date:: operator++ ()
      {
                   // increment this object
       ++dDay;
         //setDay(dDay);
         return Date(dMonth,dDay,dYear);
      }
      // overloaded postfix ++ operator
Date Date:: operator++(int )
      {
         // save the orignal value
       //setDate(dMonth, dDay++, dYear);
   //dDay++;
   return(Date(dMonth,dDay++,dYear));
       }

// overloaded prefix -- operator
Date Date:: operator-- ()
      {
                   // increment this object
       --dDay;
         //setDay(dDay);
         return Date(dMonth,dDay,dYear);
      }
      // overloaded postfix -- operator
Date Date:: operator--(int )
      {
   //dDay++;
   return(Date(dMonth,dDay--,dYear));
       }

Date Date:: operator=(Date date )
      {
       Date newD(date.getMonth(),date.getDay(),date.getYear());
       return newD;

      }
int main() {

   Date D1(12,4,2016), D2(15,5,2016);
   Date D3 = ++D1;
   D3.display();
   D3 = D1++;
   D3.display();
   D1.display();
   Date D4 = D1;
   D4.display();

   D3 = D1--;
   D2.display();
   D1.display();
   --D1;
   D1.display();
   return 1;
}


--output----------

12-5-2016
12-5-2016
12-6-2016
12-6-2016
1-5-2016
12-5-2016
12-4-2016