阅读以下说明和C++代码(代码13-1),将应填入(n)处的字句写在对应栏内。 【说明】软件设计师东方飞龙
阅读以下说明和C++代码(代码13-1),将应填入(n)处的字句写在对应栏内。
【说明】
软件设计师东方飞龙利用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 perator==(const MiniComplex &otherComplex)const; //重载运算符==
private:
double realPart; //存储实部变量
double imagPart; //存储虚部变量
};
/*_______________________________________________________*/
/* * * * * * * * *文件 MiniComplex. cpp* * * * * * * * * */
/*_______________________________________________________*/
include "MiniComplex.h"
bool MiniComplex:: perator==(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;<
(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不能缺少,否则不能运行。
你是怎么去设法说服这些学生修读快速阅读课的翻译成英文 英语 2020-05-02 …
设P(x+a,y1),Q(x,y2),R(2+a,y3)是函数f(x)=y的反函数图象上不同的三点 数学 2020-05-02 …
下图是为“5.12”大地震设计的公益海报,请你对设计元素“5.12、爱心、蜡烛、黑丝带”的设计意图 语文 2020-05-16 …
阅读下面关于项目工作管理系统的数据库设计说明,回答问题1至问题3。【说明】 E软件开发公司,决定开 计算机类考试 2020-05-26 …
阅读下列系统设计说明,回答问题1至问题3,将解答填入答题纸的对应栏内。 【说明】 某玩具公司正在 计算机类考试 2020-05-26 …
阅读以下关于项目工作管理系统的数据库设计说明,根据要求回答问题1~问题4。 [说明] 某软件开发公 计算机类考试 2020-05-26 …
阅读以下某工厂人事信息管理系统数据库的设计说明,根据要求回答问题1~问题4。 [说明] 某工厂有多 计算机类考试 2020-05-26 …
阅读以下某仓储超市进、销、存数据库管理系统的设计说明,根据要求回答问题1~问题5。 [说明] 某仓储 计算机类考试 2020-05-26 …
为什么A1x+B1y+C1+λ(A2x+B2y+C2)=0就表示过两条直线交点的直线系;这条直线系 数学 2020-07-09 …
例:某别墅四坡水屋面如图所示,试计算该屋面斜面面积为多少.阅读该工程设计说明及施工图得知:设计图标 数学 2020-07-28 …