在本节,我将通过实现日期类Date的实现来进一步阐释运算符重载的内容。
目录
一、Date.h
二、Date.cpp
三、test.cpp
一、Date.h
#include<iostream>
#include<cassert>
using namespace std;
class Date
{
public:// 获取某年某月的天数// 其为内联函数int GetMonthDay(int year, int month){static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };int day = days[month];if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))){day += 1;}return day;}// 全缺省的构造函数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);// ==运算符重载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);// 日期-日期 返回天数int operator-(const Date& d);//展示void Show(){cout << _year << "年" << _month << "月" << _day << "日" << endl;}
private:int _year;int _month;int _day;
};
二、Date.cpp
#include "Date.h"
// 全缺省的构造函数
Date::Date(int year, int month, int day)
{_year = year;_month = month;_day = day;
}
// 拷贝构造函数
// d2(d1)
Date::Date(const Date& d)
{_year = d._year;_month = d._month;_day = d._day;
}
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
// 注意内置类型连续赋值的特点,返回原对象的引用
Date& Date::operator=(const Date& d)
{_year = d._year;_month = d._month;_day = d._day;return *this;
}
// 析构函数
// 日期类可以不用析构函数,这里求全,所以还是写了
Date::~Date()
{cout << "窝被调用了" << endl;
}
// 日期+=天数
// 在该日期的基础上进行修改,需要返回该对象的引用,提高效率
Date& Date::operator+=(int day)
{assert(day >= 0);while (day + _day > GetMonthDay(_year, _month)){day -= GetMonthDay(_year, _month);_month++;if (_month == 12){_month = 1;_year++;}}_day += day;return *this;
}
// 日期+天数
// 不在该日期上修改,设置一个temp对象,对temp对象进行操作可以复用+=操作
Date Date::operator+(int day)
{assert(day >= 0);Date temp = *this;//原始写法/*while (day + _day > GetMonthDay(_year, _month)){day -= GetMonthDay(_year, temp._month);temp._month++;if (temp._month == 12){temp._month = 1;temp._year++;}}temp._day += day;*///实现了+=之后的写法temp += 60;return temp;
}
// 日期-天数
// 复用-=,减少代码量
Date Date::operator-(int day)
{assert(day>=0);Date temp=*this;//原始写法/* while(temp._day<day){day -= GetMonthDay(temp._year, temp._month);if(temp._month==1){temp._month=12;--temp._year;continue;}--temp._month;}temp._day -= day;*///实现了-=之后的写法temp -= day;return temp;
}
// 日期-=天数
// 有了+=的经验之后,可以先实现-=的操作,注意月份的转变
Date& Date::operator-=(int day)
{assert(day >= 0);while (_day < day){day -= GetMonthDay(_year, _month);if (_month == 1){_month = 12;--_year;continue;}--_month;}_day -= day;return *this;
}
// 前置++
// 前置++的效果是对对象自增后进行返回原对象的引用,得到的是自增后的对象
Date& Date::operator++()
{if (_day < GetMonthDay(_year, _month)){_day++;}else{_year= _month == 12 ? _year+1 : _year;_month = _month == 12 ? 1 : _month + 1;_day = 1;}return *this;
}
// 后置++
// 后置++的效果是对对象进行自增,然后返回自增前的对象。创建一个temp对象,对原对象进行自增后
// 返回temp对象的内容
Date Date::operator++(int)
{Date temp = *this;++*this;return temp;
}
// 后置--
// 同后置++
Date Date::operator--(int)
{Date temp = *this;--*this;return temp;
}
// 前置--
// 同前置++
Date& Date::operator--()
{if (_day == 1){if (_month == 1){_year--;_month = 12;}else{_month--;}_day = GetMonthDay(_year, _month);}else{_day--;}return *this;
}
// >运算符重载
// 依次比较,然后返回值
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){return false;}else{if (_day > d._day){return true;}else{return false;}}}return false;
}
// ==运算符重载
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 > d);
}
// <运算符重载
// 取反面的思想,<的反面是>=
bool Date::operator<(const Date& d)
{return !(*this >= d);
}
// <=运算符重载
// 复用<和==
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);
}
// 日期-日期 返回天数
// 日期追加的思想,让小日期追赶大日期,并且使计数器++
int Date::operator-(const Date& d)
{assert(d._year >= 0);int flag = 1;Date max = *this;Date min = d;if (*this < d){int flag = -1;max = d;min = *this;}int n = 0;while (min != max){++min;++n;}return n * flag;
}
三、test.cpp
#include"Date.h"
int main()
{Date d1;Date d2(2024, 3, 11);/*d1 = d2;Date d3 = d2 + 60;*//* d2 += 60;d2.Show();*//*d3.Show();d2 = d2 - 90;d2.Show();*//*++d2;d2.Show();*//*d1=d2++;d1.Show();*/d1 = d2--;//d1++;d1.Show();d2.Show();(++d2).Show();Date d4(2025, 2, 24);cout << (d1 <= d2) << endl;cout << (d4 - d2) << endl;return 0;
}
下节预告:类和对象初阶收尾