目录
1、前置++与后置++的成员函数
1、后置++的占位参数
2、前置++与后置++的成员函数实现
1、前置++与后置++的区别
2、实现
2、类中不仅可以调用成员变量,还可以调用成员函数
3、类中运算符重载的复用
对于>、>=、==、!=、<、<=,这六个运算符重载的实现,可以只实现>、==,剩下的进行复用
例如:对日期类:
//d1>d2
bool Date::operator>(const Date& d)
{
if (_year > d._year)
{
return true;
}
else if (_year == d._year && _month > d._month)
{
return true;
}
else if (_year == d._year && _month == d._month && _day > d._day)
{
return true;
}
else
{
return false;
}
}
//d1==d2
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);
}
bool Date::operator!=(const Date& d)
{
return !(*this == d);
}
4、Date类的源码
1、Date.h
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:
Date(int year = 0, int month = 1, int day = 1);
void Print();
int GetMonthDay(int year,int 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++();
Date operator++(int);
Date& operator--();
Date operator--(int);
int operator-(const Date& d);
private:
int _year;
int _month;
int _day;
};
2、Date.cpp
#include"Date.h"
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
if (!(_year >= 0 && (_month > 0 && _month < 13) &&
(_day > 0 && _day <= GetMonthDay(_year, _month))))
{
cout << "非法日期->";
Print();//类里面不仅可以调用成员变量,还可以调用成员函数
//this->Print();
}
}
void Date:: Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
int Date::GetMonthDay(int year,int month)
{
static int MonthDayArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int day = MonthDayArray[month];
if (month==2&&(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
day += 1;
}
return day;
}
//d1>d2
bool Date::operator>(const Date& d)
{
if (_year > d._year)
{
return true;
}
else if (_year == d._year && _month > d._month)
{
return true;
}
else if (_year == d._year && _month == d._month && _day > d._day)
{
return true;
}
else
{
return false;
}
}
//d1==d2
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);
}
bool Date::operator!=(const Date& d)
{
return !(*this == d);
}
//d1+=10
Date& Date::operator+=(int day)
{
_day += day;
while (_day >GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
++_month;
if (_month == 13)
{
_month = 1;
_year++;
}
}
return *this;
}
//d1+10
Date Date::operator+(int day)
{
Date ret(*this);
ret += day;
return ret;
}
//d1-=10
Date& Date::operator-=(int day)
{
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
--_year;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
Date Date::operator-(int day)
{
Date ret(*this);
ret -= day;
return ret;
}
//前置++
Date& Date::operator++()
{
*this += 1;
return *this;
}
//后置++
Date Date::operator++(int)
{
Date ret(*this);
*this += 1;
return ret;
}
//d2-d1
int Date::operator-(const Date& d)
{
Date max = *this;
Date min = d;
int flag = 1;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int count = 0;
while (min != max)
{
++min;
++count;
}
return count * flag;
}
Date& Date::operator--()
{
*this -= 1;
return *this;
}
Date Date::operator--(int)
{
Date ret(*this);
*this -= 1;
return ret;
}
3、test.cpp
#include"Date.h"
void Test1()
{
Date d1;
Date d2(2022, 10, 18);
Date d3(2022, 10, 19);
d1.Print();
d2.Print();
cout<<(d2 > d3)<<endl;
cout << (d2 == d3) << endl;
cout << (d2 >= d3) << endl;
cout << (d2 < d3) << endl;
cout << (d2 <= d3) << endl;
cout << (d2 == d3) << endl;
}
void Test2()
{
Date d1(2022, 10, 18);
d1.Print();
Date d2(d1);
d2 += 10;
d2.Print();
Date d3(d1);
d3 += 20;
d3.Print();
Date d4(d1);
d4 += 30;
d4.Print();
Date d5(d1);
d5 += 370;
d5.Print();
Date d6(d1);
Date d7 = d6 + 10;
d7.Print();
}
void Test3()
{
Date d1(2022, 10, 18);
d1.Print();
Date d2(d1);
d2 -= 10;
d2.Print();
Date d3(d1);
d3 -= 20;
d3.Print();
Date d4(d1);
d4 -= 30;
d4.Print();
Date d5(d1);
d5 -= 370;
d5.Print();
Date d6(d1);
Date d7 = d6 - 10;
d7.Print();
Date d8(d1);
++d8;
d8.Print();
}
int main()
{
//Test1();
//Test2();
Test3();
return 0;
}