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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| #define _CRT_SECURE_NO_WARNINGS #include<iostream>
using namespace std;
class A { public: A(); A(int x); ~A(); A & setValue(int x); int getValue() { return value; } bool operator&&(const A & ta); bool operator||(const A & ta); private: int value; };
A::A(int x) { value = x; }
A & A::setValue(int x) { value = x; return *this; }
bool A::operator||(const A & ta) { cout << "重载了||运算符" << endl; if (value != 0 || ta.value != 0) { return true; } return false; } bool A::operator&&(const A & ta) { cout << "重载了&&运算符" << endl; if (value != 0 && ta.value != 0) { return true; } return false; } A::A() { }
A::~A() { }
int main(char *argv[], int argc) { A a(0), b(20); int i = 0, j = 20; if (a && b) { cout << "a && b == true" << endl; } if (b || a.setValue(100)) { cout << "a && b == true" << endl; }
cout << a.getValue() << endl;
if (i && (j = 100)); cout << "j = " << j << endl;
if (j || (i=20)); cout << "i = " << i << endl; return 0; }
|