#ifndef DaTe_H #define DaTe_H // Oldham, Jeffrey D. // 2000Apr28 // CS3352 // Store a Year and Month // To the user, months are numbered 1 = Jan, ..., 12 = Dec. // Only years after January 0 are represented. (Of course, year 0 did // not exist.) // To the implementer, months are numbered 0 = Jan, ..., 11 = Dec. class Date { public: Date(void) : mon(0) {} Date(const unsigned long y, const unsigned long m) : mon(m-1 + y * 12) {} unsigned long year(void) const { return mon / 12; } unsigned long month(void) const { return 1 + (mon % 12); } // Addition friend Date operator+(const Date & d, const unsigned long mos) { return Date(d.mon + mos); } friend Date operator+(const unsigned long mos, const Date & d1) { return d1 + mos; } // Subtraction friend Date operator-(const Date & d, const unsigned long mos) { return Date(d.mon - mos); } // Increment and Decrement Operators Date& operator++(void) { // prefix ++mon; return *this; } Date operator++(int) { // postfix Date answer = *this; ++mon; return answer; } Date& operator--(void) { // prefix if (mon != 0) --mon; return *this; } Date operator--(int) { // postfix Date answer = *this; if (mon != 0) --mon; return answer; } // Comparison Operators friend bool operator<(const Date & d1, const Date & d2) { return d1.mon < d2.mon; } friend bool operator>(const Date & d1, const Date & d2) { return d2 < d1; } friend bool operator==(const Date & d1, const Date & d2) { return !(d1 < d2) && !(d2 < d1); } friend bool operator!=(const Date & d1, const Date & d2) { return !(d1 == d2); } friend bool operator<=(const Date & d1, const Date & d2) { return !(d1 > d2); } friend bool operator>=(const Date & d1, const Date & d2) { return !(d1 < d2); } #ifdef DEBUG friend ostream& operator<<(ostream& out, const Date & d) { return out << d.mon/12 << ' ' << 1+(d.mon%12); } #endif // DEBUG private: typedef unsigned long ul; ul mon; // store year and month as number of // months since January, year 0 (which did not // exist :( ) Date(const unsigned long m) : mon(m) {} }; #endif // DaTe_H