C++初阶(长期更新)第5讲:
类和对象(中)
跟着潼心走,轻松拿捏C++,困惑通通走,一去不回头~欢迎开始今天的学习内容,你的支持就是博主最大的动力。
博主主页:潼心1412o-CSDN博客
前言
今天继续学习上节课没有学习完的运算符重载的内容,gogogo,出发喽~
5.1 =赋值运算符重载
Date& Date::operator=(constDate& d) {if(this!= &d) { _year = d._year; _month = d._month; _day = d._day; }return*this; }注意和拷贝构造区分开,拷贝构造是在初始化时就已经完成的
而赋值运算符重载则是已经初始化的自定义类型进行赋值
5.2 -和-=运算符重载
下面有两种方式的代码实现,都进行了代码复用
Date& Date::operator-=(intday) { _day -= day;while(_day <=0) { --_month;if(_month ==0) { _month =12; --_year; } _day +=GetMonthDays(_year, _month); }return*this; } Date Date::operator-(intday) {Datetemp(*this); temp -= day;returntemp; }Date Date::operator-(intday) {Datetemp(*this); temp._day -= day;while(temp._day <=0) { --temp._month;if(temp._month ==0) { temp._month =12; --temp._year; } temp._day +=GetMonthDays(temp._year, temp._month); }returntemp; } Date& Date::operator-=(intday) { *this= *this- day;return*this; }同时进行计算,第一种代码进行了两次拷贝,第二次则进行了5次,所以我们优先实现-=再实现-
5.3 前后置++ --运算符重载
Date& Date::operator++() { *this+=1;return*this; } Date Date::operator++(int) {Datetemp(*this); *this+=1;returntemp; } Date Date::operator--() { *this-=1;return*this; } Date Date::operator--(int) {Datetemp(*this); *this-=1;returntemp; }前置后置的区分方式是 后置加一个int类型的参数
在使用时就这样调用,注意显式调用时一定要传一个int类型的实参
d1++;//显式调用d1.operator++(0);5.4 一些其他的二元运算符重载
boolDate::operator==(constDate d) {return_year == d._year && _month == d._month && _day == d._day; }boolDate::operator!=(constDate d) {return!(*this== d); }boolDate::operator<(constDate d) {if(_year < d._year) {returntrue; }elseif(_year == d._year) {if(_month < d._month) {returntrue; }elseif(_month == d._month) {if(_day == d._day)returntrue; } }returnfalse; }boolDate::operator<=(constDate d) {return*this< d || *this== d; }boolDate::operator>(constDate d) {return!(*this<= d); }boolDate::operator>=(constDate d) {return!(*this>= d); }5.5 []运算符重载
classSeqList{public:int&operator[](size_tx) {return_a[x]; }private:int_a[10];int_size; };voidTest01(){ SeqList s;for(size_ti =0; i <10; i++) { s[i] = i; cout << s[i]; } cout << endl; }5.6 d1-d2
intDate::operator-(constDate& d) { Date max = *this; Date min = d;intflag =1;intcount =0;if(max < min) { max = d; min = *this; flag =-1; }while(max!=min) { --max; ++count; }returncount * flag; }5.7 cout cin流插入流提取
以前面的经验,很自然地我们写出如下代码
ostream& Date::operator<<(ostream& out) { out << _year <<"/"<< _month <<"/"<< _day;returnout; } istream& Date::operator>>(istream& in) { in >> _year >> _month >> _day;returnin; }但是在调用时我们发现由于前面要求运算符重载如果是二元的话两个操作数必须按顺序传入函数但是这里this占据了默认的第一个位置,而我们一般cout是在左边的操作数,非要用只能这么写
Date d2; d2 >> cin; d2 << cout;在调用时就非常不符合我们的使用习惯,降低了代码的可读性,所以我们不用一直放在类里面而是可以通过友元的方式使用全局函数进行流插入流提取的运算符重载
//Date类内...friendostream&operator<<(ostream& out,constDate& d);friendistream&operator>>(istream& in, Date& d);//检查日期类是否合法boolDate::CheckDate(){if(_month <0|| _month>12|| _day <=0|| _day >GetMonthDays(_year, _month)) {returnfalse; }returntrue; }//全局函数ostream&operator<<(ostream& out,constDate& d) { out << d._year <<"/"<< d._month <<"/"<< d._day;returnout; } istream&operator>>(istream& in, Date& d) {while(1) { in >> d._year >> d._month >> d._day;if(d.CheckDate()) {break; }elsecout <<"非法输入,请重新输入->"<< endl; }returnin; }5.8 取地址运算符重载
5.8.1 const成员函数
5.8.2 取地址运算符重载
Date* Date::operator&() { return this; //return nullptr; } const Date* Date::operator&() const { return this; //return nullptr; }一般不用,除了不想让这个地址被拿到起一个保护作用
好了今天的学习内容就到这里啦,谢谢你的陪伴,我是潼心,下次再见~如果这篇文章对你有帮助的话。请务必给主播一个一键三连,球球了,这对主播很重要~
博主主页:潼心1412o-CSDN博客