资源描述:
《C++中的复数类.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、......C++中的复数类//类的定义(mycomplex.h)#include#ifndefmycomplex_h#definemycomplex_hclasscomplex{private:floatreal;floatimage;public:complex(float=0.0,float=0.0);//构造函数complex(constcomplex&c);//拷贝构造函数//以下为运算函数complexadd(constcomplex&c)const;//加运算complexs
2、ub(constcomplex&c)const;//减运算complexmul(constcomplex&c)const;//乘运算complexdiv(constcomplex&c)const;//除运算//以下为普通成员函数floatgetreal(void)const;//获取实部floatgetimage(void)const;//获取虚部voidsetcomplex(floatr,floati);//重新设定复数值.专业专注.......//以下为输出输入函数重载friendostream&operat
3、or<<(ostream&out,constcomplex&c);//输出重载friendistream&operator>>(istream&in,complex&c);//输入重载//以下为运算符重载friendcomplexoperator+(floatr,constcomplex&c);//friendcomplexoperator+(intr,constcomplex&c);friendcomplexoperator-(floatr,constcomplex&c);friendcomplexoperat
4、or-(intr,constcomplex&c);friendcomplexoperator(floatr,constcomplex&c);friendcomplexoperator(intr,constcomplex&c);friendcomplexoperator+(constcomplex&c,floatr);friendcomplexoperator+(constcomplex&c,intr);friendcomplexoperator-(constcomplex&c,floatr);friendcomp
5、lexoperator-(constcomplex&c,intr);friendcomplexoperator(constcomplex&c,floatr);friendcomplexoperator(constcomplex&c,intr);friendcomplexoperator/(floatr,constcomplex&c);friendcomplexoperator/(intr,constcomplex&c);friendcomplexoperator/(constcomplex&c,floatr);f
6、riendcomplexoperator/(constcomplex&c,intr);friendcomplexoperator+(constcomplex&c1,constcomplex&c2);.专业专注.......friendcomplexoperator-(constcomplex&c1,constcomplex&c2);friendcomplexoperator(constcomplex&c1,constcomplex&c2);friendcomplexoperator/(constcomplex&c
7、1,constcomplex&c2);};//定义虚数单位ieconstcomplexie(0,1);#endif//类的实现(mycomplex.cpp)#include#include"mycomplex.h"//构造函数complex::complex(floatr,floati){real=r;image=i;}//拷贝构造函数.专业专注.......complex::complex(constcomplex&c){real=c.real;image=c.image;}//加运算c
8、omplexcomplex::add(constcomplex&c)const{complext;t.real=real+c.real;t.image=image+c.image;returnt;}//减运算complexcomplex::sub(constcomplex&c)const{complext;t.real=real-c.real;t.image=image-