📄 date.h
字号:
////////////////////////////////////////
// File Name: Date.h
////////////////////////////////////////
#ifndef DATE_H
#define DATE_H
class Date
{
protected:
static const int dys[];
long int ndays; // days inclusive since Jan 1,1 (1/1/1==1)
public:
// Default and initializing constructor.
Date(int mo = 0, int da = 0, int yr = 0)
{ SetDate(mo, da, yr); }
// Copy constructor.
Date(const Date& dt)
{ *this = dt; }
// Destructor.
virtual ~Date() {}
// Overloaded assignment operator.
Date& operator=(const Date& dt)
{ ndays = dt.ndays; return *this; }
// Overloaded arithmetic operators.
Date operator+(int n) const
{ Date dt(*this); dt += n; return dt; }
Date operator-(int n) const
{ Date dt(*this); dt -= n; return dt; }
Date& operator+=(int n)
{ ndays += n; return *this; }
Date& operator-=(int n)
{ ndays -= n; return *this; }
Date& operator++() // prefix
{ ++ndays; return *this; }
Date operator++(int) // postfix
{ Date dt(*this); dt.ndays++; return dt; }
Date& operator--() // prefix
{ --ndays; return *this; }
Date operator--(int) // postfix
{ Date dt(*this); dt.ndays--; return dt; }
long int operator-(const Date& dt) const
{ return ndays-dt.ndays; }
// --- overloaded relational operators
bool operator==(const Date& dt) const
{ return ndays == dt.ndays; }
bool operator!=(const Date& dt) const
{ return ndays != dt.ndays; }
bool operator< (const Date& dt) const
{ return ndays < dt.ndays; }
bool operator> (const Date& dt) const
{ return ndays > dt.ndays; }
bool operator<=(const Date& dt) const
{ return ndays <= dt.ndays; }
bool operator>=(const Date& dt) const
{ return ndays >= dt.ndays; }
// Getter and setter functions.
void SetDate(int mo, int da, int yr);
void SetMonth(int mo)
{ *this = Date(mo, GetDay(), GetYear()); }
void SetDay(int da)
{ *this = Date(GetMonth(), da, GetYear()); }
void SetYear(int yr)
{ *this = Date(GetMonth(), GetDay(), yr); }
void GetDate(int& mo, int& da, int& yr) const;
int GetMonth() const;
int GetDay() const;
int GetYear() const;
// Display method.
virtual void Display() const;
// Test for leap year.
bool IsLeapYear(int yr) const
{ return ((yr % 4)==0 && (yr % 1000)!=0); }
bool IsLeapYear() const
{ return IsLeapYear(GetYear()); }
};
// Overloaded (int + Date)
inline Date operator+(int n, const Date& dt)
{
return dt + n;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -