类和函数的声明部分:
#include<iostream>
using namespace std;
class Date
{
friend void print_date(int year,int month);//打印日历
friend ostream& operator<<(ostream& _cout, const Date& d);
friend istream& operator>>(istream& _cin, Date& d);
public:
Date(int year,int month,int day);//构造函数
Date(const Date& d);//拷贝构造函数
Date& operator=(const Date& d);
Date operator+(int day);
Date& operator++();
Date operator++(int);
Date operator-(int day);
int operator-(/*const*/ Date& d);
Date& operator--();
Date operator--(int);
bool operator>(const Date& d);
bool operator<(const Date& d);
bool operator==(const Date& d);
bool operator!=(const Date& d);
bool operator>=(const Date& d);
bool operator<=(const Date& d);
int days_everymonth(int year,int month);//所求月的天数
Date& display(const Date& d);//打印某天日期
private:
int _year;
int _month;
int _day;
};
bool judge_leap(int year);//判断是否闰年
void print_date(int year,int month);//打印日期
int week_firstday(int year,int day);//计算要求月数的第一天是星期几
函数实现:
ostream& operator<<(ostream& _cout, const Date& d)
{
_cout<<d._year<<"--"<<d._month<<"--"<<d._day;
return _cout;
}
istream& operator>>(istream& _cin, Date& d)
{
_cin>>d._year>>d._month>>d._day;
return _cin;
}
Date::Date(int year = 2016,int month = 1,int day = 1)
:_year(year)
,_month(month)
,_day(day)
{
if(day