小工具知识网

当前位置: 首页 > 行业知识 > 【C++实战项目】Date日期类 --- 运算符重载的深入探索

行业知识

【C++实战项目】Date日期类 --- 运算符重载的深入探索

2026-11-22 06:48:26 来源:互联网转载
���池俊:个人主页

个人专栏:✅C++那些事儿 ✅Linux技术宝典

此去关山万里,定不负云起之望


文章目录

    • 引言
    • 一、为什么需要运算符重载?
    • 二、日期类的实现
      • 1. 基本框架
      • 2. 预备工作
      • 3. Date 类中六大默认成员函数的使用
        • 3.1 全缺省的构造函数
        • 3.2 拷贝构造函数
        • 3.3 析构函数
        • 3.4 赋值运算符重载
        • 3.5 const 成员函数
        • 3.6 取地址操作符重载和const取地址操作符重载
        • 4. 运算符重载
          • 4.1 += 和 + 运算符重载
          • 4.2 -= 和 - 运算符重载
          • 4.3 前置++ 和 后置++ 运算符重载
          • 4.4 前置-- 和 后置-- 运算符重载
          • 4.5 > 和 == 运算符重载
          • 4.6 >= 、
          • 5. 日期类源码
            • 5.1 Date.h文件
            • 5.2 Date.cpp 文件
            • 总结

              引言

              在C++编程中,运算符重载是一种强大的功能,它允许我们为自定义的数据类型定义运算符的行为。这种特性在创建像日期(Date)这样的类时尤其有用,因为它允许我们更自然、更直观地操作这些类的实例。通过日期类我们还能够实现一个简单的日期时间计数器的功能,想必大家都很期待接下来的内容。

              在本篇博客中,我们将深入探索如何为Date类重载运算符,并了解其中的一些最佳实践和潜在陷阱。

              注:Date.h文件下放日期类的声明代码,Date.c文件下放日期类的实现代码


              一、为什么需要运算符重载?

              运算符重载可以让我们使用更直观、更自然的方式来操作日期。

              在创建日期类时,我们可能希望执行如下操作:

              • 对两个日期进行加法运算以得到一个新的日期(例如,将5天加到今天的日期上)。
              • 比较两个日期以确定哪个日期更早或更晚。
              • 从一个日期中减去另一个日期以得到它们之间的时间差。
              • 等等…

                为了实现这些操作,我们需要为Date类重载相应的运算符,如+、-、public:// 获取某年某月的天数int GetMonthDay(int year, int month) const;// 检查日期是否合法bool CheckInvalid();// 全缺省的构造函数Date(int year = 1900, int month = 1, int day = 1);// 拷贝构造函数// d2(d1)Date(const Date& d);// 赋值运算符重载// d2 = d3 - d2.operator=(&d2, d3)Date& operator=(const Date& d);// 析构函数~Date();// ======== 有关日期计算的运算符重载 ========// 日期+=天数Date& operator+=(int day);// 日期+天数Date operator+(int day);// 日期-天数Date operator-(int day);// 日期-=天数Date& operator-=(int day);// 前置++Date& operator++();// 后置++Date operator++(int);// 后置--Date operator--(int);// 前置--Date& operator--();// =========== 比较运算符的重载 ==========// >运算符重载bool operator>(const Date& d) const;// ==运算符重载bool operator==(const Date& d) const;// >=运算符重载bool operator >= (const Date& d) const;// cout static int days[13] = { 0,31,28,31,30,31,30,31,30,31,31,30,31 };if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))){return 29;}return days[month];}// 检查日期是否合法bool Date::CheckInvalid(){if (_year return false;}return true;}_year = year;_month = month;_day = day;if (!CheckInvalid()){cout _year = d._year;_month = d._month;_day = d._day;}//cout if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;}cout return this;//return nullptr;}// const取地址操作符重载const Date* operator&()const{return this;//return nullptr;}_day += day;while (_day GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);if (_month == 12){_month = 1;_year++;}else{_month++;}}return *this;}// 日期+天数Date Date::operator+(int day){Date temp = *this; // 调用拷贝构造,因为temp不是一个已存在的对象temp += day;return temp;}Date temp = *this;temp -= day;return temp;}// 日期-=天数Date& Date::operator-=(int day){while (day = _day){day -= _day;if (_month == 1){_month = 12;_year--;_day = GetMonthDay(_year, _month);}else{_month--;_day = GetMonthDay(_year, _month);}}_day -= day;return *this;}*this += 1;return *this;}// 后置++Date Date::operator++(int){Date temp = *this;*this += 1;return temp;}Date temp = *this;*this -= 1;return temp;}// 前置--Date& Date::operator--(){*this -= 1;return *this;}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) const{return _year == d._year&& _month == d._month&& _day == d._day;}return *this d || *this == d;}// return !(*this = d);}// return !(*this d);}// !=运算符重载bool Date::operator != (const Date& d) const{return !(*this == d);}int flag = 1;Date maxDate = *this;Date minDate = d;if (*this

上一篇:背包服务器怎么开机的?

下一篇:熊猫服务器怎么注册不了?