📄 example31.cpp
字号:
#include <iostream>using namespace std;class loc { int longitude, latitude;public: loc() {} // needed to construct temporaries loc(int lg, int lt) { longitude = lg; latitude = lt; } void show() { cout << longitude << " "; cout << latitude << "\n"; } loc operator+(loc op2); loc operator-(loc op2); loc operator=(loc op2); loc operator++();};// Overload + for loc.loc loc::operator+(loc op2){ loc temp; temp.longitude = op2.longitude + longitude; temp.latitude = op2.latitude + latitude; return temp;}// Overload - for loc.loc loc::operator-(loc op2){ loc temp; // notice order of operands temp.longitude = longitude - op2.longitude; temp.latitude = latitude - op2.latitude; return temp;} loc loc::operator=(loc op2){ longitude = op2.longitude; latitude = op2.latitude; return *this; // i.e., return object that generated call}// Overload prefix ++ for loc.loc loc::operator++(){ longitude++; latitude++; return *this;}int main(){ loc ob1(10, 20), ob2( 5, 30), ob3(90, 90); ob1.show(); ob2.show(); ++ob1; ob1.show(); // displays 11 21 ob2 = ++ob1; ob1.show(); // displays 12 22 ob2.show(); // displays 12 22 ob1 = ob2 = ob3; // multiple assignment ob1.show(); // displays 90 90 ob2.show(); // displays 90 90 return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -