www.gusucode.com > 《C++高级语言程序设计》PPT及全书例子源代码-源码程序 > 《C++高级语言程序设计》PPT及全书例子源代码-源码程序/code/C++例题程序/第4章/s4_11/smain4_11.cpp
//Download by http://www.NewXing.com //主文件 //文件名:ch4_11\smain4_11.cpp #include "sclass4_11_counter.h" //包含类定义头文件 //测试函数 void main( ) { CCounter oCCounter1, oCCounter2; oCCounter1 = ++oCCounter2; //此处++为重载前缀++,因为oCCounter2是CCounter的对象 //调用重载( ),因为oCCounter是CCounter的对象 cout << "前缀++: " << oCCounter1() << " = " << oCCounter2.m_value << endl; oCCounter1 = --oCCounter2; //此处--为重载前缀--,因为oCCounter2是CCounter的对象 //调用重载( ),因为oCCounter2是CCounter的对象 cout << "前缀--: " << oCCounter1.m_value << " = " << oCCounter2() << endl; oCCounter1 = oCCounter2++; //此处++为重载后缀++,因为oCCounter2是CCounter的对象 //调用重载( ),因为oCCounter1是CCounter的对象 cout << "后缀++: " << oCCounter1.m_value << " < " << oCCounter2.m_value << endl; oCCounter1 = oCCounter2--; //此处--为重载后缀--,因为oCCounter2是CCounter的对象 //调用重载( ),因为oCCounter2是CCounter的对象 cout << "后缀--: " << oCCounter1() << " > " << oCCounter2() << endl; oCCounter1 = 10; //调用重载运算符=,将10赋值给对象 cout << "赋值=: " << oCCounter1.m_value << endl; oCCounter2 = 10 + oCCounter1; cout << "整数+对象: " << oCCounter1.m_value << "; " << oCCounter2.m_value<< endl; oCCounter2 = oCCounter1 + 20; cout << "对象+整数: " << oCCounter1.m_value << "; " << oCCounter2.m_value<< endl; oCCounter2 = oCCounter2 + oCCounter1; cout << "对象+对象: " << oCCounter1.m_value << "; " << oCCounter2.m_value<< endl; oCCounter2 = oCCounter2 - oCCounter1; cout << "对象-对象: " << oCCounter1.m_value << "; " << oCCounter2.m_value<< endl; cout << "对象>对象: " << (oCCounter2 > oCCounter1) << endl; cout << "对象<=对象: " << (oCCounter2 <= oCCounter1) << endl; }