大型网站制作都有哪些,石家庄网络关键词排名,海南智能网站建设报价,网站建设市场报价C为了增强代码的可读性引入了运算符重载#xff0c;运算符重载是具有特殊函数名的函数#xff0c;也具有其返回值类型#xff0c;函数名字以及参数列表#xff0c;其返回值类型与参数列表与普通的函数类似。 函数名字为#xff1a;关键字operator后面接需要重载的运算符符… C为了增强代码的可读性引入了运算符重载运算符重载是具有特殊函数名的函数也具有其返回值类型函数名字以及参数列表其返回值类型与参数列表与普通的函数类似。 函数名字为关键字operator后面接需要重载的运算符符号。 函数原型返回值类型 operator操作符(参数列表) 下面以日期类为例子
我们知道系统的基本类型可以进行、-等基本运算但是用户的自定义类型我们不能使用系统的基本类型和-需要对其进行函数名重载才能使用这些操作。
运算符既可以是成员函数也可以是非成员函数。因为我们通常将用户自定义类型的数据定义为私有在类外不能访问私有成员。所以通常将运算符重载函数定义为成员函数。特别注意成员函数的形参列表形参的个数比实际的个数少一个。因为有一个是这个类本身。 #pragma once#define _CRT_SECURE_NO_WARNINGS#include iostream
#include cassert
using namespace std;class Date
{
public:Date(int year 1, int month 1, int day 1){if (month 13 || month 0|| day GetMonthDay(year, month) || day 0){assert(false);}_year year;_month month;_day day;}Date operator(const Date d);bool operator(const Date d) const;bool operator(const Date d) const;bool operator(const Date d) const;bool operator(const Date d) const;bool operator(const Date d) const;bool operator!(const Date d) const;Date operator(int day);Date operator(int day);int GetMonthDay(int year, int month);Date operator();Date operator(int);Date operator-(int day);Date operator-(int day);Date operator--(int);Date operator--();int operator-(const Date d);friend ostream operator(ostream out, const Date d);friend istream operator(istream in, Date d);private:int _year;int _month;int _day;
}; 的运算符重载
Date Date::operator(const Date d)
{if (this ! (d)){_year d._year;_month d._month;_day d._day;}return *this;
}等号运算符重载的返回值为Date 类型使用的是赋值后对象的自引用可以减少拷贝提高效率。如果thisd说明是自己对自己赋值不需要进行操作。 运算符重载
bool Date::operator(const Date d) const
{return _year d._year _month d._month _day d._day;
}号运算符的重载判断年、月、日是否相等就可以了。 下面就是、、、等运算符的重载 运算符的重载
bool Date::operator(const Date d) const
{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;}return false;} 运算符的重载
在前面我们已经实现过、的运算符重载。只需要复用前面的代码就可以实现的运算符重载。
bool Date::operator(const Date d) const
{return (*this d) || (*this d);
} 运算符的重载
的反面就是我们对取反就可以完成运算符的重载。
bool Date::operator(const Date d) const
{return !(*this d);
} 运算符的重载
的反面是,只需要对取反就可以完成的操作了。
bool Date::operator(const Date d) const
{return !(*this d);
}运算符的重载
是的反面
bool Date::operator(const Date d) const
{return !(*this d);
} 、、-、-运算符的重载 运算符的重载
int Date::GetMonthDay(int year,int month)
{int Arry[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 Arry[month] 1;}else{return Arry[month];}
}Date Date::operator(int day)
{if (day 0){return (*this) - -day;}else{_day day;while (_day GetMonthDay(_year, _month)){_day - GetMonthDay(_year, _month);_month;if (_month 13){_month 1;_year;}}return *this;}}
如果一个类day我们先将这个类的_dayday,再判断_day的天数有没有大于这个月的天数如果大于了类的月份1月份1后判断月份是否超过了12超过了的话将月份置为1年份1.循环往复就可以就出day后的日期了。
如果一个负数的天数实际上是-一个整数的天数我们调用-就好了。 运算符的重载
运算符和运算符类似。
比如 int a3;
a3返回结果6但是a还是3
a3返回6a变成了6
Date Date::operator(int day)
{Date tmp (*this);tmp day;return tmp;} -运算符的重载
Date Date::operator-(int day)
{if (day 0){return (*this) -day;}else{_day - day;while (_day 0){--_month;if (_month 0){_month 12;--_year;}_day GetMonthDay(_year, _month);}return *this;}}
-运算符的操作和的类似我们先把类的_day-day;如果得到一个负数说明日期的_day不足以-day我们需要向月份借天数将月份--然后_day借的那个月的天数。月份--之后我们需要对月份判断是否正确如果本来月份是1--变成0实际上是去年的12月需要将_month12,还有减少年份。之后就需要循环判断直到_day0为止。 -运算符的重载
-和-运算符类似
Date Date::operator-(int day)
{Date tmp (*this);tmp - day;return tmp;
} 、--运算符的重载 运算符的重载
分为前置与后置 前置运算符的重载
前置返回的是后的结果
Date Date::operator()
{*this *this 1;return *this;
}后置的运算符重载
我们怎么区分前置和后置呢在形参列表加入一个int就可以说明这个是后置了。这个int便于编译器区分。
后置返回的是之前的结果的数据仍然就行了自增。
Date Date::operator(int)
{Date tmp (*this);*this (*this)1;return tmp;
} 前置--运算符重载函数
Date Date::operator--()
{(*this) - 1;return *this;
} 后置--运算符重载函数
Date Date::operator--(int)
{Date tmp (*this);*this - 1;return tmp;
}那么我们如何计算两个日期相差多少天呢。利用日期-时期。所以需要我们构造日期-日期的运算符重载函数。
int Date::operator-(const Date d)
{Date max(*this);Date mind;int flag 1;if (*this d){max d;min (*this);flag -1;}int n 0;while (min ! max){min;n;}return flag*n;
}
两个日期相减我们需要先找到两个中较大的日期。
小的日期自增到与大的日期相等自增的次数就是两个日期相差的天数。加一个flag标志用来区分日期相差的正负。 流插入运算符重载。我们知道流插入运算符可以打印基本数据类型。用户自定义的类型能不能使用系统自带的流插入运算符呢是不行的需要我们进行函数名重载便于支持自定义类型的打印。
ostream operator(ostream out,const Date d)
{out d._year 年 d._month 月 d._day 日 endl;return out;
}
流插入运算符的重载函数。不能作为类的成员函数因为成员函数的第一个参数默认是类的this指针。但是我们使用流插入运算符是coutdendl第一个参数为cout所以我们不能使用成员函数作为重载函数那么我们类外不能访问私有成员。该怎么办我们将函数声明为友元函数友元函数可以在类外访问私有成员。
在类内加入 为什么返回类型为ostream呢因为这样可以做到连续的流插入输出多个结果。 流提取运算符重载函数
我们知道可以输入基本的数据类型。但是日期类是我们自定义的类型。我们使用输入就需要对进行运算符重载。
与流插入运算符的重载函数一样。流提取运算符重载函数也不能使用成员函数。写在类外将这个函数作为类的友元函数。
istream operator(istream in, Date d)
{in d._year d._month d._day;return in;
}
在类内声明 对、进行了运算符重载。我们就可以直接使用其操作了。