Question: #ifndef DATE_H #define DATE_H #include #include using namespace std; class Date { friend std:: ostream& operator < = 1 && mm = 1900 && yy
#ifndef DATE_H
#define DATE_H
#include
#include
using namespace std;
class Date {
friend std:: ostream& operator<<(std::ostream&, const Date&);
public:
Date(int m = 1, int d =1, int y = 1900);
void setDate (int, int, int);
Date& operator++();
Date operator++(int);
Date& operator+=(unsigned int);
static bool leapYear(int);
bool endOfMonth(int) const;
private:
unsigned int month;
unsigned int day;
unsigned int year;
static const std::array
void helpIncrement();
};
#include
#include
#include "Date.h"
using namespace std;
const array
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Date::Date(int month, int day, int year){
setDate(month, day, year);}
void Date::setDate(int mm, int dd, int yy){
if (mm >= 1 && mm <= 12){
month = mm;}
else{
throw invalid_argument{"Month must be 1-12"};}
if (yy >= 1900 && yy<= 2100){
year =yy;}
else{
throw invalid_argument{"Year must be >= 1900 and <= 2100"};}
if ((mn==2 && leapYear(year) && dd >= 1 && dd <=29) || (dd >=1 && dd <=days[mn])){
day = dd;}
else {
throw invalid_argument{
"Day is out of range for current month and year"};
}}
Date& Date::operator++(){
helpIncrement();
return *this;
}
Date Date::operator++(int){
Date temp{*this};
helpIncrement();
return temp;}
Date& Date::operator+=(unsigned int additionalDays){
for (unsigned int i=0; i < additionalDays; ++i){
helpIncrement();
}return *this;}
bool Date::leapYear(int testYear){
return (testYear % 400 == 0 || (testYear % 100 != 0 && testYear % 4 ==0));}
bool Date::endOfMonth(int testDay) const {
if (month ==2 && leapYear(year)){
return testDay == days[month];}}
void Date::helpIncrement(){
if (!endOfMonth(day)){
++day;}
else {
if (month < 12) {
++month; day = 1;}
else {++year; month = 1; day = 1;}}}
ostream& operator<<(ostream& output, const Date& d) {
static string monthName[13]{"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
output << monthName[d.month] << ' ' << d.day << ", " << d.year;
return output;}
#include
#include "Date.h"
using namespace std;
int main(){
Date d1{12, 27, 2010};
Date d2;
cout << "d1 is " << d1 << " d2 is " << d2;
cout << " d1 += 7 is " << (d1 += 7);
d2.setDate(2, 28, 2008);
cout << " d2 is " << d2;
cout << " ++d2 is " << ++d2 << " (leap year allows 29th)";
Date d3{7, 13, 2010);
cout << " Testing the prefix increment operator: " << " d3 is " << d3 << endl;
cout << "++d3 is " << ++d3 << endl;
cout << " d3 is " << d3;
cout << " Testing the postfix increment operator: " << " d3 is " << d3 << endl;
cout << "d3++ is " << d3++ << endl;
cout << " d3 is " << d3 << endl;}
Consider the class Date in C++ pasted above. Test the overloaded operators ++ and +=. Modify the class to define the overloaded -- and -=. Also define the overloaded rational operators < and >.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
