第九章——<<>>的重载 (额这是怎么回事)
来源:互联网 发布:淘宝服务端数据为空 编辑:程序博客网 时间:2024/04/30 06:01
#include <iostream>using namespace std;class Complex{public: Complex(){real=0;imag=0;} friend ostream& operator<< (ostream& output, Complex& c); friend istream& operator>> (istream& input,Complex& c); Complex(double r,double i){real=r; imag=i;} Complex operator+(Complex &c2); Complex operator-(Complex &c2); Complex operator-(); Complex operator*(Complex &c2); Complex operator/(Complex &c2); void display();private: double real; double imag;};//下面定义成员函数ostream& operator<< (ostream& output, Complex& c){ output<<"("<<c.real; if (c.imag>0) output<<"+"<<c.imag<<"i)";}istream& operator>> (istream& input,Complex& c){ char ch; input>>c.real>>ch; if (ch!='-'||ch!='+') { cout<<"请输入+或者-"<<endl; operator>> (input,c); }}Complex Complex::operator-(){ return -*this;}Complex Complex::operator+(Complex &c2){ Complex c; c.real=real+c2.real; c.imag=imag+c2.imag; return c;}Complex Complex::operator-(Complex &c2){ Complex c; c.real=real-c2.real; c.imag=imag-c2.imag; return c;}Complex Complex::operator*(Complex &c2){ Complex c; c.real=real*c2.real-imag*c2.imag; c.imag=imag*c2.real+real*c2.imag; return c;}Complex Complex::operator/(Complex &c2){ Complex c; c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); c.imag=(imag*c2.real+real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); return c;}//下面定义用于测试的main()函数int main(){ Complex c1(3,4),c2(5,-10),c3; cout<<"c1="; cout<<c1<<endl; cout<<"c2="; cout<<c2<<endl; c3=c1+c2; cout<<"c1+c2="; cout<<c3<<endl; c3=c1-c2; cout<<"c1-c2="; cout<<c3<<endl; c3=c1*c2; cout<<"c1*c2="; cout<<c3<<endl; c3=c1/c2; cout<<"c1/c2="; cout<<c3<<endl; return 0;}
1 0
- 第九章——<<>>的重载 (额这是怎么回事)
- 第九周——<<>>的重载(正常了)
- 15第九周项目二——Time类中的运算符的重载(续)
- 15第九周项目三——分数类中的运算符的重载(续)
- 第九周—2.Time类(运用<<和>>重载)
- 第九周—1.Complex类(运用<<和>>的重载)
- c++第九章-(运算符重载)
- 第九章 重载
- CSS—单位是怎么回事
- 函数重载,这里是怎么回事?
- 第九周——时间类和输入输出运算符的重载
- 第九周项目二——Time类中的运算符重载(利用输入输出运算符重载)
- 第九周项目三——分数类中的运算符重载(利用输入输出运算符重载)
- 您的文章已经被推荐到CSDN首页业界评点栏目——这是怎么回事?
- 第九周——运算符重载——阅读项目(一)学生管理系统
- 第九周——运算符重载——项目一复数类(续)
- 第九周——运算符重载——项目三分数类(续)
- 第九周项目一——Complex类(利用输入输出运算符重载)
- Web性能测试工具
- UIView不停旋转
- hdu 2492 Ping pong(线段树+思维)
- Ruby的多继承
- 第九周上机实践项目——更自然实现复数类中的运算符重载
- 第九章——<<>>的重载 (额这是怎么回事)
- Ubuntu 14.04 使用VirtualBox 4.3.10 虚拟 Windows 7
- HDU 1392 Surround the Trees(纯裸的凸包)
- makefile missing separator
- drop table purge
- TF-IDF(term frequency–inverse document frequency)
- kindle touch 修复
- cocos2dx引擎架构图
- 如何让UIView不停旋转