您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 交通运输 > c++第六次实验报告
1、实验目的理解运算符重载(非成员形式和成员形式)、学习重载几类运算符(++,=,!=,+,-,==等)。2、实验内容应用VC++6.0的构建一个复数类Complex,试对下列几个运算符进行重载:++,=,!=,+,-,==,其中要求要有成员重载形式和友元重载形式,而且,++运算符要求实现先加和后加两种形式。该类定义原型说明:classcomplex{public:complex(doubler=0,doublei=0);complex&operator+(complex&c);complexoperator-(complex&c);complexoperator*(complex&c);friendcomplexoperator/(complex&c1,complex&c2);friendintoperator==(complex&c1,complex&c2);friendintoperator!=(complex&c1,complex&c2);//friendcomplexoperator++(complex&c);complexoperator++();voiddisp();private:doublereal;doubleimag;};3、概要设计(1)实现的功能:本次实验主要是运用运算符重载来重载基类运算符,其中要求要有成员重载形式和友元重载形式。(2)构造函数及函数原型complex(doubler=0,doublei=0);//构造函数complex&operator+(complex&c);//重载+函数complexoperator-(complex&c);//重载-函数complexoperator*(complex&c);//重载*函数friendcomplexoperator/(complex&c1,complex&c2);//友元重载/函数friendintoperator==(complex&c1,complex&c2);//友元重载==函数friendintoperator!=(complex&c1,complex&c2);//友元重载!=函数complexoperator++();//重载先++函数complexoperator++(int);//重载后++函数voiddisp();//输出函数4、详细设计1、complex::complex(doubler,doublei){real=r;imag=i;}//构造函数,完成对变量的初始化2、complex&complex::operator+(complex&c){complextemp;temp.real=real+c.real;temp.imag=imag+c.imag;returntemp;}//重载+函数3、complexcomplex::operator-(complex&c){complextemp;temp.real=real-c.real;temp.imag=imag-c.imag;returntemp;}//重载-函数4、complexcomplex::operator*(complex&c){complextemp;temp.real=real*c.real-imag*c.imag;temp.imag=real*c.imag+imag*c.real;returntemp;}//重载*函数5、complexoperator/(complex&c1,complex&c2){complextemp;doublet;t=1/(c2.real*c2.real+c2.imag*c2.imag);temp.real=(c1.real*c2.real+c1.imag*c2.imag)*t;temp.imag=(c2.real*c1.imag-c1.real*c2.imag)*t;returntemp;}//重载/函数6、intoperator==(complex&c1,complex&c2){if(c1.real==c2.real&&c1.imag==c2.imag)returntrue;elsereturnfalse;}//重载==函数7、intoperator!=(complex&c1,complex&c2){if(c1.real!=c2.real||c1.imag!=c2.imag)returntrue;elsereturnfalse;}//重载!=函数8、complexcomplex::operator++(){++real;++imag;return*this;}//重载先++函数9、complexcomplex::operator++(int){real++;imag++;return*this;}//重载后++函数10、voidcomplex::disp(){coutreal;if(imag0)cout+;if(imag!=0)coutimagi\n;}//输出函数5、程序调试在整个程序的调试和运行中,没有出现一些语法上的错误,就是有一些逻辑上的错误,主要是在怎样在主函数中通过输入语句输入要用的测试值的问题,但后来通过修改已经没有问题了。下面是程序测试的数据及结果:测试的结果为:6、实验总结通过这次试验,对运算符重载的认识更一步加深了,对它的使用也进一步熟悉了,使自己在以后的实验中可以更好的使用它们来解决一些问题。但还是在本次试验中找到了自己的不足,自己不能很好的记住一些关于它们的基本知识,如一些它们常用的形式和规则,这需在日后加强。7、附录#includeiostreamusingnamespacestd;classcomplex{public:complex(doubler=0,doublei=0);//构造函数complex&operator+(complex&c);//重载+函数complexoperator-(complex&c);//重载-函数complexoperator*(complex&c);//重载*函数friendcomplexoperator/(complex&c1,complex&c2);//友元重载/函数friendintoperator==(complex&c1,complex&c2);//友元重载==函数friendintoperator!=(complex&c1,complex&c2);//友元重载!=函数complexoperator++();//重载先++函数complexoperator++(int);//重载后++函数voiddisp();//输出函数private:doublereal;doubleimag;};complex::complex(doubler,doublei){real=r;imag=i;}//构造函数,完成对变量的初始化complex&complex::operator+(complex&c){complextemp;temp.real=real+c.real;temp.imag=imag+c.imag;returntemp;}//重载+函数complexcomplex::operator-(complex&c){complextemp;temp.real=real-c.real;temp.imag=imag-c.imag;returntemp;}//重载-函数complexcomplex::operator*(complex&c){complextemp;temp.real=real*c.real-imag*c.imag;temp.imag=real*c.imag+imag*c.real;returntemp;}//重载*函数complexoperator/(complex&c1,complex&c2){complextemp;doublet;t=1/(c2.real*c2.real+c2.imag*c2.imag);temp.real=(c1.real*c2.real+c1.imag*c2.imag)*t;temp.imag=(c2.real*c1.imag-c1.real*c2.imag)*t;returntemp;}//重载/函数intoperator==(complex&c1,complex&c2){if(c1.real==c2.real&&c1.imag==c2.imag)returntrue;elsereturnfalse;}//重载==函数intoperator!=(complex&c1,complex&c2){if(c1.real!=c2.real||c1.imag!=c2.imag)returntrue;elsereturnfalse;}//重载!=函数complexcomplex::operator++(){++real;++imag;return*this;}//重载先++函数complexcomplex::operator++(int){real++;imag++;return*this;}//重载后++函数voidcomplex::disp(){coutreal;if(imag0)cout+;if(imag!=0)coutimagi\n;}//输出函数intmain(){complexA1(2.3,4.6),A2(3.6,2.8),A3,A4,A5,A6,A7,A8,A9,A10;A3=A1+A2;A4=A1-A2;cout-------------两个复数分别是-------------endl;A1.disp();A2.disp();cout-------------两个复数和为----------------endl;A3.disp();cout-------------两个复数差为----------------endl;A4.disp();A5=A1*A2;cout-------------两个复数乘积为----------------endl;A5.disp();A6=A1/A2;cout-------------两个复数相除为----------------endl;A6.disp();cout-------------在==运算符下输出值为------------------endl;A7=A1==A2;A7.disp();coutendl-------------在=运算符下输出值为-----------------endl;A8=A1!=A2;A8.disp();A9=++A1;coutendl;cout------------在前面++后值为-----------------endl;A9.disp();A10=A2++;cout------------在后面++后值为-----------------endl;A10.disp();return0;}
本文标题:c++第六次实验报告
链接地址:https://www.777doc.com/doc-5790051 .html