#include //for cout #include //for the functions time and localtime #include "date.h" using 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 }; date::date(int m, int d, int y) : day(365 * y + d - 1) { for (int month {1}; month < m; ++month) { day += length[month]; } } date::date() //today's date { const time_t t {time(nullptr)}; const tm *const p = localtime(&t); day = 365 * (p->tm_year + 1900) + p->tm_mday - 1; for (int month {1}; month < p->tm_mon + 1; ++month) { day += length[month]; } } ostream& operator<<(ostream& ost, const date& d) { const int year {d.day / 365}; int dayOfYear {d.day % 365 + 1}; //1 to 365 inclusive int month {1}; for (; dayOfYear > date::length[month]; ++month) { dayOfYear -= date::length[month]; } ost << month << "/" << dayOfYear << "/" << year; return ost; } //The last two statements in the above friend function can be combined to //return ost << month << "/" << dayOfYear << "/" << year;