【说明】软件设计师东方飞龙利用UML设计了一个迷你小型复数类,其类图如图13-11所示。
【代码13-l】 /*___________________________________*/ /********* 文件 MiniComplex. h*********/ /*___________________________________*/ #include<iostream> using namespace std; class MiniComplex { (1) : //重载流插入和提取运算符 (2) ostream & operator <<(ostream & osObject, const MiniComplex & complex) { osObject <<"("<<complex. realPart<<"+"<<complex. imagPart <<"I"<<")"; return osObject; } friend (3) operator >>(istream & isObject, MiniComplex & complex) { char ch; isObject >>complex. realPart >>ch>>complex. imagPart >>ch; return isObject; } MiniComplex(double real=0, double imag=0); //构造函数 MiniComplex operator+(const MiniComplex & otherComplex)const! //重载运算符+ MiniComplex operator--(const MiniComplex & otherComplex)const! //重载运算符- MiniComplex operator*(const MiniComplex& othmComplex)const; //重载运算符* MiniComplex operator/(const MiniComplex & otherComplex)const; //重载运算符/ bool operator==(const MiniComplex &otherComplex)const; //重载运算符== private: double realPart; //存储实部变量 double imagPart; //存储虚部变量 }; /*_______________________________________________________*/ /* * * * * * * * *文件 MiniComplex. cpp* * * * * * * * * */ /*_______________________________________________________*/ # include "MiniComplex.h" bool MiniComplex:: operator==(const MiniComplex & otherComplex)const { (1) ;} MiniComplex:: MiniComplex(double real, double imag){realPart=real;imagPart=imag!} MiniComplex MiniComplex:: operator+(const MiniComplex & otherComplex)const { MiniComplex temp; temp. realPart=realPart+ otherComplex. realPart; temp. imagPart=imagPart+ otherComplex. imagPart; return temp; } MiniComplex MiniComplex::operator--(const MiniComplex & otherComplex)const { MiniComplex temp; temp.realPart=realPart-otherComplex.realPart; temp. imagPart=imagPart-otherCompler.imagPart; return temp; } MiniComplex MiniComplex:: operator*(const MiniComplex& otherComplex)const { MiniComplex temp; temp.realPart=(realPart* otherComplex.realPart)-(imag-Part* otherComplex.imag-Part); temp imagPart=(realPart* otherComplex. imagPart)+(imag-Part *otherComplex.realPart); return temp, } MiniComplex MiniComplex:: operator/(const MiniComplex& otherComplex)eonst { MiniComplex temp; float tt; tt=1/(otherComplex. realPart *otherComplex. realPart+otherComplex. imagPart* other Complex.imagPart); temp. realPart=((realPart* otherComplex.realPart)+(imagPart* otherComplex.imagPart))*tt; temp. imagPart=((imagPart * otherComplex.realPart)-(realPart* otherComplex.imagPart))*tt; return temp; } /*__________________________________________________*/ /* * * * * * * *主函数所在文件main.cpp* * * * * * * */ /*_________________________________________________*/ # include<iostream> # include " (5) " using namespace std; int main(void) { MiniComplex num1(23, 34), num2; cin>>num2; cout<<"Initial Value of Numl="<<num1<<"\nInitial Value of Num2="<<num2<<end1; cout<<num1<<"+"<<num2<<"="<<num1+num2<<end1; //使用重载的加号运算符 cout<<num1<<"-"<<num2<<"="<<num1-num2<<end1; //使用重载的减号运算符 cout<<num1<<"*"<<num2<<"-"<<num1*num2<<end1; //使用重载的乘号运算符 cout<<num1<<"/"<<num2<<"="<<num1/num2<<end1; //使用重载的除号运算符 return 0; }
参考答案:
解析:(1)public(2)friend(3)istream&. (4)return(realPart==otherComplex. realPart && imagPart==otherComplex. imagPart) (5)MiniComplex.h 根据UML的类图可知,该迷你小型复数类有两个属性realPart、imagPart,分别表示复数的实部和虚部。它还重载了输出流和输入流运算符,而且重载了加、减、乘、除以及等于这5种运算符。以方法“+operator+(otherComplex:const MiniComplex&):MiniComplex”为例来说明其表述的方式:最前面的“+”号表示该方法是公有的(若是“-”号则表示是私有的,若是“#”则表示是保护的);otherComplex是该方法的参数,const MiniComplex&是该参数的类型;最后的MiniComplex表示该方法的返回类型。 通过上述分析可知,(1)空显然填public,因为各方法及构造函数均是公有的。在 operator<<的定义体内,发现使用了参数complex的属性realPart和imagPart,并对比 operator>>可知,operator<<是MiniComplex的友元函数,因此第(2)空显然应填 friend。(3)空显然是要填operator>>的返回类型,根据UML类图可知填istream&。两个复数的实部和虚部均相等时两复数相等,因此,第(4)空填“return(realPart==other Complex.realPart &&imagPart==otherComplex.imagPart);”,注意,不能丢分号。在使用一个类时,我们只要在文件中包含它的头文件即可,于是第(5)空填MiniComplex.h。 运行上述程序,输入复数56+35i,可得运行结果如下: 56+35i Initial Value of Num1=<23+34i> Initial Value of Num2=<56+35i> <23+34i>+<56+35i>=<79+69i> <23+34i>-<56+35i>=<-33+-1i> <23+34i>*<56+35i>=<98+2709i> <23+34i>/<56+35i>=<0.568218+0.252006i> 注意,各文件必须放在同一个工程之内,而且operator<<和operator>>的友元声明关键字:friend不能缺少,否则不能运行。