低价货源网站,怎么做盗版网站,php做p2p网站源码,阿里云服务器网站开发1.运算符重载
• 当运算符被⽤于类类型的对象时#xff0c;C语⾔允许我们通过运算符重载的形式指定新的含义。C规定类类型对象使⽤运算符时#xff0c;必须转换成调⽤对应运算符重载#xff0c;若没有对应的运算符重载#xff0c;则会编译报错#xff1b;#xff08;运算…1.运算符重载
• 当运算符被⽤于类类型的对象时C语⾔允许我们通过运算符重载的形式指定新的含义。C规定类类型对象使⽤运算符时必须转换成调⽤对应运算符重载若没有对应的运算符重载则会编译报错运算符重载转化成一个函数 • 运算符重载是具有特别名字的函数他的名字是由operator关键字和后⾯要定义的运算符共同构成。和其他函数⼀样它也具有其返回类型和参数列表以及函数体格式operator运算符 构成函数名 • 重载运算符函数的参数个数和该运算符作⽤的运算对象数量⼀样多⼀元运算符有⼀个参数⼆元运算符有两个参数⼆元运算符的左侧运算对象传给第⼀个参数右侧运算对象传给第⼆个参数/- -/*(解引用)/.....就是一元 • 如果⼀个重载运算符函数是成员函数则它的第⼀个运算对象默认传给隐式的this指针因此运算符重载作为成员函数时参数⽐运算对象少⼀个 • 运算符重载以后其优先级和结合性与对应的内置类型运算符保持⼀致
#includeiostream
using namespace std;
class Date
{
public:Date(int year 1, int month 1, int day 1){_year year;_month month;_day day;}void Print(){cout _year / _month / _day endl;}//Get函数int Get_year(){return _year;}int Get_month(){return _month;}int Get_day(){return _day;}//因为存在this指针所以应当少一个bool operator(Date d2){return _year d2._year _month d2._month _day d2._day;}
private:int _year;int _month;int _day;//自定义类型怎么去比较他的行为应该是我们自己去定义的而不是系统去定义的
};
//比较大小的返回值是一个bool值
bool operator(Date d1, Date d2)
{}//重载成全局的方式是最正的二元的左操作数传给第一个参数右操作数传给第二个参数
//bool operator(Date d1, Date d2)
//{
// return d1._year d2._year
// d1._month d2._month
// d1._day d2._day;
// //没有访问权限1可以直接将private的内容包含于public
// //·······2提供Get函数
// //·······3友元
// //·······4放在类里面成为成员函数
//}int main()
{Date x1(2024, 7, 16);Date x2(2024, 7, 16);到底是x1对应d1,还是d2可以显现调用跟一个普通函数一样//operator(x1, x2);还可以//x1 x2;//全局的和成员的都有会优先调用成员的//改后x1.operator(x2);x1 x2;return 0;
}
• 不能通过连接语法中没有的符号来创建新的操作符/运算符⽐如operator • .* : : sizeof ?: . 注意以上5个运算符不能重载。(选择题⾥⾯常考⼤家要记⼀下)重载操作符⾄少有⼀个类类型参数不能通过运算符重载改变内置类型对象的含义如 int operator(int x, int y) .* 案例
#includeiostream
using namespace std;
class A
{
public:void func(){cout A::func() endl;}
};
//用回调的方式去调用成员函数的指针
//普通函数指针void(*)();
typedef void(A::* PF)(); //成员函数指针类型
int main()
{//void(A:: * PF)() nullptr;PF pf nullptr;//实现回调// C规定成员函数要加才能取到函数指针pf A::func;//普通的全局的函数是可以回调的//(*pf)();//成员函数回调不了因为有隐含的this指针//要调用函数指针要传隐含的this指针这个实参A aa;//(*pf)(aa);//这样也不行因为this指针在形参和实参的位置不能显示//C规定回调成员函数的指针要这么回调(aa.*pf)();//aa就悄悄传给this
} • ⼀个类需要重载哪些运算符是看哪些运算符重载后有意义⽐如Date类重载operator-就有意 义天数但是重载operator就没有意义 日期加日期没有意义不会构成重载又因为没有要求运算符两边必须要同类型而是只要求至少有一个类类型的参数
d1 100;//100天后日期是多少
d1 - 100;//100天前日期是多少
d1 - d2;//间隔天数
运算符重载
//d1100
Date operator(int day);
//d1-100
Date operator-(int day);
//d1-d2
int operator-(const Date d);
注运算符重载和函数重载都用了重载但他们没有关系函数重载是函数名相同参数不同运算符重载是重新定义运算符的行为另外两个运算符重载函数可以构成函数重载
• 重载运算符时有前置和后置运算符重载函数名都是operator⽆法很好的区分 C规定后置重载时增加⼀个int形参跟前置构成函数重载⽅便区分 • 重载和时需要重载为全局函数因为重载为成员函数this指针默认抢占了第⼀个形参位 置第⼀个形参位置是左侧运算对象调⽤时就变成了 对象cout不符合使⽤习惯和可读性。 重载为全局函数把ostream/istream放到第⼀个形参位置就可以了第⼆个形参位置当类类型对 象
2.赋值运算符重载 赋值运算符重载是⼀个默认成员函数⽤于完成两个已经存在的对象直接的拷⻉赋值这⾥要注意跟拷⻉构造区分拷⻉构造⽤于⼀个对象拷⻉初始化给另⼀个要创建的对象 int main()
{Date d1(2024, 7, 5);Date d2(2024, 7, 6);//赋值重载拷贝d1 d2;//拷贝构造Date d3(d2);Date d4 d2;return 0;
} 赋值运算符重载的特点
1. 赋值运算符重载是⼀个运算符重载规定必须重载为成员函数不可以是全局。赋值运算重载的参数建议写成 const 当前类类型引⽤否则会传值传参会有拷⻉
void operator(const Date d)
{//尽管用传值传参不会产生所谓的无穷递归现在是赋值重载传值传参把d2传给d调用拷贝构造调用完回来了接着调用赋值//operator赋值//但是建议用引用_year d._year;_month d._month;_day d._day;
}bool operator(const Date d)
{//拷贝构造赋值重载拷贝去传值传参形成新的拷贝构造。。。。。无穷递归//operator等于
}
因为拷贝构造和复制重载两者本来就不一样
2. 有返回值 且建议写成当前类类型引⽤引⽤返回可以提⾼效率 有返回值⽬的是为了⽀持连赋值场景
Date operator(const Date d)
{_year d._year;_month d._month;_day d._day;//d3d1//this是d3的地址return *this;//类里面可以显示写
}
优化传值返回也会生成一个拷贝就调用拷贝构造白白生成一个拷贝付出了代价用印用来解决提高效率
Date operator(const Date d)
{_year d._year;_month d._month;_day d._day;return *this;
} 3. 没有显式实现时编译器会⾃动⽣成⼀个默认赋值运算符重载默认赋值运算符重载⾏为跟默认的拷贝构造函数类似对内置类型成员变量会完成值拷⻉/浅拷⻉(⼀个字节⼀个字节的拷⻉)对⾃定义类型成员变量会调⽤他的赋值重载 4. 像Date这样的类成员变量全是内置类型且没有指向什么资源编译器⾃动⽣成的赋值运算符重载就可以完成需要的拷⻉所以不需要我们显⽰实现赋值运算符重载。像Stack这样的类虽然也都是内置类型但是_a指向了资源编译器⾃动⽣成的赋值运算符重载完成的值拷⻉/浅拷⻉不符合我们的需求所以需要我们⾃⼰实现深拷⻉(对指向的资源也进⾏拷⻉)。像MyQueue这样的类型内部主要是⾃定义类型Stack成员编译器⾃动⽣成的赋值运算符重载会调⽤Stack的赋值运算符重载也不需要我们显⽰实现MyQueue的赋值运算符重载。这⾥还有⼀个⼩技巧如果⼀个类显⽰实现了析构并释放资源那么他就需要显⽰写赋值运算符重载否则就不需要
3.日期类实现
补充两个日期间隔多少天有算正负数-----算法图解 另外还可以尽可能复用逻辑代码中会演示到取小日期比较大小让小的日期不断真到与大日期相等不过相比于上图方法会相对慢但cpu太快了无所谓
代码
头文件Date.h
#pragma once
#includeiostream
using namespace std;
#includeassert.h
class Date
{//我和小笨蛋不认识所以不可以去他家玩但我很想去因为他爸爸会数分就给小笨蛋吹彩虹屁就和他成为朋友了//友元声明friend函数声明friend ostream operator(ostream out, const Date d);friend istream operator(istream in, Date d);
public:bool CheckDate();Date(int year 1900, int month 1, int day 1);void Print()const;//频繁调用的小函数//没有做声明和定义分离 因为定义在类里面的成员函数默认是内联inlineint GetMonthDay(int year, int month){//防止month在减的时候是从一月减到零月assert(month 0 month 13);//因为要频繁调用所以放到静态区去static int monthDayArray[13] { -1,31,28,31,30,31,30,31,31,30,31,30,31 };if ((month 2) (year % 100 ! 0 year % 4 0) || (year % 400 0)){return 29;}return monthDayArray[month];}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)const;Date operator(int day);Date operator-(int day)const;Date operator-(int day);d1//Date operator();d1//Date operator();//这么区分这两个d1,d1:重载运算符时有前置和后置运算符重载函数名都是operator⽆法很好的区分。//C规定后置重载时增加⼀个int形参跟前置构成函数重载⽅便区分。//d1:后置Date operator(int);//d1:前置Date operator();Date operator--(int);Date operator--();//d1-d2int operator-(const Date d);//printf与scanf他们直接适应的是内置类型//流插入之所以可以自定识别类型是因为本质是函数重载//void operator(ostream out);//写成成员函数是为了访问私有但是这个实现形式倒反天罡
private:int _year;int _month;int _day;
};//全局
//流插入
ostream operator(ostream out, const Date d);
//用友元声明让类外面的函数访问类里面的元素
//可是连续输出又不行EGcoutd1d2
//流插入是从左到右(赋值相反)所以应该返回out//流提取
istream operator(istream in, Date d);//提取的值要放到Date对象中所以不加const
源文件Date.cpp
#includeDate.h
bool Date::CheckDate()
{if (_month 1 || _month 12|| _day 1 || _day GetMonthDay(_year, _month)){return false;}else{return true;}
}Date::Date(int year, int month, int day)
{_year year;_month month;_day day;if (!CheckDate()){cout 非法日期 endl;Print();}
}
void Date::Print() const
{cout _year - _month - _day endl;
}
//这是会改变_day
Date Date::operator(int day)
{//有坑当day为负数时// 解决if (day 0){return *this - (-day);}//与加法进数一样天满了往月上进位月满了往年进位_day day;while (_day GetMonthDay(_year, _month)){_day - GetMonthDay(_year, _month);_month;if (_month 13){_year;_month 1;}}return*this;
}
//d1100:不会改变d1
Date Date::operator(int day)const
{//拷贝构造的场景Date tmp *this;//就会往tmp上走/*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;}}*///复用tmp day;return tmp;//tmp为局部对象出了作用域销毁不能用引用返回即使会多进行一次拷贝
}Date Date:: operator-(int day)
{if (day 0){return *this (-day); }_day - day;while (_day 0){_month--;if (_month 0){_month 12;}_day GetMonthDay(_year, _month);}return *this;
}Date Date:: operator-(int day)const
{Date tmp *this;/*tmp._day - day;while (tmp._day 0){tmp._month--;if (tmp._month 0){tmp._month 12;}tmp._day GetMonthDay(tmp._year, tmp._month);}*/tmp - day;return tmp;
}d1-100
用-实现-d1改变
//Date Date:: operator-(int day)
//{
// /*Date tmp *this - day;
// *this tmp;*/
// *this *this - day;
// return *this;
//}//其实-复用-更好因为-的效率本身是更低的
//
//Date tmp *this;是一次拷贝return *this 传值返回也是一次拷贝
//-复用-或-复用-在“-”中都要进行两次构造所以真正的区别在于-
//如果-是自己实现全程没有拷贝
//但是-去复用-就会加上-带来的两次拷贝和多加的赋值拷贝很亏//至少实现或
bool Date:: operator(const Date d)const
{if (_year d._year){return true;}else if (_year d._year){if (_month d._month){return true;}else if (_month d._month){return _day d._day;}}return false;
}
//d1d2
bool Date:: operator(const Date d)const
{return *this d || *this d;
}
bool Date:: operator(const Date d)const
{/*if (_year d._year){return true;}else if (_year d._year){if (_month d._month){return true;}else if (_month d._month){return _day d._day;}}return false;*/return !(*this d);
}
bool Date:: operator(const Date d)const
{return !(*this d);
}
bool Date:: operator(const Date d)const
{return _year d._year _month d._month _day d._day;
}
bool Date:: operator!(const Date d)const
{return !(*this d);
}//d1
//d1.operator(0);
Date Date::operator(int)
{Date tmp *this;*this 1;return tmp;
}//d1
//d1.operator();
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;
}//天数的实现
int Date:: operator-(const Date d)
{int flag 1;int n 0;//假设法Date max *this;Date min d;if (*this d){max d;min *this;flag -1;}while (min ! max){min;n;}return n * flag;
}//倒反天罡
//void Date::operator(ostream out)
//{
// //out就是cout
// //对于二元函数默认左操作数对应第一个参数
// out _year 年 _month 月 _day 日 endl;
// //要这么配得上d1out//d1.operator(cout)
//}//因为流对象中含有指向IO缓冲区的指针假如流对象可以复制那么将会有两个指针同时操作缓冲区如何释放、如何修改都会有冲突同步问题因此流对象无法复制。
ostream operator(ostream out, const Date d)
{out d._year 年 d._month 月 d._day 日 endl;return out;
}istream operator(istream in, Date d)
{while (1){cout 请依次输入年月日:;in d._year d._month d._day;//在进行流提取是cout的buffer刷新了be flushed了if (!d.CheckDate()){cout 输入日期非法;d.Print();cout 请重新输入 endl;}else{break;}}return in;
}