对基本的数据类型,都支持+-*/等操作,对于自定义类型,要实现这些操作就要自己编写函数。这时候,C++提供了运算符重载的语法,及简化了调用的代码,又增加了可读性!
运算符重载的特征:
- operator+合法的运算符 构成函数名(eg operator<);
- 运算符重载后,不能改变运算符的优先级/结合性/操作数个数。
下图就是改变了操作数的个数导致的错误:
注:5个C++不能重载的运算符:.* :: sizeof ?: .
实现日期类:
.h文件:
#pragma once
#include <stdlib.h>
#include <iostream>
#include <assert.h>
using namespace std;
class Date
{
public:
//构造函数
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
if (!IsValid())
assert(false);
}
//析构函数
~Date()
{}
//其它成员函数
int GetMonthDays(int year, int month);
bool IsValid();
void show();
Date& operator=(Date& d);
bool operator==(Date& d);
bool operator!=(Date& d);
bool operator>(Date& d);
bool operator<(Date& d);
bool operator>=(Date& d);
bool operator<=(Date& d);
Date operator+(int day);
Date& operator+=(int day);
Date operator-(int day);
Date& operator-=(int day);
int operator-(Date& d);//两日期相减
Date& operator++(); //前置
Date operator++(int); //后置
Date& operator--(); //前置
Date operator--(int); //后置
private:
int _year;
int _month;
int _day;
};
.cpp文件:
#include "Date.h"
int Date::GetMonthDays(int year, int month)
{
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 = 29;
return day;
}
bool Date::IsValid()
{
return _year >= 0 && _month > 0 && _month<13 && _day>0 && _day < GetMonthDays(_year, _day);
}
void Date::show()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
Date& Date::operator=(Date& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
bool Date::operator==(Date& d)
{
return _year == d._year&&_month == d._month&&_day == d._day;
}
bool Date::operator!=(Date& d)
{
return !(*this == d);
}
bool Date::operator>(Date& d)
{
if (_year > d._year || (_year == d._year&&_month > d._month) || (_year == d._year&&_month == d._month&&_day > d._day))
return true;
return false;
}
bool Date::operator<(Date& d)
{
return !(*this == d) && !(*this>d);
}
bool Date::operator>=(Date& d)
{
return !(*this < d);
}
bool Date::operator<=(Date& d)
{
return !(*this>d);
}
Date Date::operator+(int day)
{
if (day < 0)
return *this - (-day);
Date tmp(*this);
tmp._day += day;
while (tmp._day > GetMonthDays(tmp._year, tmp._month))
{
tmp._day -= GetMonthDays(tmp._year, tmp._month);
tmp._month++;
if (tmp._month > 12)
{
tmp._month = 1;
tmp._year++;
}
}
return tmp;
}
Date& Date::operator+=(int day)
{
*this = *this + day;
return *this;
}
Date Date::operator-(int day)
{
if (day < 0)
return *this + (-day);
Date tmp(*this);
tmp._day -= day;
while (tmp._day <= 0)
{
if (tmp._month == 1)
{
tmp._day += GetMonthDays(tmp._year, 12);
tmp._month = 12;
tmp._year--;
}
else
{
tmp._day += GetMonthDays(tmp._year, tmp._month - 1);
tmp._month--;
}
}
return tmp;
}
Date& Date::operator-=(int day)
{
*this = *this - day;
return *this;
}
int Date::operator-(Date& d)
{
Date max(*this);
Date min(d);
int flag = 1;
if (max < min)
{
max = d;
min = *this;
flag = -1;
}
int count = 0;
while (min < max)
{
++min;
++count;
}
return flag*count;
}
Date& Date::operator++() //前置
{
*this += 1;
return *this;
}
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;
}