行业知识
【C++奇妙冒险】日期类Date的实现
2026-11-06 15:45:43
来源:互联网转载
文章目录
- 前言
- 日期类Date的接口设计
- 构造函数和打印函数
- 获取日期并判断日期是否合法
- 日期类的大小比较关系
- = 运算符重载 判断大于等于
- != 运算符重载 不等于
- 日期类计算
- 日期+=天数
- 日期+天数
- 日期-=天数
- 日期-天数
- 前置++
- 后置++
- 前置--
- 后置--
- 日期-日期
- 完整代码
- Date.h
- Date.cpp
- Test.cpp
前言
日期类Date的接口设计
我们把函数的声明放到类中,定义在类的外边,实现声明与定义分离
以下是日期类中所包含的成员函数和成员变量
️Date.h
class Date{public:// 判断日期是否合法bool CheckInvalid() const;// 构造函数Date(int year = 1, int month = 1, int day = 1);// 日期类的大小关系比较bool operator=(const Date& d) const;bool operator==(const Date& d) const;bool operator!=(const Date& d) const;// d1 + 100Date& operator+=(int day);Date operator+(int day) const;// d1 - 100Date operator-(int day) const;Date& operator-=(int day);// ++d1Date& operator++();// 特殊处理:解决语法逻辑不自洽,自相矛盾的问题// d1++// 为了跟前置++区分,强行增加一个int形参,够成重载区分Date operator++(int);Date operator--(int);Date& operator--();// d1 - d2int operator-(const Date& d) const;// 本质就是inlineint GetMonthDay(int year, int month) const{assert(month > 0 && month
上一篇:ftp服务器怎么建立多个账户?
下一篇:世界服务器连不上怎么办?