// simpledate.cpp #include "simpledate.h" ////////////////////////////////////////////////////////////////////// // constructors // "default constructor"; constructs an invalid date SimpleDate::SimpleDate() { month = 0; day = 0; year = 0; } SimpleDate::SimpleDate(int newMonth, int newDay, int newYear) { month = newMonth; day = newDay; year = newYear; } ////////////////////////////////////////////////////////////////////// // methods // returns true iff (if and only if) this date is a valid calendar // date; *doesn't account for true start of Gregorian calendar bool SimpleDate::isValid() { if (month < 1 || month > NUM_MONTHS || // check months day < 1 || day > daysInMonth(month, year) || // check day year < 1) { // check year* return false; } return true; } // "accessor" method int SimpleDate::getMonth() { return month; } // "accessor" method int SimpleDate::getDay() { return day; } // "accessor" method int SimpleDate::getYear() { return year; } // returns true iff this date comes before the given date d bool SimpleDate::isBefore(SimpleDate d) { if (year == d.year) { if (month == d.month) { // same year, same month if control reaches here return (day < d.day); } else { // same year, different months if control reaches here return (month < d.month); } } // years are different if control reaches here return (year < d.year); } // returns number of days in given month in given year, accounting for // Februaries in leap years int SimpleDate::daysInMonth(int m, int y) { int days = monthLengths[m - 1]; // if leap year... if (year % 4 == 0 && (!(year % 100 == 0) || (year % 400 == 0))) { // ...February has 29 days if (m == 2) { return days + 1; } } return days; }