注释比较详细,可以直接跑起来,直接上代码(vs2012 win7)
一、头文件
/**************
Date.h
***************/
#pragma once
#include <iostream>
using namespace std;
class Date
{
private:
int my_iYear;
int my_iMonth;
int my_iDay;
int GetOneMonthDays(int year, int month)const;//获取某个月的天数
public:
Date()
:my_iYear(1900)
,my_iMonth(1)
,my_iDay(1)
{
}
Date(int year, int month, int day);// 构造函数
Date(const Date& d); // 拷贝构造函数
~Date(){} // 析构函数
int GetYear()const; // 返回年份
int GetMonth()const; // 返回月份
int GetDay()const; // 返回天数
void Print()const; // 输出日期
bool IsLeapYear()const; // 判断当前对象年是否是闰年
bool IsLeapYear(const int y)const; // 判断指定年份是否是闰年
// 操作符重载部分
// 天数 + 日期
friend Date operator+(const int d, const Date date);
// 日期 + 天数
friend Date operator + (const Date date, const int d);
// 前置 ++
friend Date& operator ++ (Date& date);
// 后置 ++ 多一个int参数与前置区别
friend Date operator ++ (Date& date, int);
// 重载 +=
friend Date operator +=(Date& date, const int d);
// 日期 - 天数
friend Date operator - (const Date date, const int d);
// 天数 - 日期
friend Date operator - (const int d, const Date date);
// 前置 --
friend Date& operator -- (Date& date);
// 后置 --
friend Date operator -- (Date& date, int);
// 重载 -=
friend Date operator -=(Date& date, const int d);
// 日期 - 日期
friend int operator - (const Date a, const Date b);
// 重载比较操作符
friend bool operator< (const Date a, const Date b);
friend bool operator<= (const Date a, const Date b);
friend bool operator> (co