欢迎来到天天文库
浏览记录
ID:37714979
大小:17.05 KB
页数:8页
时间:2019-05-29
《《C++面向对象程序设计》答案-第四章--谭浩强-清华大学出版社》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、1:#includeusingnamespacestd;classComplex{public:Complex(){real=0;imag=0;}Complex(doubler,doublei){real=r;imag=i;}doubleget_real();doubleget_imag();voiddisplay();private:doublereal;doubleimag;};doubleComplex::get_real(){returnreal;}doubleComplex::get_imag(){re
2、turnimag;}voidComplex::display(){cout<<"("<3、includeusingnamespacestd;classComplex{public:Complex(){real=0;imag=0;}Complex(doubler,doublei){real=r;imag=i;}Complexoperator+(Complex&c2);Complexoperator-(Complex&c2);Complexoperator*(Complex&c2);Complexoperator/(Complex&c2);voiddisplay();private:doublereal;4、doubleimag;};ComplexComplex::operator+(Complex&c2){Complexc;c.real=real+c2.real;c.imag=imag+c2.imag;returnc;}ComplexComplex::operator-(Complex&c2){Complexc;c.real=real-c2.real;c.imag=imag-c2.imag;returnc;}ComplexComplex::operator*(Complex&c2){Complexc;c.real=real*c2.re5、al-imag*c2.imag;c.imag=imag*c2.real+real*c2.imag;returnc;}ComplexComplex::operator/(Complex&c2){Complexc;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);returnc;}voidComplex::disp6、lay(){cout<<"("<//用VC++时7、改为∶#includeusingnamespacestd;//用VC++时为取消此行classComplex{public:Complex(){real=0;imag=0;}Complex(doubler,doublei){real=r;imag=i;}Complexoperator+(Complex&c2);Complexoperator+(int&i);friendComplexoperator+(int&,Complex&);voiddisplay();private:doublereal;double8、imag;};ComplexComplex::operator+(Complex&c){returnComplex(real+c.real,imag+c.imag);}ComplexComplex::operator+(int&i){
3、includeusingnamespacestd;classComplex{public:Complex(){real=0;imag=0;}Complex(doubler,doublei){real=r;imag=i;}Complexoperator+(Complex&c2);Complexoperator-(Complex&c2);Complexoperator*(Complex&c2);Complexoperator/(Complex&c2);voiddisplay();private:doublereal;
4、doubleimag;};ComplexComplex::operator+(Complex&c2){Complexc;c.real=real+c2.real;c.imag=imag+c2.imag;returnc;}ComplexComplex::operator-(Complex&c2){Complexc;c.real=real-c2.real;c.imag=imag-c2.imag;returnc;}ComplexComplex::operator*(Complex&c2){Complexc;c.real=real*c2.re
5、al-imag*c2.imag;c.imag=imag*c2.real+real*c2.imag;returnc;}ComplexComplex::operator/(Complex&c2){Complexc;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);returnc;}voidComplex::disp
6、lay(){cout<<"("<//用VC++时
7、改为∶#includeusingnamespacestd;//用VC++时为取消此行classComplex{public:Complex(){real=0;imag=0;}Complex(doubler,doublei){real=r;imag=i;}Complexoperator+(Complex&c2);Complexoperator+(int&i);friendComplexoperator+(int&,Complex&);voiddisplay();private:doublereal;double
8、imag;};ComplexComplex::operator+(Complex&c){returnComplex(real+c.real,imag+c.imag);}ComplexComplex::operator+(int&i){
此文档下载收益归作者所有