标准重载代码
来源:互联网 发布:java 线程局部变量 编辑:程序博客网 时间:2024/06/07 22:33
#include "iostream"using namespace std;class Complex{private: int a; int b; friend ostream& operator<<(ostream &out, Complex &c1);public: Complex(int a = 0, int b = 0) { this->a = a; this->b = b; } void printCom() { cout<<a<<"+"<<b<<"i"<<endl; }public: //通过类的成员函数实现-操作 Complex operator-(Complex &c2) { Complex tmp; tmp.a = this->a -c2.a; tmp.b = b -c2.b; return tmp; } Complex operator+(Complex &c2) { Complex tmp; tmp.a = this->a + c2.a; tmp.b = b + c2.b; return tmp; } // 前置-- Complex& operator--() { this->a--; this->b--; return *this; } // 前置++ Complex& operator++() { this->a++; this->b++; return *this; } //后置-- Complex operator--(int) { Complex tmp = *this; this->a --; this->b --; return tmp; } //后置++ Complex operator++(int) { Complex tmp = *this; this->a ++; this->b ++; return tmp; }};//1操作符重载,首先是通过函数实现的。int main(){ Complex c1(1, 2), c2(3, 4); Complex c3 = c1 + c2; c3.printCom(); Complex c5 = c1 - c2 ; c5.printCom(); ++c2; c2.printCom(); --c2; c2.printCom(); c2++; c2.printCom(); c2--; c2.printCom(); return 0;}
0 0
- 标准重载代码
- 编写高质量代码——重载operator=的标准三步走
- 代码标准1-日志标准
- 关于代码重载(java)
- 一份代码编写标准
- Delphi代码标准文档
- Delphi代码标准文档
- Delphi代码标准文档
- Delphi代码标准文档
- Delphi代码标准文档
- PHP代码优化标准
- 数控机床标准M代码
- 数控机床标准M代码
- 一份代码编写标准
- delphi代码标准文档(
- Delphi代码标准文档
- MFC标准代码集
- 好代码的标准
- 设置Activity、AppcompatActivity为透明
- 团队项目_动作游戏demo(1)
- xlsx文件解析处理:openpyxl库 csv文件格式生成:csv
- HDU 4028 The time of a day By Assassin dp+离散化!
- Sum All Numbers in a Range 返回两个数字和它们之间所有数字的和
- 标准重载代码
- python学习摘要
- 天气API
- java分支结构,循环结构,方法,递归,生成API文档
- XML头详解
- ThinkPHP实现用户注册、登录模块
- 【NOIP2016提高A组集训第12场11.10】图的半径
- 基于STM32F4的小波分解(Mallat算法)程序说明
- eclipse和myeclipse一键取消所有断点