c++第六次实验报告.doc

上传人:苏美尔 文档编号:5725266 上传时间:2020-07-24 格式:DOC 页数:7 大小:64.50KB
返回 下载 相关 举报
c++第六次实验报告.doc_第1页
第1页 / 共7页
c++第六次实验报告.doc_第2页
第2页 / 共7页
c++第六次实验报告.doc_第3页
第3页 / 共7页
c++第六次实验报告.doc_第4页
第4页 / 共7页
c++第六次实验报告.doc_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《c++第六次实验报告.doc》由会员分享,可在线阅读,更多相关《c++第六次实验报告.doc(7页珍藏版)》请在三一文库上搜索。

1、1、实验目的理解运算符重载(非成员形式和成员形式)、学习重载几类运算符(+,=,!=,+,-,=等)。2、实验内容应用VC+6.0的构建一个复数类Complex,试对下列几个运算符进行重载:+,=,!=,+,-,=,其中要求要有成员重载形式和友元重载形式,而且,+运算符要求实现先加和后加两种形式。该类定义原型说明:class complexpublic:complex(double r=0,double i=0);complex &operator +(complex &c);complex operator -(complex &c);complex operator *(complex &

2、c);friend complex operator /(complex &c1,complex &c2);friend int operator =(complex &c1,complex &c2);friend int operator !=(complex &c1,complex &c2);/friend complex operator+(complex &c);complex operator+();void disp();private:double real; double imag;3、概要设计(1)实现的功能:本次实验主要是运用运算符重载来重载基类运算符,其中要求要有成员重载

3、形式和友元重载形式。(2)构造函数及函数原型complex(double r=0,double i=0);/构造函数complex &operator +(complex &c);/重载+函数complex operator -(complex &c);/重载-函数complex operator *(complex &c);/重载*函数friend complex operator /(complex &c1,complex &c2);/友元重载/函数friend int operator =(complex &c1,complex &c2);/友元重载=函数friend int opera

4、tor !=(complex &c1,complex &c2);/友元重载!=函数complex operator +();/重载先+函数complex operator +(int);/重载后+函数void disp();/输出函数4、详细设计1、complex:complex(double r,double i) real=r;imag=i;/构造函数,完成对变量的初始化2、complex &complex:operator +(complex &c) complex temp;temp.real=real+c.real;temp.imag=imag+c.imag;return temp;

5、/重载+函数3、complex complex:operator -(complex &c)complex temp; temp.real=real-c.real;temp.imag=imag-c.imag;return temp;/重载-函数4、complex complex:operator *(complex &c)complex temp;temp.real=real*c.real-imag*c.imag;temp.imag=real*c.imag+imag*c.real;return temp;/重载*函数5、complex operator /(complex &c1,comple

6、x &c2)complex temp;double t;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;return temp;/重载/函数6、int operator =(complex &c1,complex &c2)if(c1.real=c2.real&c1.imag=c2.imag)return true;elsereturn false;/重载=函数7、int operato

7、r!=(complex &c1,complex &c2)if(c1.real!=c2.real|c1.imag!=c2.imag)return true;elsereturn false;/重载!=函数8、complex complex:operator+()+real;+imag;return *this;/重载先+函数9、complex complex:operator+(int)real+;imag+;return *this;/重载后+函数10、void complex:disp()cout0)cout+;if(imag!=0)coutimagin;/输出函数 5、程序调试在整个程序的

8、调试和运行中,没有出现一些语法上的错误,就是有一些逻辑上的错误,主要是在怎样在主函数中通过输入语句输入要用的测试值的问题,但后来通过修改已经没有问题了。下面是程序测试的数据及结果:测试的结果为: 6、实验总结通过这次试验,对运算符重载的认识更一步加深了,对它的使用也进一步熟悉了,使自己在以后的实验中可以更好的使用它们来解决一些问题。但还是在本次试验中找到了自己的不足,自己不能很好的记住一些关于它们的基本知识,如一些它们常用的形式和规则,这需在日后加强。 7、附录#includeusing namespace std;class complexpublic:complex(double r=0,

9、double i=0);/构造函数complex &operator +(complex &c);/重载+函数complex operator -(complex &c);/重载-函数complex operator *(complex &c);/重载*函数friend complex operator /(complex &c1,complex &c2);/友元重载/函数friend int operator =(complex &c1,complex &c2);/友元重载=函数friend int operator !=(complex &c1,complex &c2);/友元重载!=函数

10、complex operator +();/重载先+函数complex operator +(int);/重载后+函数void disp();/输出函数private:double real; double imag;complex:complex(double r,double i) real=r;imag=i;/构造函数,完成对变量的初始化complex &complex:operator +(complex &c) complex temp;temp.real=real+c.real;temp.imag=imag+c.imag;return temp;/重载+函数complex comp

11、lex:operator -(complex &c)complex temp; temp.real=real-c.real;temp.imag=imag-c.imag;return temp;/重载-函数complex complex:operator *(complex &c)complex temp;temp.real=real*c.real-imag*c.imag;temp.imag=real*c.imag+imag*c.real;return temp;/重载*函数complex operator /(complex &c1,complex &c2)complex temp;doubl

12、e t;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;return temp;/重载/函数int operator =(complex &c1,complex &c2)if(c1.real=c2.real&c1.imag=c2.imag)return true;elsereturn false;/重载=函数int operator!=(complex &c1,complex &c2)

13、if(c1.real!=c2.real|c1.imag!=c2.imag)return true;elsereturn false;/重载!=函数complex complex:operator+()+real;+imag;return *this;/重载先+函数complex complex:operator+(int)real+;imag+;return *this;/重载后+函数void complex:disp()cout0)cout+;if(imag!=0)coutimagin;/输出函数int main()complex A1(2.3,4.6),A2(3.6,2.8),A3,A4,

14、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();return 0;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1