第二节运算符重载(教材P253-285) coord coord: operator ++O ++x 耀莒 ++this->x ++y;// 仔:++this->y; return *this: void main( i coord ob(10, 20) ob. print(; ++0D, ob. printo ob operator++0; ob. printo;
第二节 运算符重载(教材①P253-285) coord coord::operator ++() { ++x;//相当于:++this->x; ++y;//相当于:++this->y; return *this; } void main() { coord ob(10,20); ob.print(); ++ob; ob.print(); ob.operator++(); ob.print(); }
第〓节运算符载(敵材①P253285) >重载单目运算符时,成员函数中没有参数传递,其参 数是通过ths指针隐含传递给函数的。 >重载单目运算符使用方法: ★@obj; 隐式调用 ●☆ obj. operator(@( 显式调用 2、前置运算与后置运算 例【54】前置运算与后置运算举例
第二节 运算符重载(教材①P253-285) ➢重载单目运算符时,成员函数中没有参数传递,其参 数是通过this指针隐含传递给函数的。 ➢重载单目运算符使用方法: @obj ; —— 隐式调用 obj.operator@( ) —— 显式调用 2、前置运算与后置运算 ➢例【5.4】前置运算与后置运算举例
第二节运算符重载(教材P253-285) #include <iostream. h> class coord i int x, ys public: coord(int i=0,intj=0); void printo; coord operator ++0; coord operator ++(int) coord operator +(coord c); coord: coord(int i, int j) Rx=;y=j; void coord: printo f cout <<x=<<X<<y=<<y<<end;
第二节 运算符重载(教材①P253-285) #include <iostream.h> class coord { int x,y; public : coord(int i=0,int j=0); void print(); coord operator ++(); coord operator ++(int); coord operator +(coord c); }; coord::coord(int i,int j) {x=i;y=j;} void coord::print() { cout << "x="<<x<<" y="<<y<<endl;}
第〓节运算符载(敵材①P253285) coord coord: operator++(〃重载前置运算符++ 十X; ++y return *this: coord coord: operator++(int)∥重载后置运算符++ X+十 y++; e return*this coord coor: operator+( coord c/重载双目运算符+ coord temp temp.X=X+CX; temp. y=y+cy, ● return temp;
第二节 运算符重载(教材①P253-285) coord coord::operator ++() //重载前置运算符++ { ++x; ++y; return *this; } coord coord::operator ++(int)//重载后置运算符++ { x++; y++; return *this; } coord coord::operator +(coord c)//重载双目运算符+ {coord temp; temp.x=x+c.x; temp.y=y+c.y; return temp; }
第二节运算符重载(教材P253-285) void maino { coord obl(10,20),0b2(20,40),ob obl. printo ob2 printo ++obl ob2++; obl. printO: ob2 printo; 0b=(++ob1)+(0b2++) ob. printo >即:后置运算在参数表中加一个nt即可
第二节 运算符重载(教材①P253-285) void main() { coord ob1(10,20),ob2(20,40),ob; ob1.print(); ob2.print(); ++ob1; ob2++; ob1.print(); ob2.print(); ob=(++ob1)+(ob2++); ob.print(); } ➢即:后置运算在参数表中加一个int即可