Override the operator -= for the Date class given at the end of Unit 16. To support this method, also create a private method called decrement() that works similar to increment, but correctly subtracts one day from current date. Test your program using June 7, 1982 and subtract 5 days, add 1 and then subtract 15. Be sure to print out each intermediary date. Turn in copies of all three files needed to compile your program: main.cpp, Date.cpp, and Date.h
(I have some of this program written from a previous program that i tried to edit it to fit the question above but i am having trouble making the program work the way the questions is asking for. Please help and some of the program lines are not being used because it is from a previous program.)
here is my code:
//header file
#ifndef DATE_H
#define DATE_H
#include <iostream>
using namespace std;
#define nMONTHS 12
class Date
{
public:
//Constructors
Date( int m = 1, int d = 1, int y = 1000 );
friend ostream& operator<<( ostream&, const Date& date );
friend istream& operator>>( istream&, Date& date );
Date operator+( const int d );
Date operator+=( const int d );
Date operator-=( const int d );
Date operator--( ); //prefix operator (ex. ++date;)
Date operator--(int dummy);
Date operator++( ); //prefix operator (ex. ++date;)
Date operator++(int dummy); //postfix operator (ex. date++;)
bool operator<( const Date date );
int& operator[]( const int& index );
void SetDate( int m, int d, int y );
int GetDay( ) const;
int GetMonth( ) const;
int GetYear( ) const;
//Returns true if it is a leap year
bool LeapYear() const;
void AddNumberOfDays( int days );
//aprivate:
int month;
int day;
int year;
int nDays[nMONTHS + 1];
void increment();
void decrement();
};
#endif
//Date.cpp file:
#include <iostream>
#include "date.h"
Date::Date( int m, int d, int y )
{
nDays[0]=0; nDays[1]=31; nDays[2]=28;
nDays[3]=31; nDays[4]=30; nDays[5]=31;
nDays[6]=30; nDays[7]=31; nDays[8]=31;
nDays[9]=30; nDays[10]=31; nDays[11]=30;
nDays[12]=31;
int& Date::operator[]( const int& index )
{
if( index == 0 )
return month;
else if (index == 1 )
return day;
else if (index == 2 )
return year;
else
{
cout << "Date::operator[]( const int& index ) - Index out of range error." << endl;
exit(0);
}
}
void Date::SetDate( int m, int d, int y )
{
//Ensure arguments are valid. If not, set to default values
month = ( m>0 && m<13 ) ? m : 1;
year = ( y>0 ) ? y : 1;
//Account for leap years
if( month == 2 && LeapYear( ) )
{
day = ( d>0 && d<=29 ) ? d : 1;
}else{
day = ( d>0 && d<=nDays[month] ) ? d : 1;
}
return;
}
int Date::GetDay( ) const { return day; }
int Date::GetMonth( ) const { return month; }
int Date::GetYear( ) const { return year; }
void Date::AddNumberOfDays( int days )
{
for( int i=1; i<=days; i++ )
increment( );
return;
}
bool Date::LeapYear( ) const
{
// leap years happen every 4 years.
if( year%4 == 0 )
{
//but not every hundred years, unless 400 years have passed.
if( year%400 == 0 )
return true;
if( year%100 == 0 )
return false;
return true;
}
return false;
}
void Date::increment()
{
day++;
//Handle 4 special cases
if( month==2 && LeapYear() && day == 30 ){
// february: leap year
day = 1;
month = 3;
}else if(month==2 && !LeapYear() && day == 29 ){
// february: not a leap year
day = 1;
month = 3;
}else if( month==12 && day == 32 ){
// end of year case
day = 1;
month = 1;
year++;
}else if( month != 2 && day == nDays[month]+1 ){
// end of all months but february
day = 1;
month++;
}
return;
}
//main.cpp
#include <iostream>
using namespace std;
#include "date.h"
int main()
{
Date partyDate( 12, 25, 1999 );
Date yourDate;
cin >> yourDate;