1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| #include <iostream> using namespace std;
class myInt { friend ostream& operator<< (ostream &cout,myInt myint); public: myInt() { m_Num = 0; } myInt& operator++() { m_Num++; return *this; } myInt operator++(int) { myInt temp = *this; m_Num++; return temp; }
myInt &operator--() { m_Num--; return *this; } myInt operator--(int) { myInt temp = *this; m_Num--; return temp; }
private: int m_Num; };
ostream &operator<< (ostream &cout,myInt myint) { cout << myint.m_Num; return cout; }
void test1() { myInt myint; cout << "递增" << endl; cout << ++myint << endl; cout << myint++ << endl; cout << myint << endl; } void test2() { myInt myint; cout << "递减" << endl; cout << --myint << endl; cout << myint-- << endl; cout << myint << endl; }
int main(){ test1(); test2(); return 0; }
|