Question
Help me do these code. //////Date.h/////// #include using namespace std; class Date { private: int month; int day; int year; public: Date(); Date(int m, int
Help me do these code.
//////Date.h///////
#include
class Date { private: int month; int day; int year; public: Date(); Date(int m, int d, int y); int endOfMonth() const; int getMonth() const; int getDay() const; int getYear() const; void setMonth(int m); void setDay(int d); void setYear(int y); void printDate() const; bool operator==(const Date& myDate) const; Date operator++(); Date operator+ (const Date&, int);
}; ostream& operator
Date operator+ (int, const Date&);
////////////Date.cpp///////////////
// Name: // Student Number: // Date:
#include "Date.h"
Date::Date() { month = 0; day = 0; year = 0; }
Date::Date(int m, int d, int y) { month = m; day = d; year = y; }
int Date::endOfMonth() const { int lastDay = 0; switch (month) { case 1: lastDay = 31; break; case 2: if ((year%400==0)||((year%4==0 && year%100!=0))) lastDay = 29; else lastDay = 28; break; case 3: lastDay = 31; break; case 4: lastDay = 30; break; case 5: lastDay = 31; break; case 6: lastDay = 30; break; case 7: lastDay = 31; break; case 8: lastDay = 31; break; case 9: lastDay = 30; break; case 10: lastDay = 31; break; case 11: lastDay = 30; break; case 12: lastDay = 31; break; } return lastDay; }
int Date::getMonth()const { return month; }
int Date::getDay()const { return day; }
int Date::getYear()const { return year; }
void Date::setMonth(int m) { month = m; }
void Date::setDay(int d) { day = d; }
void Date::setYear(int y) { year = y; }
void Date::printDate() const { cout
bool Date::operator==(const Date& myDate) const { if (month == myDate.month && day == myDate.day && year == myDate.year) return true; else return false; }
Date Date::operator++() { if (day == endOfMonth()) //endOfMonth returns 28, 29, 30 or 31 depending on month { if (month == 12) { year += 1; month = 1; } else month += 1; day = 1; } else day += 1; return *this; }
ostream& operator
Date Date::operator+ (int z) { }
Date operator+ (int , const Date&) { }
////////main.cpp/////////
#include "Date.h" #include
int main() { Date date1; int month, day, year; cout
cout > month >> day >> year; date1.setMonth(month); date1.setDay(day); date1.setYear(year); cout > month >> day >> year;
//Calling constructor with three arguments Date date2(month, day, year); /*----------------------------------------------------------*/ /*-PART 1 Change this code to the comments indicated beside-*/ /*----------------------------------------------------------*/ //date1.printDate(); cout
//if (date1.sameMonth(date2)) if (date1 == date2) cout
++date1; ++date2; cout
/*----------------------------------------------------------*/ /*-PART 2 Uncomment these lines */ /*----------------------------------------------------------*/
Date date3, date4; date3 = date1 + 82; date4 = 6 + date2;
cout
return 0; }
Overload the + operator so that the following lines of code run in main: Date date3, date4; date3=date1+82; date4=6+date2; More Details: o Define two functions. The prototypes will look like this: Date operator+ (const Date&, int); Date operator+ (int, const Date&); Are they member or non-member functions? (Notice how many arguments they have) o Both functions will create a temporary date and return it. o Think about reusing as much code as possible. a. For one implementation of operator+, can you call the operator++ function? b. For the second implementation of operator+, can you call the first operator+ function? c. Together these two functions should be no more that 10 lines of code in the bodies. If you make it longer, you are working too hard. o Which of the two functions must be defined as a non-member? Why? Sample Run (after Part 2) 1 1 1 2 2 2 >./main Date is: 0/0/0 Please enter integer month, day, and year separated by spaces: Please enter integer month, day, and year separated by spaces: Date is: 1/1/1 Date is: 2/2/2 The dates are different. After incrementing Date is: 1/2/1 Date is: 2/3/2 After addition Date is: 1/2/1 Date is: 2/3/2 Date is: 3/25/1 Date is: 2/9/2
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