c++运算符重载
来源:互联网 发布:js 数组长度限制 编辑:程序博客网 时间:2024/05/01 06:42
<span style="font-size:18px;">#include<iostream>using namespace std;class Complex{public: Complex(){real=0,image=0;}; Complex(double,double); Complex operator++(int); Complex& operator++(); Complex& operator--(); Complex operator+(const Complex &); Complex operator-(const Complex &); Complex operator*(const Complex &); //Complex& operator+=(Complex &); //Complex& operator-=(Complex &); //iostream & operator<<(iostream &); //iostream & operator>>(iostream &); double getReal(); double getimage();//private:double real;double image;};Complex::Complex(double r,double i){ real=r; image=i;};Complex& Complex::operator++(){ real++;image++;return *this;}Complex Complex::operator++(int a){ Complex temp=*this;real++;image++;return temp;}Complex& Complex::operator--(){ real--;image--;return *this;};Complex Complex::operator+(const Complex &a){ return Complex(real+a.real,image+a.image);};Complex Complex::operator-(const Complex &a){ return Complex(real-a.real,image-a.image);}; Complex Complex::operator*(const Complex &a){ return Complex(real*a.real,image*a.image);};/*Complex& Complex::operator+=(Complex &a){ real+=a.real; image+=a.image; return *this;};*//*Complex& Complex::operator-=(Complex &a){ real-=a.real; image-=a.image; return *this;};*/Complex& operator+=(Complex &a,const Complex &b){ a.real+=b.real; a.image+=b.image; return a;};Complex& operator-=(Complex &a,const Complex &b){ a.real-=b.real; a.image-=b.image; return a;};ostream &operator<<( ostream &out,const Complex &a){ //out<<"("<<a.real<","<<a.image<<")"<<endl; out<<"("<<a.real<<","<<a.image<<")"; return out;};istream &operator>>(istream &in,Complex &a){in>>a.real>>a.image; return in;};int main(){ Complex a; cout<<"请输入一个复数:"<<endl; cin>>a; cout<<"你输入的数是:"<<a<<endl; Complex b; cout<<"a:"<<a<<" b:"<<b<<endl; b=a++; cout<<"b=a++ :"<<b<<endl; cout<<"a:"<<a<<" b:"<<b<<endl; b=++a; cout<<"b=++a :"<<b<<endl; b=Complex(10,10); Complex c; c=a+b; cout<<"a:"<<a<<" b:"<<b<<endl; cout<<"a+b:"<<c<<endl; c=a-b; cout<<"a:"<<a<<" b:"<<b<<endl; cout<<"a-b: "<<c<<endl; c=a*b; cout<<"a:"<<a<<" b:"<<b<<endl; cout<<"a*b: "<<c<<endl; cout<<"a:"<<a<<" b:"<<b<<endl; a+=b; cout<<"a+=b: "<<a<<endl; cout<<"a:"<<a<<" b:"<<b<<endl; a-=b; cout<<"a-=b: "<<a<<endl;//之前我定义了一个条件编译条件没想到 编译器中居然有相同的宏 结果老是编译不过去 坑!!//#ifndef COMPLEX_H//cout<<"error!!"<<endl;//#endif return 0; }</span>
0 0
- C++-运算符重载
- [C++]重载运算符
- C#:运算符重载
- c++-++运算符重载
- C++:重载运算符
- 运算符重载(C++)
- [c++]运算符重载
- 【C++】运算符重载
- C++--------------------------------------------运算符重载
- C#:运算符重载
- C++:运算符重载
- C++:运算符重载
- C#:运算符重载
- [C++]运算符重载
- 【c++】运算符重载
- C++:运算符重载
- C#:运算符重载
- C++:运算符重载
- Sqlite3 FMDB 库 使用注意
- Codeforces Round #269 (Div. 2) D题 MUH and Cube Walls(KMP)
- 用R软件绘制中国分省市地图
- 三极管的基极电流究竟做了什么?
- Java中Runnable和Thread的区别
- c++运算符重载
- linux系统编程第一章
- BitTorrent协议小结
- JVM内存模型及垃圾收集策略解析(1)
- 无所谓的事情
- Win10和他的老兄弟们
- 玩的第一个黑客小游戏,isa test
- c语言对数组名取地址
- JVM内存模型及垃圾收集策略解析