Date.cpp部分
#define _CRT_SECURE_NO_WARNINGS
#include"Date.h"
#include<assert.h>
Date::Date(int year, int month, int day)
{
assert(month > 0 && month < 13);
assert(day > 0 && day < 32);
_year = year;
_month = month;
_day = day;
}
//拷贝构造
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
//析构函数
Date::~Date()
{
_year = -1;
_month = -1;
_day = -1;
}
//赋值运算符重载
Date& Date::operator=(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
// 第一种方式: 先实现+= 再实现+
日期 += 天数
//Date& Date::operator+=(int day)
//{
// _day += day;
// while (_day > GetMonthDay(_year, _month))//判断日期是否大于本月天数
// {
// _day -= GetMonthDay(_year, _month);//减掉本月的天数
// ++_month;//月份加一 进入下一个月
// if (_month == 13)
// {
// _month = 1;//当月份到12时 重置一下
// _year++;
// }
// }
// return *this;
//}
日期 + 天数
// 先实现+= 在 + 中复用 +=
//Date Date::operator+(int day)
//{
// Date tmp = *this;
// tmp += day;
// return tmp;
//}
// 第二种方式: 先实现+ 再实现+=
Date Date::operator+(int day)
{
Date tmp = *this;
tmp._day = tmp._day + day;
while (tmp._day > GetMonthDay(tmp._year, tmp._month))
{
//先减掉本月的天数 ,再让月份加一 顺序不能乱!!!
tmp._day = tmp._day - GetMonthDay(tmp._year, tmp._month);
tmp._month++;
if (tmp._month == 13)
{
++tmp._year;
tmp._month = 1;
}
}
return tmp;
}
//先实现 + 在 += 中复用 +
Date& Date::operator+=(int day)
{
*this = *this + day;
return *this;
}
日期 - 天数
Date Date::operator-(int day)
{
Date tmp = *this;
tmp -= day;
return tmp;
}
日期 -= 天数
Date& Date::operator-=(int day)
{
_day = _day - day;
while (_day <= 0)//判断本月日期是否够
{
--_month;
//像上一个月借天数
if (_month == 0)//判断是否为1月,向上一年借天数
{
_year--;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
日期 - 日期 返回天数
int Date::operator-(const Date& d)
{
int flag = 1, count = 0;
Date max = *this;
Date min = d;
if (*this < d)//如果假设错误则交换。
{
flag = -1;
max = d;
min = *this;
}
while (min != max)
{
min++;
count++;
}
return count * flag;
}
//++前置
Date& Date::operator++()
{
//第一种写法
/**this = *this+1;
return *this;*/
//第二种
return *this += 1;
}
//后置++
Date Date::operator++(int)
{
Date tmp = *this;
*this += 1;
return tmp;
}
//--前置
Date& Date::operator--()
{
return *this-=1;
}
//后置--
Date Date::operator--(int)
{
Date tmp = *this;
*this-=1;
return tmp;
}
// 运算符重载 >
bool Date::operator>(const Date& d)
{
return !(*this <= d);
}
// 运算符重载 >=
bool Date::operator>=(const Date& d)
{
return !(*this < d);
}
//运算符重载 <
bool Date::operator<(const Date& d)
{
if (_year < d._year)
{
return true;
}
else if (_year == d._year)
{
if (_month < d._month)
{
return true;
}
else if (_month == d._month)
{
if (_day < d._day)
return true;
}
}
return false;
}
// 运算符重载 <=
bool Date::operator<=(const Date& d)
{
return (*this < d) || (*this == d);
}
// 运算符重载 ==
bool Date::operator==(const Date& d)
{
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
// 运算符重载 !=
bool Date::operator!=(const Date& d)
{
return !(*this == d);
}
Date.h部分
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:
//打印日期
void print()
{
cout << _year << "/" << _month << "/" << _day << endl;
}
//获取某年某月的日期的天数
//写在函数内部本身就是 iline
int GetMonthDay(int year, int month)
{
assert(month > 0 && month <= 12);
int MonthDay[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (month == 2
&& ((year % 4 == 0) && year % 100 != 0) || year % 400 == 0)
{
return 29;
}
return MonthDay[month];
}
//全缺省的构造函数
Date(int year = 2000, int month = 1, int day = 1);
//拷贝构造函数
//d2(d1)
Date(const Date& d);
//析构函数
~Date();
// 赋值运算符重载
// d2 = d3 ----》 d2.operator = (&d2 ,d3)
Date& operator=(const Date& d);
//日期 + 天数
Date operator+(int day);
//日期 += 天数
Date& operator+=(int day);
//日期 - 天数
Date operator-(int day);
//日期 -= 天数
Date& operator-=(int day);
//日期 - 日期 返回天数
int operator-(const Date& d);
//++前置
Date& operator++();
//后置++
Date operator++(int);
//--前置
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);
private:
int _year;
int _month;
int _day;
};
流插入与流提取的重载
//流插入
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
//流提取
istream& operator>>(istream& in, Date& d)
{
cout << "请输入日期:>";
cin >> d._year >> d._month >> d._day;
while (1)
{
if (!d.CheckDay())
{
cout << "日期输入错误,请重新输入:>" << endl;
cin >> d._year >> d._month >> d._day;
}
else
{
break;
}
}
return in;
}
流提取时类不能加 const 否则无法输入数据,而流插入是类加入 const 为了防止显示数据遭到修改。
注意:流插入和流提取不能够在类的内部重载,这里采用声明和定义分离的做法。
//友元声明
friend ostream& operator<<(ostream & out, const Date & d);
friend istream& operator>>(istream& in, Date& d);