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

// File: Driver.cpp // Note: Used to test our date class with overloaded functio

ID: 3847942 • Letter: #

Question

// File: Driver.cpp
// Note: Used to test our date class with overloaded functions

#include
#include "d_date.h"

using namespace std;

void main (void)
{
   date date1;
   date date2;

   cout << "Enter date1 and date2:" << endl;

   while (cin >> date1 >> date2)
   {
       cout << "Printing date1 and date2" << endl;
       cout << date1 << endl << date2 << endl;

       if (date1 == date2)
           cout << date1 << " is equal to " << date2 << endl;

       if (date1 != date2)
           cout << date1 << " is not equal to " << date2 << endl;

       if (date1 < date2)
           cout << date1 << " is less than " << date2 << endl;

       if (date1 > date2)
           cout << date1 << " is greater than " << date2 << endl;

       ++date1;
       ++date2;

       cout << "Increment of date1: " << date1 << endl;
       cout << "Increment of date2: " << date2 << endl;

       cout << endl << "---End of Run---" << endl << endl;

       cout << "Enter date1 and date2:" << endl;
   }

}

#ifndef DATE_CLASS
#define DATE_CLASS

#include <iostream>
#include <iomanip>
#include <string>

#include "d_except.h"

using namespace std;

class date
{
public:
date (int mm=1, int dd=1, int yyyy=1900);
           // supply date in format MM/DD/YYYY
           // preconditions: 1 <= mm <= 12,
           // 1 <= dd <= daysInMonth()

void writeShortDate () const;
           // output the date in the format "MM/DD/YYYY"
void writeLongDate () const;
           // output the date in the format "month day, year"

void incrementDate(int ndays);
           // add ndays days to the date
           // precondition: 0 <= ndays <= 365

int numberOfDays() const;
           // return the number of days into the year

int getMonth() const;
           // return the month as integer value 1 to 12
int getDay() const;
           // return day of the month
int getYear() const;
           // return the year

void setMonth(int mm);
           // update the month
           // precondition: 1 <= mm <= 12
void setDay(int dd);
           // update the day
           // precondition: 1 <= dd <= daysInMonth()
void setYear(int yyyy);
           // update the year
           // precondition: if the date is February 29,
           // yyyy must be a leap year
int daysInMonth() const;
           // return number of days in the month

bool isLeapYear() const;
           // is the current year a leap year (true/false)

private:
enum monthName {Jan = 1, Feb, Mar, Apr, May, Jun,
Jul, Aug, Sep, Oct, Nov, Dec};
           // private type used by date

int month, day, year;
           // private members that specify the date

};

// constructor. month, day, year given as integer
// values mm dd yyyy
date::date (int mm, int dd, int yyyy):
month(mm), day(dd), year(yyyy)
{
if (month < 1 || month > 12)
       throw dateError("date constructor:", month, "invalid month");

if (day <= 0 || day > daysInMonth())
       throw dateError("date constructor:", day, "invalid day of month");
}

// write date in the format "MM/DD/YYYY"
void date::writeShortDate () const
{
// save current format flags and fill character
ios_base::fmtflags currentFlags = cout.flags();
char currentFill = cout.fill();

// enable right justification
cout.setf(ios::right,ios::adjustfield);

// output the month right-justified in
// two character positions, blank fill
cout.fill(' ');
cout << setw(2) << month;

// set fill char to '0'
cout.fill('0');
cout << '/' << setw(2) << day << '/' << year;

// restore the fill char and the format flags
cout.fill(currentFill);
cout.setf(currentFlags);
}

// write date with full month name
void date::writeLongDate() const
{
// array begins with empty string corresponding to month 0
static string monthNames[] =
{"", "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"
};
// use month as index into monthNames
cout << monthNames[month] << " " << day << ", " << year;
}

// add ndays days to the date. ndays must be in the range
// 0 <= ndays <= 365
void date::incrementDate(int ndays)
{
int totalDays, daysInYear = 365;
int addDay;

if (ndays < 0 || ndays > 365)
       throw dateError("date incrementDate():", ndays, "is out of range");

if(isLeapYear())
{
addDay = 1;
daysInYear++;
}
else
addDay = 0;

totalDays = numberOfDays() + ndays;
if (totalDays/daysInYear == 1 && totalDays%daysInYear != 0)
{
year++;
if(isLeapYear())
addDay = 1;
else
addDay = 0;
totalDays = totalDays % daysInYear;
}

if (totalDays <= 31)
{
month = 1;
day = totalDays;
}
else if (totalDays <= 59 + addDay)
{
month = 2;
day = totalDays - 31;
}
else if (totalDays <= 90 + addDay)
{
month = 3;
day = totalDays - (59 + addDay);
}
else if (totalDays <= 120 + addDay)
{
month = 4;
day = totalDays - (90 + addDay);
}
else if (totalDays <= 151 + addDay)
{
month = 5;
day = totalDays - (120 + addDay);
}
else if (totalDays <= 181 + addDay)
{
month = 6;
day = totalDays - (151 + addDay);
}
else if (totalDays <= 212 + addDay)
{
month = 7;
day = totalDays - (181 + addDay);
}
else if (totalDays <= 243 + addDay)
{
month = 8;
day = totalDays - (212 + addDay);
}
else if (totalDays <= 273 + addDay)
{
month = 9;
day = totalDays - (243 + addDay);
}
else if (totalDays <= 304 + addDay)
{
month = 10;
day = totalDays - (273 + addDay);
}
else if (totalDays <= 334 + addDay)
{
month = 11;
day = totalDays - (304 + addDay);
}
else if (totalDays <= 365 + addDay)
{
month = 12;
day = totalDays - (334 + addDay);
}
}


// determine the number of days into the year
int date::numberOfDays() const
{
// assign a place holder value of 0 for monthLength[0]
int monthLength[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

int daysToDate = 0, i;

// add up all the days in the preceding months
for (i = 1; i < month; i++)
daysToDate += monthLength[i];

// if year is a leap year and the month is March or later,
// increment daysToDate. use member function isLeapYear()
if (isLeapYear() && month > 2)
daysToDate += day + 1;
else
daysToDate += day;

return daysToDate;
}

// return the month as integer value 1 to 12
int date::getMonth() const
{
return month;
}

// return day of the month
int date::getDay() const
{
return day;
}

// return the year
int date::getYear() const
{
return year;
}

// assign a new month
void date::setMonth(int mm)
{
// verify that mm is in the range 1 to 12
if (mm < 1 || mm > 12)
       throw dateError("date setMonth():", mm, "invalid month");

month = mm;
}

// assign a new day of current month
void date::setDay(int dd)
{
// verify that dd is in correct range for
// the current month
if (dd <= 0 || dd > daysInMonth())
       throw dateError("date setDay():", dd, "invalid day of month");

day = dd;
}

void date::setYear(int yyyy)
{
// assign the new year
year = yyyy;

// if the date is February 29, yyyy must be a
// leap year
if (month == 2 && day == 29 && !isLeapYear())
       throw dateError("date setYear():",
                           year, "Not a leap year. February 29 invalid");
}

// return the number of days in the month
int date::daysInMonth() const
{
int monthLength;

// monthName(month) converts integer month to the
// equivalent monthName object
switch (monthName(month))
{
case Jan:
case Mar:
case May:
case Jul:
case Aug:
case Oct:
case Dec: monthLength = 31; // months with 31 days
break;

case Apr:
case Jun:
case Sep:
case Nov: monthLength = 30; // months with 30 days
break;

case Feb: if (isLeapYear()) // special case of Feb
monthLength = 29;
else
monthLength = 28;
break;
}
return monthLength;
}

// is the current year a leap year (true/false)
bool date::isLeapYear() const
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return true;
else
return false;
}

#endif // DATE_CLASS

#ifndef EXCEPTION_CLASSES
#define EXCEPTION_CLASSES

#include <sstream>
#include <string>

using namespace std;

class baseException
{
   public:
       baseException(const string& str = ""):
           msgString(str)
       {
           if (msgString == "")
               msgString = "Unspecified exception";
       }

       string what() const
       {
           return msgString;
       }

   // protected allows a derived class to access msgString.
   // chapter 13 discusses protected in detail
   protected:
       string msgString;
};

// failure to allocate memory (new() returns NULL)
class memoryAllocationError: public baseException
{
   public:
       memoryAllocationError(const string& msg = ""):
           baseException(msg)
       {}
};

// function argument out of proper range
class rangeError: public baseException
{
   public:
       rangeError(const string& msg = ""):
           baseException(msg)
       {}
};

// index out of range
class indexRangeError: public baseException
{
   public:
       indexRangeError(const string& msg, int i, int size):
           baseException()
       {
           ostringstream indexErr;

           indexErr << msg << " index " << i << " size = " << size << ends;
           // indexRangeError can modify msgString, since it is in
           // the protected section of baseException
           msgString = indexErr.str();
       }
};

// attempt to erase from an empty container
class underflowError: public baseException
{
   public:
       underflowError(const string& msg = ""):
           baseException(msg)
       {}
};

// attempt to insert into a full container
class overflowError: public baseException
{
   public:
       overflowError(const string& msg = ""):
           baseException(msg)
       {}
};

// error in expression evaluation
class expressionError: public baseException
{
   public:
       expressionError(const string& msg = ""):
           baseException(msg)
       {}
};

// bad object reference
class referenceError: public baseException
{
   public:
       referenceError(const string& msg = ""):
           baseException(msg)
       {}
};

// feature not implemented
class notImplementedError: public baseException
{
   public:
       notImplementedError(const string& msg = ""):
           baseException(msg)
       {}
};

// date errors
class dateError: public baseException
{
   public:
       dateError(const string& first, int v, const string& last):
           baseException()
       {
           ostringstream dateErr;

           dateErr << first << ' ' << v << ' ' << last << ends;
           // dateError can modify msgString, since it is in
           // the protected section of baseException
           msgString = dateErr.str();
       }
};

// error in graph class
class graphError: public baseException
{
   public:
       graphError(const string& msg = ""):
           baseException(msg)
       {}
};

// file open error
class fileOpenError: public baseException
{
   public:
       fileOpenError(const string& fname):
           baseException()
       {
           ostringstream fileErr;

           fileErr << "Cannot open "" << fname << """ << ends;
           // fileOpenError can modify msgString, since it is in
           // the protected section of baseException
           msgString = fileErr.str();
       }
};

// error in graph class
class fileError: public baseException
{
   public:
       fileError(const string& msg = ""):
           baseException(msg)
       {}
};

#endif   // EXCEPTION_CLASSES

Modify the above date class. You will need to copy 2 files: d_date.h and d_except.h. a) Overload the relational operators (==, !=, < and >) to compare dates. Two dates are to be compared by chronology of the year. For example: date(3,5,1999) != date(3,7,1999) (implement as external – free function) date(3,5,1999) == date(3,5,1999) (implement as external – free function) date(5,5,1977) > date(10,24,1973) (implement as member function) date(12,25,1944) < date(9,30,1982) (implement as member function) b) Write member function operator++ that can increment the date by one day. c) Overload the input and output stream operators so that the date can be read/written as follows: date date1; cin >> date1 >> … // read in as 12/25/1944 cout << date1 << … // printout out as 12/25/1944

Explanation / Answer

#include
#include "d_date.h"

using namespace std;

void main (void)
{
   date date1;
   date date2;

   cout << "Enter date1 and date2:" << endl;

   while (cin >> date1 >> date2)
   {
       cout << "Printing date1 and date2" << endl;
       cout << date1 << endl << date2 << endl;

       if (date1 == date2)
           cout << date1 << " is equal to " << date2 << endl;

       if (date1 != date2)
           cout << date1 << " is not equal to " << date2 << endl;

       if (date1 < date2)
           cout << date1 << " is less than " << date2 << endl;

       if (date1 > date2)
           cout << date1 << " is greater than " << date2 << endl;

       ++date1;
       ++date2;

       cout << "Increment of date1: " << date1 << endl;
       cout << "Increment of date2: " << date2 << endl;

       cout << endl << "---End of Run---" << endl << endl;

       cout << "Enter date1 and date2:" << endl;
   }

}

#ifndef DATE_CLASS
#define DATE_CLASS

#include <iostream>
#include <iomanip>
#include <string>

#include "d_except.h"

using namespace std;

class date
{
public:
date (int mm=1, int dd=1, int yyyy=1900);
           // supply date in format MM/DD/YYYY
           // preconditions: 1 <= mm <= 12,
           // 1 <= dd <= daysInMonth()

void writeShortDate () const;
           // output the date in the format "MM/DD/YYYY"
void writeLongDate () const;
           // output the date in the format "month day, year"

void incrementDate(int ndays);
           // add ndays days to the date
           // precondition: 0 <= ndays <= 365

int numberOfDays() const;
           // return the number of days into the year

int getMonth() const;
           // return the month as integer value 1 to 12
int getDay() const;
           // return day of the month
int getYear() const;
           // return the year

void setMonth(int mm);
           // update the month
           // precondition: 1 <= mm <= 12
void setDay(int dd);
           // update the day
           // precondition: 1 <= dd <= daysInMonth()
void setYear(int yyyy);
           // update the year
           // precondition: if the date is February 29,
           // yyyy must be a leap year
int daysInMonth() const;
           // return number of days in the month

bool isLeapYear() const;
           // is the current year a leap year (true/false)

private:
enum monthName {Jan = 1, Feb, Mar, Apr, May, Jun,
Jul, Aug, Sep, Oct, Nov, Dec};
           // private type used by date

int month, day, year;
           // private members that specify the date

};

// constructor. month, day, year given as integer
// values mm dd yyyy
date::date (int mm, int dd, int yyyy):
month(mm), day(dd), year(yyyy)
{
if (month < 1 || month > 12)
       throw dateError("date constructor:", month, "invalid month");

if (day <= 0 || day > daysInMonth())
       throw dateError("date constructor:", day, "invalid day of month");
}

// write date in the format "MM/DD/YYYY"
void date::writeShortDate () const
{
// save current format flags and fill character
ios_base::fmtflags currentFlags = cout.flags();
char currentFill = cout.fill();

// enable right justification
cout.setf(ios::right,ios::adjustfield);

// output the month right-justified in
// two character positions, blank fill
cout.fill(' ');
cout << setw(2) << month;

// set fill char to '0'
cout.fill('0');
cout << '/' << setw(2) << day << '/' << year;

// restore the fill char and the format flags
cout.fill(currentFill);
cout.setf(currentFlags);
}

// write date with full month name
void date::writeLongDate() const
{
// array begins with empty string corresponding to month 0
static string monthNames[] =
{"", "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"
};
// use month as index into monthNames
cout << monthNames[month] << " " << day << ", " << year;
}

// add ndays days to the date. ndays must be in the range
// 0 <= ndays <= 365
void date::incrementDate(int ndays)
{
int totalDays, daysInYear = 365;
int addDay;

if (ndays < 0 || ndays > 365)
       throw dateError("date incrementDate():", ndays, "is out of range");

if(isLeapYear())
{
addDay = 1;
daysInYear++;
}
else
addDay = 0;

totalDays = numberOfDays() + ndays;
if (totalDays/daysInYear == 1 && totalDays%daysInYear != 0)
{
year++;
if(isLeapYear())
addDay = 1;
else
addDay = 0;
totalDays = totalDays % daysInYear;
}

if (totalDays <= 31)
{
month = 1;
day = totalDays;
}
else if (totalDays <= 59 + addDay)
{
month = 2;
day = totalDays - 31;
}
else if (totalDays <= 90 + addDay)
{
month = 3;
day = totalDays - (59 + addDay);
}
else if (totalDays <= 120 + addDay)
{
month = 4;
day = totalDays - (90 + addDay);
}
else if (totalDays <= 151 + addDay)
{
month = 5;
day = totalDays - (120 + addDay);
}
else if (totalDays <= 181 + addDay)
{
month = 6;
day = totalDays - (151 + addDay);
}
else if (totalDays <= 212 + addDay)
{
month = 7;
day = totalDays - (181 + addDay);
}
else if (totalDays <= 243 + addDay)
{
month = 8;
day = totalDays - (212 + addDay);
}
else if (totalDays <= 273 + addDay)
{
month = 9;
day = totalDays - (243 + addDay);
}
else if (totalDays <= 304 + addDay)
{
month = 10;
day = totalDays - (273 + addDay);
}
else if (totalDays <= 334 + addDay)
{
month = 11;
day = totalDays - (304 + addDay);
}
else if (totalDays <= 365 + addDay)
{
month = 12;
day = totalDays - (334 + addDay);
}
}


// determine the number of days into the year
int date::numberOfDays() const
{
// assign a place holder value of 0 for monthLength[0]
int monthLength[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

int daysToDate = 0, i;

// add up all the days in the preceding months
for (i = 1; i < month; i++)
daysToDate += monthLength[i];

// if year is a leap year and the month is March or later,
// increment daysToDate. use member function isLeapYear()
if (isLeapYear() && month > 2)
daysToDate += day + 1;
else
daysToDate += day;

return daysToDate;
}

// return the month as integer value 1 to 12
int date::getMonth() const
{
return month;
}

// return day of the month
int date::getDay() const
{
return day;
}

// return the year
int date::getYear() const
{
return year;
}

// assign a new month
void date::setMonth(int mm)
{
// verify that mm is in the range 1 to 12
if (mm < 1 || mm > 12)
       throw dateError("date setMonth():", mm, "invalid month");

month = mm;
}

// assign a new day of current month
void date::setDay(int dd)
{
// verify that dd is in correct range for
// the current month
if (dd <= 0 || dd > daysInMonth())
       throw dateError("date setDay():", dd, "invalid day of month");

day = dd;
}

void date::setYear(int yyyy)
{
// assign the new year
year = yyyy;

// if the date is February 29, yyyy must be a
// leap year
if (month == 2 && day == 29 && !isLeapYear())
       throw dateError("date setYear():",
                           year, "Not a leap year. February 29 invalid");
}

// return the number of days in the month
int date::daysInMonth() const
{
int monthLength;

// monthName(month) converts integer month to the
// equivalent monthName object
switch (monthName(month))
{
case Jan:
case Mar:
case May:
case Jul:
case Aug:
case Oct:
case Dec: monthLength = 31; // months with 31 days
break;

case Apr:
case Jun:
case Sep:
case Nov: monthLength = 30; // months with 30 days
break;

case Feb: if (isLeapYear()) // special case of Feb
monthLength = 29;
else
monthLength = 28;
break;
}
return monthLength;
}

// is the current year a leap year (true/false)
bool date::isLeapYear() const
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return true;
else
return false;
}

#endif // DATE_CLASS

#ifndef EXCEPTION_CLASSES
#define EXCEPTION_CLASSES

#include <sstream>
#include <string>

using namespace std;

class baseException
{
   public:
       baseException(const string& str = ""):
           msgString(str)
       {
           if (msgString == "")
               msgString = "Unspecified exception";
       }

       string what() const
       {
           return msgString;
       }

   // protected allows a derived class to access msgString.
   // chapter 13 discusses protected in detail
   protected:
       string msgString;
};

// failure to allocate memory (new() returns NULL)
class memoryAllocationError: public baseException
{
   public:
       memoryAllocationError(const string& msg = ""):
           baseException(msg)
       {}
};

// function argument out of proper range
class rangeError: public baseException
{
   public:
       rangeError(const string& msg = ""):
           baseException(msg)
       {}
};

// index out of range
class indexRangeError: public baseException
{
   public:
       indexRangeError(const string& msg, int i, int size):
           baseException()
       {
           ostringstream indexErr;

           indexErr << msg << " index " << i << " size = " << size << ends;
           // indexRangeError can modify msgString, since it is in
           // the protected section of baseException
           msgString = indexErr.str();
       }
};

// attempt to erase from an empty container
class underflowError: public baseException
{
   public:
       underflowError(const string& msg = ""):
           baseException(msg)
       {}
};

// attempt to insert into a full container
class overflowError: public baseException
{
   public:
       overflowError(const string& msg = ""):
           baseException(msg)
       {}
};

// error in expression evaluation
class expressionError: public baseException
{
   public:
       expressionError(const string& msg = ""):
           baseException(msg)
       {}
};

// bad object reference
class referenceError: public baseException
{
   public:
       referenceError(const string& msg = ""):
           baseException(msg)
       {}
};

// feature not implemented
class notImplementedError: public baseException
{
   public:
       notImplementedError(const string& msg = ""):
           baseException(msg)
       {}
};

// date errors
class dateError: public baseException
{
   public:
       dateError(const string& first, int v, const string& last):
           baseException()
       {
           ostringstream dateErr;

           dateErr << first << ' ' << v << ' ' << last << ends;
           // dateError can modify msgString, since it is in
           // the protected section of baseException
           msgString = dateErr.str();
       }
};

// error in graph class
class graphError: public baseException
{
   public:
       graphError(const string& msg = ""):
           baseException(msg)
       {}
};

// file open error
class fileOpenError: public baseException
{
   public:
       fileOpenError(const string& fname):
           baseException()
       {
           ostringstream fileErr;

           fileErr << "Cannot open "" << fname << """ << ends;
           // fileOpenError can modify msgString, since it is in
           // the protected section of baseException
           msgString = fileErr.str();
       }
};

// error in graph class
class fileError: public baseException
{
   public:
       fileError(const string& msg = ""):
           baseException(msg)
       {}
};

#endif