四.基本运算符重载【前置++,后置++等】
1.机制说明:
1.如何设置返回类型?
前置的是【先赋值后使用】:返回的是本身(Date&接收)(引用提高效率)
后置的是【先使用后赋值】:返回的是临时变量(Date接收)(不用引用,因为临时变量出作用域即销毁,引用会变成野引用)
2.如何在定义与声明中区分前后置?
增加参数int,构成函数重载
2.代码展示:
类内声明:
//增加这个int参数不是为了接收具体的值,仅仅是占位,跟前置++构成重载 Date& operator++(); Date operator++(int); Date& operator--(); Date operator--(int);
.c内实现:
// 前置++ Date& Date::operator++() { *this += 1; return *this; } // 后置++ // 增加这个int参数不是为了接收具体的值,仅仅是占位,跟前置++构成重载 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; }
五. 减法的重载(日期-日期)
技巧:
- 预设大小:得以计算绝对值
- 预设flag:得以实现最终结果
.c文件实现:
int Date::operator-(const Date& d) const { Date max = *this; Date min = d; int flag = 1; if (*this < d) { max = d; min = *this; flag = -1; } int n = 0; while (min != max) { ++min; ++n; } return n * flag; }
六.完整代码实现
头文件:
#pragma once #include<iostream> #include<assert.h> 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() const { cout << _year << "-" << _month << "-" << _day << endl; } Date(const Date& d) // 错误写法:编译报错,会引发无穷递归 { _year = d._year; _month = d._month; _day = d._day; } bool operator<(const Date& x) const; bool operator==(const Date& x) const; bool operator<=(const Date& x) const; bool operator>(const Date& x) const; bool operator>=(const Date& x) const; bool operator!=(const Date& x) const; int GetMonthDay(int year, int month); // d1 + 100 Date& operator+=(int day); Date operator+(int day) const; Date& operator-=(int day); Date operator-(int day) const; Date& operator++(); Date operator++(int); Date& operator--(); Date operator--(int); int operator-(const Date& d) const; // 流插入不能写成成员函数? // 因为Date对象默认占用第一个参数,就是做了左操作数 // 写出来就一定是下面这样子,不符合使用习惯 //d1 << cout; // d1.operator<<(cout); //void operator<<(ostream& out); /*int GetYear() { return _year; }*/ private: int _year; int _month; int _day; }; ostream& operator<<(ostream& out, const Date& d); istream& operator>>(istream& in, Date& d);
.c文件:
#include "Date.h" Date::Date(int year, int month, int day) { if (month > 0 && month < 13 && day > 0 && day <= GetMonthDay(year, month)) { _year = year; _month = month; _day = day; } else { cout << "非法日期" << endl; assert(false); } } bool Date::operator<(const Date& x) const { if (_year < x._year) { return true; } else if (_year == x._year && _month < x._month) { return true; } else if (_year == x._year && _month == x._month && _day < x._day) { return true; } return false; } bool Date::operator==(const Date& x) const { return _year == x._year && _month == x._month && _day == x._day; } // 复用 // d1 <= d2 bool Date::operator<=(const Date& x) const { return *this < x || *this == x; } bool Date::operator>(const Date& x) const { return !(*this <= x); } bool Date::operator>=(const Date & x) const { return !(*this < x); } bool Date::operator!=(const Date& x) const { return !(*this == x); } int Date::GetMonthDay(int year, int month) { static int daysArr[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; //if (((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) && month == 2) if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) { return 29; } else { return daysArr[month]; } } 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; } // d1 + 100 Date Date::operator+(int day) const { 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); ++tmp._month; if (tmp._month == 13) { ++tmp._year; tmp._month = 1; } } return tmp; */ } Date& Date::operator-=(int day) { if (day < 0) { return *this += -day; } _day -= day; while (_day <= 0) { --_month; if (_month == 0) { _month = 12; --_year; } _day += GetMonthDay(_year, _month); } return *this; } Date Date::operator-(int day) const { Date tmp = *this; tmp -= day; return tmp; } // 前置++ Date& Date::operator++() { *this += 1; return *this; } // 后置++ // 增加这个int参数不是为了接收具体的值,仅仅是占位,跟前置++构成重载 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; } // d1 - d2; int Date::operator-(const Date& d) const { Date max = *this; Date min = d; int flag = 1; if (*this < d) { max = d; min = *this; flag = -1; } int n = 0; while (min != max) { ++min; ++n; } return n * flag; } //void Date::operator<<(ostream& out) //{ // out << _year << "年" << _month << "月" << _day << "日" << endl; //} // 21:20继续 ostream& operator<<(ostream& out, const Date& d) { out << d._year << "年" << d._month << "月" << d._day << "日" << endl; return out; } istream& operator>>(istream& in, Date& d) { int year, month, day; in >> year >> month >> day; if (month > 0 && month < 13 && day > 0 && day <= d.GetMonthDay(year, month)) { d._year = year; d._month = month; d._day = day; } else { cout << "非法日期" << endl; assert(false); } return in; }