#include //for cout #include "date.h" using namespace std; //because invalid_argument and cout belong to namespace std const int date::length[] { 0, //dummy, so that January will have subscript 1 31, //January 28, //February. Pretend there are no leap years. 31, //March 30, //April 31, //May 30, //June 31, //July 31, //August 30, //September 31, //October 30, //November 31 //December }; //The constructor installs 1 valid value into a newborn date object, //or it throws an exception. date::date(int m, int d, int y) : day {365 * y + d - 1} { if (m < 1 || m > 12) { throw invalid_argument("bad month"); } if (d < 1 || d > length[m]) { throw invalid_argument("bad day of the month"); } //Add on all the days in all the months before month m. for (int month {1}; month < m; ++month) { day += length[month]; } } void date::print() const //Can't change the date object. { const int year {day / 365}; int dayOfYear {day % 365 + 1}; //in range 1 to 365 inclusive int m {1}; for (; dayOfYear > length[m]; ++m) { dayOfYear -= length[m]; } cout << m << "/" << dayOfYear << "/" << year; } void date::next(int n) //Move this date n days into the future. { day += n; //means day = day + n } void date::next() //Move this date one day into the future. { ++day; } void date::prev(int n) { day -= n; //means day = day - n } void date::prev() //Move this date one day into the past. { --day; }