Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- #include "Date.h"
- ostream& operator<< (ostream &output, const Date &a)
- {
- output << a.month << '/' << a.day << '/' << a.year;
- return output;
- }
- istream& operator>>(istream &input, Date &a)
- {
- input >> a.month;
- cin.ignore(1);
- input >> a.day;
- cin.ignore(1);
- input >> a.year;
- return input;
- }
- //constructor validates month and calls utility function to validate day
- Date::Date(int mn, int dy, int yr)
- {
- setDate(mn, dy, yr);
- }
- Date& Date::setDate(int mn, int dy, int yr)
- {
- if(mn > 0 && mn <= 12)
- {
- month = mn;
- }
- else
- {
- month = 1;
- }
- year = yr; //could also validate year
- day = checkDay(dy); //to validate the day
- return *this; //should this be here?
- }
- //PRIVATE COST UTILITY FUNCTION
- int Date::checkDay(int testDay) const
- {
- static const int daysPerMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
- if(testDay > 0 && testDay <= daysPerMonth[month])
- {
- return testDay;
- }
- //determine whether testDay is valid for a specific month
- if(month == 2 && testDay == 29 && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
- {
- return testDay;
- }
- cout << "Invalid Day " << testDay << " was set to 1" << endl;
- return 1; //keep data in valid state
- }
- int Date::getMonth() const
- {
- return month;
- }
- Date& Date::setMonth(int m)
- {
- month = (m >= 1 && m <= 12) ? m : 1; //validates month, if valid set to m, else set to 1
- return *this;
- }
- int Date::getDay() const
- {
- return day;
- }
- Date& Date::setDay(int d)
- {
- day = checkDay(d);
- return *this;
- }
- int Date::getYear() const
- {
- return year;
- }
- Date& Date::setYear(int y)
- {
- year = y;
- return *this;
- }
- //void Date::operator= (const Date &a)
- //{
- // month = a.month;
- // day = a.day;
- // year = a.year;
- //
- //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement