一、运算符重载
C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。
关键字:operator
使用方式:
// 全局的operator==
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
//private:
int _year;
int _month;
int _day;
};
bool operator==(const Date& d1, const Date& d2)
{
return d1._year == d2._year
&& d1._month == d2._month
&& d1._day == d2._day;
}
void Test ()
{
Date d1(2018, 9, 26);
Date d2(2018, 9, 27);
cout<<(d1 == d2)<<endl;
}
注意
1.不能通过连接其他符号来创建新的操作符:比如operator@
2.重载操作符必须有一个类类型参数
3.用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不能改变其含义
4.作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this
5. .* :: sizeof ?: . 注意以上5个运算符不能重载。这个经常在笔试选择题中出现。
连续赋值
从右往左赋值
二、写一个日期类
1.Date.h
代码如下(示例):
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1);
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=(const Date& d);
void Print();
Date& operator+=(int day);
Date operator+(int day);
Date operator-(int day);
Date& operator-=(int day);
//++d1
Date& operator++();
//d1++
Date operator++(int);
int operator-(const Date& d);
private:
int _year;
int _month;
int _day;
};
2.Date.cpp
代码如下(示例):
#include"Date.h"
int Date::GetMonthDay(int year, int month)
{
assert(month > 0 && month < 13);
int monthArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400) == 0))
{
return 29;
}
else
{
return monthArray[month];
}
}
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;
}
}
//d1==d2 -> d1.operator==(d2)
bool Date::operator==(const Date& d)
{
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
//d1<d2
bool Date::operator<(const Date& d)
{
return _year < d._year
|| (_year == d._year && _month < d._month)
|| (_year == d._year && _month == d._month && _day < d._day);
}
//d1<=d2
bool Date::operator<=(const Date& d)
{
return *this < d || *this == d;
}
//d1>d2
bool Date::operator>(const Date& d)
{
return !(*this <= d);
}
//d1>=d2
bool Date::operator>=(const Date& d)
{
return !(*this < d);
}
//d1!=d2
bool Date::operator!=(const Date& d)
{
return !(*this == d);
}
//d1=d2
//d1=d1
Date& Date::operator=(const Date& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
void Date::Print()
{
cout << _year << "/" << _month << "/" << _day << endl;
}
Date& Date::operator+=(int day)
{
if (day < 0)
{
*this -= -day;
return *this;
}
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 13)
{
++_year;
_month = 1;
}
}
return *this;
}
Date Date::operator+(int day)
{
Date tmp(*this);
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)
{
*this = *this + day;
return *this;
}*/
/*Date Date::operator+(int day)
{
Date tmp;
tmp += day;
return tmp;
}*/
Date& Date::operator++()
{
*this = *this + 1;
return *this;
}
Date Date::operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
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 tmp(*this);
tmp -= day;
return tmp;
}
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 n = 0;
while (min != max)
{
++min;
++n;
}
return n * flag;
}
Test.cpp
代码如下(示例):
void TestDate1()
{
Date d1(2023, 3, 4);
d1.Print();
/*Date d2(2023, 2, 29);
d2.Print();*/
/*d1 += 100;
d1.Print();
Date d2 = d1 + 100;
d2.Print();*/
++d1;
d1.Print();
d1++;
d1.Print();
d1++;
void TestDate2()
{
Date d1(2023,3,3);
d1.Print();
d1 -= 2;
d1.Print();
d1 -= 100;
d1.Print();
Date d2(2023, 3, 3);
d2.Print();
d2 += 100;
d2.Print();
d2 -= 100;
d2.Print();
}
void TestDate3()
{
Date d1(2023, 2, 4);
d1.Print();
Date d2(2023, 4, 5);
d2.Print();
cout << d2 - d1 << endl;
cout << d1 - d2 << endl;
}
int main()
{
TestDate1();
TestDate2();
TestDate3();
return 0;
}