欢迎来到天天文库
浏览记录
ID:52563620
大小:65.96 KB
页数:14页
时间:2020-03-28
《c++运算符重载习题.docx》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、Task8-1/*1.定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。编写程序,求两个复数之和*/#includeusingnamespacestd;classComplex{public:Complex(){real=0;imag=0;}Complex(doubler,doublei){real=r;imag=i;}voiddisplay();doublereal;doubleimag;};voidComplex::display(){cout<<"("<2、"<3、除。运算符重载函数作为Complex类的成员函数,编程,分别求两个复数之和差积商。*/#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);voidd4、isplay();private:doublereal;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.rea5、l=real*c2.real;c.imag=imag*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::display(){cout<<"("<6、ag<<"i)"<7、之能用于矩阵向加减乘除,如c=a+b、c=a*b。*/#include#definen2#definem3usingnamespacestd;classMatrix//定义Matrix类{public:Matrix();//默认构造函数friendMatrixoperator+(Matrix&,Matrix&);//重载运算符“+”friendMatrixoperator-(Matrix&,Matrix&);friendMatrixoperator*(Matrix&,Matrix&);friendMatrixoperator/(Matrix&,8、Matri
2、"<3、除。运算符重载函数作为Complex类的成员函数,编程,分别求两个复数之和差积商。*/#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);voidd4、isplay();private:doublereal;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.rea5、l=real*c2.real;c.imag=imag*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::display(){cout<<"("<6、ag<<"i)"<7、之能用于矩阵向加减乘除,如c=a+b、c=a*b。*/#include#definen2#definem3usingnamespacestd;classMatrix//定义Matrix类{public:Matrix();//默认构造函数friendMatrixoperator+(Matrix&,Matrix&);//重载运算符“+”friendMatrixoperator-(Matrix&,Matrix&);friendMatrixoperator*(Matrix&,Matrix&);friendMatrixoperator/(Matrix&,8、Matri
3、除。运算符重载函数作为Complex类的成员函数,编程,分别求两个复数之和差积商。*/#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);voidd
4、isplay();private:doublereal;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.rea
5、l=real*c2.real;c.imag=imag*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::display(){cout<<"("<6、ag<<"i)"<7、之能用于矩阵向加减乘除,如c=a+b、c=a*b。*/#include#definen2#definem3usingnamespacestd;classMatrix//定义Matrix类{public:Matrix();//默认构造函数friendMatrixoperator+(Matrix&,Matrix&);//重载运算符“+”friendMatrixoperator-(Matrix&,Matrix&);friendMatrixoperator*(Matrix&,Matrix&);friendMatrixoperator/(Matrix&,8、Matri
6、ag<<"i)"<7、之能用于矩阵向加减乘除,如c=a+b、c=a*b。*/#include#definen2#definem3usingnamespacestd;classMatrix//定义Matrix类{public:Matrix();//默认构造函数friendMatrixoperator+(Matrix&,Matrix&);//重载运算符“+”friendMatrixoperator-(Matrix&,Matrix&);friendMatrixoperator*(Matrix&,Matrix&);friendMatrixoperator/(Matrix&,8、Matri
7、之能用于矩阵向加减乘除,如c=a+b、c=a*b。*/#include#definen2#definem3usingnamespacestd;classMatrix//定义Matrix类{public:Matrix();//默认构造函数friendMatrixoperator+(Matrix&,Matrix&);//重载运算符“+”friendMatrixoperator-(Matrix&,Matrix&);friendMatrixoperator*(Matrix&,Matrix&);friendMatrixoperator/(Matrix&,
8、Matri
此文档下载收益归作者所有