实现日期类
- [1. 代码](#1. 代码)
- [2. 解析](#2. 解析)
-
- [2.1 关系运算符](#2.1 关系运算符)
- [2.2 赋值运算符](#2.2 赋值运算符)
- [2.3 单目运算符](#2.3 单目运算符)
- [2.4 重载操作流](#2.4 重载操作流)
1. 代码
cpp
// 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 = 1900, int month = 1, int day = 1);
void Print();
// 获得每个月的天数
static int GetMonthDays(int year, int month)
{
int days_array[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;
}
else
{
return days_array[month];
}
}
// 重载操作符
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);
Date& operator+=(int day);
Date operator+(int day);//为什么不引用?
Date& operator-=(int day);
Date operator-(int day);
Date& operator++();// 不传int的是前置
Date operator++(int);// 传int的是后置
Date& operator--();
Date operator--(int);
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);
cpp
// Date.cpp
#include"Date.h"
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
void Date::Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
// 重载<
bool Date::operator<(const Date d)
{
// 逻辑是 先比年 年相同则比月;比较月 月相同则比日;日
if (this->_year < d._year)
{
return true;
}
else if (_year == d._year)
{
if (_month < d._month)
{
return true;
}
else if (_month = d._month)
{
return _day < d._day;
}
}
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); // this指针解引用后才是类类型
}
// 重载>=
bool Date::operator>=(const Date d)
{
return !(*this < d);
}
// 重载!=
bool Date::operator!=(const Date d)
{
return !(*this == d);
}
// 重载+=
// 用+=去实现+会比反过来好
Date& Date::operator+=(int day)
{
if (day < 0)
{
return *this -= -day;
}
// 逻辑是从日开始加 超过月对应的天数月就加1 日减去对应天数
_day += day;
while (_day > GetMonthDays(_year, _month))
{
_day -= GetMonthDays(_year, _month);
_month++;
if (_month > 12)
{
_year++;
_month = 1;
}
}
return *this;
}
// 重载+
Date Date::operator+(int day)
{
Date tmp = *this;// 先找个中间变量
tmp += day;
return tmp;
}
// 重载-=
Date& Date::operator-=(int day)
{
_day -= day;
while (_day < 1)
{
_month--;
if (_month < 1)
{
_year--;
_month = 12;
}
_day += GetMonthDays(_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)
{
Date tmp = *this;
*this += 1;
return tmp;
}
Date& Date::operator--()
{
*this -= 1;
return *this;
}
Date Date::operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
istream& operator>>(istream& in, Date& d)
{
cout << "请依次输入年月日:>";
in >> d._year >> d._month >> d._day;
return in;
}
cpp
// Test.cpp
#include"Date.h"
int main()
{
Date d1(2025, 4, 28);
Date d2(2024, 4, 28);
Date d3(2024, 4, 14);
cout << "d1 < d2:" << (d1 < d2) << endl;
cout << "d1 > d2:" << (d1 > d2) << endl;
cout << "d1 <= d2:" << (d1 <= d2) << endl;
cout << "d1 >= d2:" << (d1 >= d2) << endl;
cout << "d1 == d2:" << (d1 == d2) << endl;
cout << "d1 != d2:" << (d1 != d2) << endl;
cout << "--------------" << endl;
Date d4 = d3;
d4 += 30000;
d4.Print();
cout << "--------------" << endl;
Date d5 = d3 + 50;
d5.Print();
cout << "--------------" << endl;
Date d6 = d3;
d6 -= 5000;
d6.Print();
cout << "--------------" << endl;
Date d7(2025, 4, 29);
d7++;
d7.Print();
Date d8 = d1;
++d8;
d8.Print();
Date d9;
cin >> d9;
cout << d9;
return 0;
}

2. 解析
2.1 关系运算符
cpp
// Date.h
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);
在任意类里需要实现这些操作符都可以用这种方法:实现 >和= 或者实现 <和=
其他的运算符都可以用这实现
2.2 赋值运算符
cpp
// Date.h
Date& operator+=(int day);
Date operator+(int day);
Date& operator-=(int day);
Date operator-(int day);
// Date.cpp
// 重载+=
Date& Date::operator+=(int day)
{
if (day < 0)
{
return *this -= -day;
}
_day += day;
while (_day > GetMonthDays(_year, _month))
{
_day -= GetMonthDays(_year, _month);
_month++;
if (_month > 12)
{
_year++;
_month = 1;
}
}
return *this;
}
// 重载+
Date Date::operator+(int day)
{
Date tmp = *this;
tmp += day;
return tmp;
}
// 重载-=
Date& Date::operator-=(int day)
{
_day -= day;
while (_day < 1)
{
_month--;
if (_month < 1)
{
_year--;
_month = 12;
}
_day += GetMonthDays(_year, _month);
}
return *this;
}
// 重载-
Date Date::operator-(int day)
{
Date tmp = *this;
tmp -= day;
return tmp;
}
用+=和-=去实现+和-更好
先实现+=比先实现+效率高,区别就在于二者的逻辑上。+=实现在原对象上,+则是实现在另一个对象上。这也解释了为什么一个是引用一个是传值
C++ -- 类和对象(中2)-- 引用和重载
2.3 单目运算符
cpp
// Date.h
Date& operator++();// 不传int的是前置
Date operator++(int);// 传int的是后置
Date& operator--();
Date operator--(int);
// Date.cpp
/ 重载++ --
Date& Date::operator++()
{
*this += 1;
return *this;
}
Date Date::operator++(int)
{
Date tmp = *this;
*this += 1;
return tmp;
}
Date& Date::operator--()
{
*this -= 1;
return *this;
}
Date Date::operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
函数重载和运算符重载有什么关系?二者没啥关系,但多个运算符重载之间可以构成函数重载。知道这个后就能明白接下来我要说的为什么一个无参数一个有参数int。
为了区分前置和后置,规定给后置添加参数int,只需要写个int进去就行。
你问为什么是int不是char?打个电话问问本贾尼吧。
2.4 重载操作流
cpp
// Date.h
class Date{
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
};
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);
// Date.cpp
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
istream& operator>>(istream& in, Date& d)
{
cout << "请依次输入年月日:>";
in >> d._year >> d._month >> d._day;
return in;
}
以此解决这些问题:为什么定义在全局?friend是什么?为什么加const?
为什么定义在全局? ------我们知道,参数顺序和操作数顺序相同,如果定义为成员函数,在调用的时候就得这么写
cpp
// d1.operator(cout);
d1 << cout;
因为有隐藏参数this指针,但若写在全局则不会有这个问题。
friend是什么?------友元关键字,写在类里的什么地方都可以,代表可以访问私有成员,保护程序安全。
为什么加const?------编译器不让传值。