#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 2 valid values into a newborn date object, //or it throws an exception. date::date(int m, int d, int y) : year {y}, day {d} { 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 to day the sum of all the days in all the months before month m. for (int month {1}; month < m; ++month) { day += length[month]; } } void date::print() const { int d {day}; //d is probably too big int m {1}; for (; d > length[m]; ++m) { //Cut d down to size. d -= length[m]; } cout << m << "/" << d << "/" << year; } void date::next(int n) //Can change the date struct. { for (int i {0}; i < n; ++i) { next(); //Call the other next function, the one with no argument } } void date::next() //Move this date one day into the future. { if (day < 365) { ++day; } else { day = 1; //Advance into the next year. ++year; } } void date::prev(int n) { for (int i {0}; i < n; ++i) { prev(); } } void date::prev() //Move this date one day into the past. { if (day > 1) { --day; } else { day = 365; //Retreat into the previous year. --year; } }