c++ 重载运算符
2015-06-12 16:36
239 查看
#include <iostream> using namespace std; class CTmp{ public: //friend const CTmp operator++(CTmp& tmp); //friend const CTmp operator++(CTmp& tmp, int); CTmp(int iflag=0):i(iflag) { } void print()const { cout<<"i:"<<i<<endl; } const CTmp operator +(const CTmp& info) { cout<<"operator+:"<<endl; return CTmp(i+info.i); } const CTmp operator++() { cout<<"++CTmp:"<<endl; i++; return *this; } const CTmp operator++(int) { cout<<"CTmp++:"<<endl; CTmp tmp(i); this->i++; return tmp; } CTmp& operator +=(const CTmp& info) { cout<<"operator+="<<endl; i += info.i; return *this; } bool operator >=(const CTmp& info) { cout<<"operator>=:"<<endl; return (this->i > info.i)?true:false; } private: int i; }; /* const CTmp operator++(CTmp& tmp) { cout<<"friend ++tmp:"<<endl; tmp.i++; return tmp; } const CTmp operator++(CTmp& tmp, int) { cout<<"friend tmp++:"<<endl; CTmp otmp(tmp.i); tmp.i++; return otmp; } */ int main() { int i = 1, j = 2, k = 3; CTmp I(i); CTmp J(j); CTmp K(k); K += I+J; I++; ++I; if( I >= K ) cout<<"true"<<endl; else cout<<"false"<<endl; return 0; }
相关文章推荐
- C++ Primer Plus学习笔记一(第二章)
- typedef函数指针的用法(C++)
- 自动化测试程序之二模拟触摸屏点击事件和滑动事件(C语言)
- (搬运工)推荐!国外程序员整理的 C++ 资源大全
- 国外程序员整理的 C++ 资源大全
- chapter12test3
- c语言实现灰度图转换为二值图
- 遍历当前文件目录并且删除所有最近一周没有修改的文件(C/C++)
- 对string的一些扩展函数 分类: C/C++ 2015-06-12 15:43 170人阅读 评论(1) 收藏
- C++ Socket服务器简单代码示例
- c/c++面试题
- C++内存管理
- C语言宏定义总结
- C语言之#define用法(宏定义命令)
- 【基础】C++设计模式之单例模式
- C++中使用new定义的变量和不使用new定义的变量区别
- C语言嵌入式编程
- c语言资格认证
- 如果new 一个对象,用free去释放,会有什么问题?
- 冒泡排序(C++语言描述)