15. Consider the class dateType given in Chapter 13. In this class, add the func
ID: 3545490 • Letter: 1
Question
15. Consider the class dateType given in Chapter 13. In this class, add the functions to overload the increment and decrement operators to increase the date by a day and decrease the data by a day, respectively; relational operators to compare two dates; and stream operators for easy input and output. (assume that the date is input and output in the form MM-DD-YYYY.) Also write a program to test your class.
I was given the dateType.h and dateTypeImp.cpp.
dateType.h
#ifndef dateType_H
#define dateType_H
class dateType
{
public:
void setDate(int month, int day, int year);
//Function to set the date.
//The member variables dMonth, dDay, and dYear are set
//according to the parameters.
//Postcondition: dMonth = month; dDay = day;
// dYear = year
int getDay() const;
//Function to return the day.
//Postcondition: The value of dDay is returned.
int getMonth() const;
//Function to return the month.
//Postcondition: The value of dMonth is returned.
int getYear() const;
//Function to return the year.
//Postcondition: The value of dYear is returned.
void printDate() const;
//Function to output the date in the form mm-dd-yyyy.
dateType(int month = 1, int day = 1, int year = 1900);
//Constructor to set the date
//The member variables dMonth, dDay, and dYear are set
//according to the parameters.
//Postcondition: dMonth = month; dDay = day; dYear = year;
// If no values are specified, the default
// values are used to initialize the member
// variables.
private:
int dMonth; //variable to store the month
int dDay; //variable to store the day
int dYear; //variable to store the year
};
#endif
dateTypeImp.cpp
//Implementation file date
#include
#include "dateType.h"
using namespace std;
void dateType::setDate(int month, int day, int year)
{
dMonth = month;
dDay = day;
dYear = year;
}
int dateType::getDay() const
{
return dDay;
}
int dateType::getMonth() const
{
return dMonth;
}
int dateType::getYear() const
{
return dYear;
}
void dateType::printDate() const
{
cout << dMonth << "-" << dDay << "-" << dYear;
}
//Constructor with parameters
dateType::dateType(int month, int day, int year)
{
dMonth = month;
dDay = day;
dYear = year;
}
Explanation / Answer
dataType.h
#ifndef dateType_H
#define dateType_H
#include <iostream>
using namespace std;
class dateType
{
static const int MONTHS_DAY[12];
public:
void setDate(int month, int day, int year);
//Function to set the date.
//The member variables dMonth, dDay, and dYear are set
//according to the parameters.
//Postcondition: dMonth = month; dDay = day;
// dYear = year
int getDay() const;
//Function to return the day.
//Postcondition: The value of dDay is returned.
int getMonth() const;
//Function to return the month.
//Postcondition: The value of dMonth is returned.
int getYear() const;
//Function to return the year.
//Postcondition: The value of dYear is returned.
void printDate() const;
//Function to output the date in the form mm-dd-yyyy.
dateType(int month = 1, int day = 1, int year = 1900);
//Constructor to set the date
//The member variables dMonth, dDay, and dYear are set
//according to the parameters.
//Postcondition: dMonth = month; dDay = day; dYear = year;
// If no values are specified, the default
// values are used to initialize the member
// variables.
bool isLeap()const;
dateType& operator++();
dateType operator++(int);
dateType& operator--();
dateType operator--(int);
bool operator<(const dateType& other)const;
bool operator<=(const dateType& other)const;
bool operator==(const dateType& other)const;
bool operator>(const dateType& other)const;
bool operator>=(const dateType& other)const;
friend ostream& operator<<(ostream& out, const dateType& d);
friend istream& operator>>(istream& in, dateType& d);
private:
bool isValidDayInMonth()const;
void incr();
void decr();
private:
int dMonth; //variable to store the month
int dDay; //variable to store the day
int dYear; //variable to store the year
};
#endif
dataTypeImp.cpp
#include <iostream>
#include <iomanip>
#include "dateType.h"
using namespace std;
const int dateType::MONTHS_DAY[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
void dateType::setDate(int month, int day, int year)
{
dMonth = month;
dDay = day;
dYear = year;
}
int dateType::getDay() const
{
return dDay;
}
int dateType::getMonth() const
{
return dMonth;
}
int dateType::getYear() const
{
return dYear;
}
void dateType::printDate() const
{
cout << dMonth << "-" << dDay << "-" << dYear;
}
//Constructor with parameters
dateType::dateType(int month, int day, int year)
{
dMonth = month;
dDay = day;
dYear = year;
}
bool dateType::isLeap()const
{
return (dYear%400==0) || (dYear%4==0 && dYear%100);
}
bool dateType::isValidDayInMonth()const
{
if (dDay < 1) return false;
return dDay <= MONTHS_DAY[dMonth-1] + (dMonth==2 && isLeap());
}
void dateType::incr()
{
++dDay;
if (!isValidDayInMonth())
{
dDay = 1;
++dMonth;
if (dMonth > 12)
{
++dYear;
dMonth = 1;
}
}
}
void dateType::decr()
{
--dDay;
if (!isValidDayInMonth())
{
--dMonth;
if (dMonth < 1)
{
--dYear;
dMonth = 12;
}
dDay = MONTHS_DAY[dMonth-1] + (dMonth==2 && isLeap());
}
}
dateType& dateType::operator++()
{
incr();
return *this;
}
dateType dateType::operator++(int dummy)
{
dateType ret = *this;
incr();
return ret;
}
dateType& dateType::operator--()
{
decr();
return *this;
}
dateType dateType::operator--(int dummy)
{
dateType ret = *this;
decr();
return ret;
}
bool dateType::operator<(const dateType& other)const
{
if (dYear == other.dYear)
{
if (dMonth == other.dMonth) return dDay < other.dDay;
else return dMonth < other.dMonth;
}
return dYear < other.dYear;
}
bool dateType::operator==(const dateType& other)const
{
return dYear==other.dYear && dMonth==other.dMonth && dDay==other.dDay;
}
bool dateType::operator<=(const dateType& other)const
{
return *this < other || *this == other;
}
bool dateType::operator>(const dateType& other)const
{
return other < *this;
}
bool dateType::operator>=(const dateType& other)const
{
return other < *this || *this == other;
}
ostream& operator<<(ostream& out, const dateType& d)
{
char c = out.fill();
return out << setfill('0') << setw(2) << d.dMonth << '-'
<< setw(2) << d.dDay << '-' << d.dYear << setfill(c);
}
istream& operator>>(istream& in, dateType& d)
{
char sep;
return in >> d.dMonth >> sep >> d.dDay >> sep >> d.dYear;
}
//----------------------------------------------------------------------------------------------
//testdriver.cpp
#include <iostream>
#include <iomanip>
#include "dateType.h"
using namespace std;
void printComparisons(const dateType& date1, const dateType& date2)
{
cout << " Is " << date1 << " less than " << date2 << "? "
<< (date1 < date2) << " "
<< "Is " << date2 << " less than or equal to " << date2 << "? "
<< (date1 <= date2) << " "
<< "Is " << date1 << " equal to " << date2 << "? "
<< (date1 == date2) << " "
<< "Is " << date1 << " greater than " << date2 << "? "
<< (date1 > date2) << " "
<< "Is " << date1 << " greater than or equal to " << date2 << "? "
<< (date1 >= date2) << " ";
}
int main()
{
dateType test1(1,15,2000);
// test output stream operator<<
cout << test1 << " "; // 01-15-2000
// test prefix and postfix increment
for (int i = 0; i < 31; ++i) ++test1; //increase one month (jan)
cout << test1 << " "; //should print 02-15-2000
for (int i = 0; i < 29; ++i) ++test1; //increase one month (feb leap year)
cout << test1 << " "; //should print 03-15-2000
for (int i = 0; i < 31; ++i) ++test1; //increase one month (mar)
cout << test1 << " "; //should print 04-15-2000
for (int i = 0; i < 30; ++i) ++test1; //increase one month (apr)
cout << test1 << " "; //should print 05-15-2000
for (int i = 0; i < 31; ++i) ++test1; //increase one month (may)
cout << test1 << " "; //should print 06-15-2000
for (int i = 0; i < 30; ++i) ++test1; //increase one month (jun)
cout << test1 << " "; //should print 07-15-2000
for (int i = 0; i < 31; ++i) ++test1; //increase one month (jul)
cout << test1 << " "; //should print 08-15-2000
for (int i = 0; i < 31; ++i) ++test1; //increase one month (aug)
cout << test1 << " "; //should print 09-15-2000
for (int i = 0; i < 30; ++i) ++test1; //increase one month (sep)
cout << test1 << " "; //should print 10-15-2000
for (int i = 0; i < 31; ++i) ++test1; //increase one month (oct)
cout << test1 << " "; //should print 11-15-2000
for (int i = 0; i < 30; ++i) ++test1; //increase one month (nov)
cout << test1 << " "; //should print 12-15-2000
for (int i = 0; i < 31; ++i) ++test1; //increase one month (dec)
cout << test1 << " "; //should print 01-15-2001
for (int i = 0; i < 365; ++i) ++test1; //increase one year (2001)
cout << test1 << " "; //should print 01-15-2002
for (int i = 0; i < 365*2; ++i) test1++; //increase (postfix) two year (2002, 2003)
cout << test1 << " "; //should print 01-15-2004
for (int i = 0; i < 366; ++i) test1++; //increase (postfix) two year (2004). Notice 366 days.
cout << test1 << " "; //should print 01-15-2005
// test prefix and postfix increment, decrement
cout << " ";
dateType test2(2,28,1900); //not a leap year, no 02-29-1900
test2++;
cout << test2 << " "; //should print 03-01-1900
test2--;
cout << test2 << " "; //should print 02-28-1900
test2.setDate(12,31,2012);
++test2;
cout << test2 << " "; //should print 01-01-2013
--test2;
cout << test2 << " "; //should print 12-31-2012
//test input stream operator>>
cout << " Entered today's date: ";
cin >> test2;
cout << "Today is: " << test2 << " ";
cout << "Yesterday: " << --test2 << " ";
++test2;
cout << "Tomorrow: " << ++test2 << " ";
cout << "Hit ENTER to continue testing relational operators . . . ";
cin.ignore(100, ' ');
cin.get();
// test relational operators
cout << boolalpha;
test1.setDate(8,5,2011);
test2.setDate(8,17,2011);
printComparisons(test1, test2);
test1.setDate(2,17,2011);
test2.setDate(8,17,2011);
printComparisons(test1, test2);
test1.setDate(8,17,2006);
test2.setDate(8,17,2000);
printComparisons(test1, test2);
test1.setDate(8,17,2011);
test2.setDate(8,17,2011);
printComparisons(test1, test2);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.