C++运算符重载
在C++中,预定义的运算符只能进行基本数据类型的操作。而对于用户的自定义类型,不能使用这些预定义的运算符来操作。
如何实现两个对象之间的运算?这时候需要用户编写程序来说明运算符的功能。使用运算符的重载可以为已有的运算符赋予多重含义,使同一个运算符能够作用于不同类型的数据导致不同类型的行为。
运算符重载是为了增强程序的可读性,只能重载已经存在的运算符。
特征:1)函数的构成:函数类型operator+ 合法的运算符(形参表)
Operator是定义运算符的关键字。运算符是重载的运算符名称。
例如:重载小于运算符“<”:bool operator<(const Date& d){函数体}
2)运算符被重载后,运算符的优先级/结合性/操作数个数保持不变。
3)运算符重载是根据新类型的需要对原因运算符的改造,与原有运算符的功能类似。
C++具有五个不能重载的运算符:1).* 2) 作用域运算符:: 3)sizeof 4)条件运算符?: 5)成员运算符(.)
运算符重载的形式有两种:重载为类的成员函数,重载为类的友元函数。
bool operator<(const Date& d){函数体}
frend bool operator<(const Date& d){函数体}
赋值运算符的重载:
拷贝构造函数是创建的对象,使用一个已有对象来初始化这个准备创建的对象。
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
Date d1(2018,3,25);//已有的对象d1
Date d2(d1);//正在创建的对象d2
赋值运算符是对一个已有的对象进行赋值。
Date& operator=(const Date& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
Date d3;//已经创建好的对象d3
d3 = d1;//对d3进行赋值。此时d1,d3是Date类的两个对象。
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include<Windows.h>
#include<assert.h>
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
:_year(year)
, _month(month)
, _day(day)
{
if (IsInvalid()) // this->IsInvalid(this)
{
assert(false);
//cout<<"非法日期"< //exit(-1);
}
}
bool IsInvalid()
{
int days = GetMonthDay(_year, _month);
if (_year<1900
||_month<1
||_month>12
||_day<1
|| _day>days)
{
return true;
}
return false;
}
bool IsLeapYear(int year)
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
return true;
}
return false;
}
int GetMonthDay(int year, int month)
{
if (year < 1900 || month<1 || month>12)
{
return -1;
}
static int monthdays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//定义成静态只被创建一次,提高效率
int days = monthdays[month];
if (month == 2 && IsLeapYear(year))
{
days += 1;
}
return days;
}
void show()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
bool 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)
{
if (_day > d._day)
{
return true;
}
}
}
return false;
}
bool operator==(const Date& d)
{
if (_year == d._year&&_month == d._month&&_day == d._day)
{
return true;
}
return false;
}
bool operator!=(const Date& d)
{
return !(*this == d);
}
bool operator>=(const Date& d)
{
return *this > d || *this == d;
}
bool operator<=(const Date& d)
{
return !(*this > d) || *this == d;
}
bool operator<(const Date& d)
{
return !(*this>d)||*this==d;
}
//Date(const Date& d){}
//Date& operator=(const Date& d){}
//~Date(){}
Date operator+(int day)/*日期加n天有可能遇到以下情况:2017.12.31.
当对该日期加一时会发生天满进月,月满进年。因此只有当天数恢复某月正常
天数范围时,计算才能结束*/
{
if (day < 0)
{
return *this-(-day);
}
Date tmp(*this);
tmp._day += day;
while (tmp.IsInvalid()) // this->IsInvalid(this)
{
int days = GetMonthDay(tmp._year, tmp._month);
tmp._day -= days;
tmp._month++;
if (tmp._month == 13)
{
tmp._year++;
tmp._month = 1;
}
}
return tmp;
}
Date& operator+=(int day)
{
*this = *this + day;
return *this;
}
Date operator-(int day)
{
if (day < 0)
{
return *this + (-day);
}
Date tmp(*this);
tmp._day -= day;
while (tmp.IsInvalid()) // this->IsInvalid(this)
{
--tmp._month;
if (tmp._month == 0)
{
tmp._year--;
tmp._month = 12;
}
tmp._day += GetMonthDay(tmp._year, tmp._month);
}
return tmp;
}
Date& operator-=(int day)
{
*this = *this - day;
return *this;
}
int operator-(const Date& d);//3
//++d1
Date& operator++() // 前置
{
*this = *this + 1;
return *this;
}
//d1++
Date operator++(int) // 后置
{
Date tmp(*this);
*this = *this + 1;
return tmp;
}
Date operator--()
{
*this = *this - 1;
return *this;
}
Date operator--(int)
{
Date tmp(*this);
*this = *this - 1;
return tmp;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2018,3,25);
d1.show();
Date d2(2018, 3, 25);
d2.show();
Date d3 = d1 + 1000;
(d1 + 1000).show();
d3.show();
(d3-1000).show();
cout << (d1> d2) << endl;
cout << (d1== d2) << endl;//编译器处理过程:d1.operetor==(&d1,d2);->
//bool operator==(Date* this,const Date& d){...}
cout << (d1!= d2) << endl;
cout << (d1>= d2) << endl;
cout << (d1< d2) << endl;
cout << (d1<= d2) << endl;
//cout << d1<< endl;
d2.Show();
++d1; // d1.operator++(&d1);
d1.show();
d2++; // d1.operator++(&d1, 0);
d2.show();
--d1; // d1.operator++(&d1);
d1.show();
d2--; // d1.operator++(&d1, 0);
d2.show();
system("pause");
return 0;
}