// ============================================================================
//  p1c04_06_02  —  4.6.4 自增自减(++/--)运算符重载
//  来源：川大 C++/LinuxC wiki「02.Qt方向」
//  说明：本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
//        // --- 原文片段 [pageId:blockIdx] --- 标注出处。
//        已做最小修正（笔误/平台移植），详见 docs/ERRATA.md。
// ============================================================================

#include <iostream>
using namespace std;

// --- 原文片段 [58954458:1]  4.6.4 自增自减(++/--)运算符重载 ---
class Complex{
	friend ostream& operator<<(ostream& os,Complex& complex){
		os << "A:" << complex.mA << " B:" << complex.mB << endl;
		return os;
	}
public:
	Complex(){
		mA = 0;
		mB = 0;
	}
	//重载前置++
	Complex& operator++(){
		mA++;
		mB++;
		return *this;
	}
	//重载后置++
	Complex operator++(int){	
		Complex temp;
		temp.mA = this->mA;
		temp.mB = this->mB;
		mA++;
		mB++;
		return temp;
	}
	//前置--
	Complex& operator--(){
		mA--;
		mB--;
		return *this;
	}
	//后置--
	Complex operator--(int){
		Complex temp;
		temp.mA = mA;
		temp.mB = mB;
		mA--;
		mB--;
		return temp;
	}
	void ShowComplex(){
		cout << "A:" << mA << " B:" << mB << endl;
	}
private:
	int mA;
	int mB;
};

void test(){
	Complex complex;
	complex++;
	cout << complex;
	++complex;
	cout << complex;

	Complex ret = complex++;
	cout << ret;
	cout << complex;

	cout << "------" << endl;
	ret--;
	--ret;
	cout << "ret:" << ret;
	complex--;
	--complex;
	cout << "complex:" << complex;
}

// --- main ---
int main() {
    test();
    return 0;
}
