1、Date.h
#pragma once
#include <iostream>
using namespace std;
class Date
{
//友元声明
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
public:
Date(int year = 1, int month = 1, int day = 1);
void Print();
//Date& operator=(const Date& d); //赋值重载
int GetMonthDay(int year, int month);
//总结:只读函数可以加const,内部不涉及修改成员的都是只读函数
bool operator<(const Date& d) const; //d1 就是 this,d2 就是 d
bool operator==(const Date& d) const;
bool operator<=(const Date& d) const;
bool operator>(const Date& d) const;
bool operator>=(const Date& d) const;
bool operator!=(const Date& d) const;
Date& operator+=(int day); //日期+天数
Date operator+(int day) const;
Date& operator-=(int day); //日期-天数
Date operator-(int day) const;
int operator-(const Date& d) const; //日期-日期
//++d1 -> d1.operator++()
Date& operator++();
//d1++ -> d1.operator++(0)
//【加一个int参数,进行占位,跟前置++构成函数重载进行区分】
//【本质:后置++调用,编译器进行特殊处理】
Date operator++(int i);
Date& operator--();
Date operator--(int i);
private:
//内置类型
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& out, const Date& d); //重载流插入
istream& operator>>(istream& in, Date& d); //重载流提取,不能加const修饰d,因为要输入值
2、Date.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include "Date.h"
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
//检查日期是否合法
if (month < 1 || month>12 || day<1 || day>GetMonthDay(year, month))
{
cout << "非法日期:";
//exit(-1);
}
}
void Date::Print()
{
cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
int Date::GetMonthDay(int year, int month)
{
//GetMonthDay会被重复调用,加一个static就不用每次调用都创建一个数组,节省了空间
static int monthArray[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 monthArray[month];
}
bool Date::operator<(const Date& d) //d1 就是 this,d2 就是 d
{
if (_year < d._year)
{
return true;
}
else if (_year == d._year && _month < d._month)
{
return true;
}
else if (_year == d._year && _month == d._month && _day < d._day)
{
return true;
}
else
{
return false;
}
}
//d1==d1
bool Date::operator==(const Date& d) //d1 就是 this,d2 就是 d
{
return (_year == d._year && _month == d._month && _day == d._day);
}
//d1<=d2
bool Date::operator<=(const Date& d) //d1 就是 this,d2 就是 d
{
return *this < d || *this == d; //*this就是d1,d就是d2
}
//d1>d2
bool Date::operator>(const Date& d)
{
return !(*this <= d); //>就是<=取反
}
//d1>=d2
bool Date::operator>=(const Date& d)
{
return !(*this < d); //>=就是<取反
}
//d1!=d2
bool Date::operator!=(const Date& d)
{
return !(*this == d);
}
Date& Date::operator+=(int day) //日期+天数
{
//若+=负数,等于-=正数
if (day < 0)
{
return *this -= (-day);
}
_day += day;
while (_day > GetMonthDay(_year, _month))
{
//月进位
_day -= GetMonthDay(_year, _month);
++_month;
//月满,年进位
if (_month == 13)
{
_year++;
_month = 1;
}
}
return *this; //this是指针,返回*this
}
Date Date::operator+(int day)
{
Date tmp(*this);
tmp += day;
return tmp;
//tmp._day += day;
//while (tmp._day > GetMonthDay(tmp._year, tmp._month))
//{
// //月进位
// tmp._day -= GetMonthDay(tmp._year, tmp._month);
// ++_month;
// //月满,年进位
// if (tmp._month == 13)
// {
// ++tmp._year;
// tmp._month = 1;
// }
//}
//return tmp;
}
//Date& Date::operator=(const Date& d)
//{
// if (this != &d) //屏蔽d1=d1的操作
// {
// this->_year = d._year;
// this->_month = d._month;
// this->_day = d._day;
// }
// return *this;
//}
Date& Date::operator-=(int day)
{
//若-=负数,等于+=正数
if (day < 0)
{
return *this += (-day);
}
_day -= day;
while (_day < 1)
{
_month--;
if (_month == 0)
{
_year--;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
Date Date::operator-(int day)
{
Date tmp(*this);
tmp -= day;
return tmp;
}
Date& Date::operator++()
{
*this += 1;
return *this;
}
Date Date::operator++(int i)
{
Date tmp = *this;
*this += 1;
return tmp;
}
Date& Date::operator--()
{
*this -= 1;
return *this;
}
Date Date::operator--(int i)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
int Date::operator-(const Date& d)
{
Date max = *this;
Date min = d;
int flag = 1;
if (max < min)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min != max) //算相差的天数
{
++min;
++n;
}
return n * flag;
}
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
istream& operator>>(istream& in, Date& d)
{
in >> d._year >> d._month >> d._day;
return in;
}
3、Test.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include "Date.h"
void Test1()
{
Date d1(2023, 7, 23);
d1.Print();
Date d2(2023, 8, 23);
d2.Print();
Date d3(2023, 13, 0);
d3.Print();
}
void Test2()
{
Date d1(2023, 7, 23);
d1.Print();
Date d2(2023, 8, 13);
d2.Print();
//拷贝构造,一个已经存在的对象去初始化另外一个要创建的对象
Date d3(d2); //默认拷贝构造
d3.Print();
//赋值,两个已经存在的对象进行拷贝
d1 = d2; //d1.operator=(d2);
d1.Print();
d1 = d2 = d3;
d1.Print();
}
void Test3()
{
Date d1(2023, 7, 27);
d1 += 2000;
d1.Print();
Date d2(2023, 7, 27);
Date ret = d2; //拷贝构造,不是赋值
/*Date ret;
ret = d2 + 2000;*/ //这是赋值
ret.Print();
}
void Test4()
{
Date d1(2023, 7, 27);
d1 -= 2000;
d1.Print();
d1 -= -200;
d1.Print();
}
void Test5()
{
Date d1(2023, 7, 27);
Date ret1 = d1++;
//Date ret1 = d1.operator++(0); //显式调用,此处的值传多少都无所谓
ret1.Print();
d1.Print();
Date ret2 = ++d1;
//Date ret2 = d1.operator++(); //显式调用
ret2.Print();
d1.Print();
}
void Test6()
{
Date d1(2002, 12, 14);
Date d2(2023, 7, 27);
cout << d2 - d1 << endl;
}
void Test7()
{
Date d1(2002, 12, 14);
Date d2(2023, 7, 30);
cout << d1 << d2;
Date d3;
cin >> d3;
cout << d3;
}
int main()
{
cout << "Test1:" << endl;
Test1();
cout << endl;
cout << "Test2:" << endl;
Test2();
cout << endl;
cout << "Test3:" << endl;
Test3();
cout << endl;
cout << "Test4:" << endl;
Test4();
cout << endl;
cout << "Test5:" << endl;
Test5();
cout << endl;
cout << "Test6:" << endl;
Test6();
cout << endl;
cout << "Test7:" << endl;
Test7();
return 0;
}
4、运行结果