Question
In this assignment, you are going to enhance a Date class to add functions to overload several operators: 1. Increment (++) and decrement (--) operators,
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
#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
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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started