欢迎来到天天文库
浏览记录
ID:56770976
大小:64.50 KB
页数:7页
时间:2020-07-08
《c++第六次实验报告.doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、1、实验目的理解运算符重载(非成员形式和成员形式)、学习重载几类运算符(++,=,!=,+,-,==等)。2、实验内容应用VC++6.0的构建一个复数类Complex,试对下列几个运算符进行重载:++,=,!=,+,-,==,其中要求要有成员重载形式和友元重载形式,而且,++运算符要求实现先加和后加两种形式。该类定义原型说明:classcomplex{public:complex(doubler=0,doublei=0);complex&operator+(complex&c);complexoperator-(c
2、omplex&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;dou
3、bleimag;};3、概要设计(1)实现的功能:本次实验主要是运用运算符重载来重载基类运算符,其中要求要有成员重载形式和友元重载形式。(2)构造函数及函数原型complex(doubler=0,doublei=0);//构造函数complex&operator+(complex&c);//重载+函数complexoperator-(complex&c);//重载-函数complexoperator*(complex&c);//重载*函数friendcomplexoperator/(complex&c1,compl
4、ex&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;}//构
5、造函数,完成对变量的初始化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::
6、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.im
7、ag)*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
8、
9、c1.imag!=c2.imag
10、)returntrue;elsereturnfalse;}//重载!=函数8、complexcomplex::operator++(){++real;++imag;return*this;}//重载先++函数9、complexcomplex::operator++(int){real++;imag++;return*this;}//重载后++函数10、voi
此文档下载收益归作者所有