首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
阅读以下说明和C++代码(代码13-1),将应填入(n)处的字句写在对应栏内。 【说明】 软件设计师东方飞龙利用UML设计了一个迷你小型复数类,其类图如图13-11所示。 【代码13-l】 /*________________________
阅读以下说明和C++代码(代码13-1),将应填入(n)处的字句写在对应栏内。 【说明】 软件设计师东方飞龙利用UML设计了一个迷你小型复数类,其类图如图13-11所示。 【代码13-l】 /*________________________
admin
2010-01-15
22
问题
阅读以下说明和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 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不能缺少,否则不能运行。
转载请注明原文地址:https://kaotiyun.com/show/TcDZ777K
本试题收录于:
软件设计师下午应用技术考试题库软考中级分类
0
软件设计师下午应用技术考试
软考中级
相关试题推荐
下面关于程序语言的叙述,错误的是(22)。
关于汇编语言,以下叙述中正确的是(20)。
假设某公司营销系统有营销点关系S(营销点,负责人姓名,联系方式)、商品关系P(商品名,条形码,型号,产地,数量,价格),其中,营销点唯一标识S中的每一个元组。每个营销点可以销售多种商品,每一种商品可以由不同的营销点销售。关系S和P的主键分别为(15),S
将高级语言程序翻译为机器语言程序的过程中,常引入中间代码,其好处是()。
下图是一个软件项目的活动图,其中顶点表示项目里程碑,连接顶点的边表示包含的活动,边上的值表示完成活动所需要的时间,则关键路径长度为______。
对某商店业务处理系统采用数据流图(DFD)进行功能建模,其中“检查订货单”是其中的一个①。由于在进行订货单检查时,需要根据客户的欠款情况、订单金额等多个条件判断是否采取发出催款单、准备货物、发出发货单等行为,此时适合采用②进行描述。②处
对某商店业务处理系统采用数据流图(DFD)进行功能建模,其中“检查订货单”是其中的一个①。由于在进行订货单检查时,需要根据客户的欠款情况、订单金额等多个条件判断是否采取发出催款单、准备货物、发出发货单等行为,此时适合采用②进行描述。①处
对于下面的有向图,其邻接矩阵是一个①的矩阵。采用邻接链表存储时,顶点0的表结点个数为2,顶点3的表结点个数为0,顶点1的表结点个数为②个。②处应填入?
函数main()、f()的定义如下所示。调用函数f()时,第一个参数采用传值(callbyvalue)方式,第二个参数采用传引用(callbyreference)方式,main()执行后输出的值为______。
对于关键字序列(10,34,37,51,14,25,56,22,3),用线性探查法解决冲突构造哈希表,哈希函数为H(key)=key%11,关键字25存入的哈希地址编号为________________。
随机试题
甘油三酯是在_______合成。
( )为强效、长效抗炎镇痛药。( )属于吡唑酮类抗炎抗风湿药。
拆除高度()m以上的模板时,应搭设脚手架,并设防身栏,防止上下在同一垂直面操作。
水塔基础与塔身划分()。
关于基本分析法,下列说法错误的是()。
下列各项指标中,属于评价盈利能力状况基本指标的是()。
甲为某市副市长,在代表该市到其他省、市进行招商引资活动中多次接受对方省、市赠送的礼品,价值50万元,应当交公而没有交公。甲的行为构成()。
“以铜为镜,可以正衣冠;以古为镜,可以知兴替;以人为镜,可以明得失。”《贞观政要》中这句富含哲理的名言给我们的启迪是()
whatistheconversationmainlyabout?
Heresented_________(听从比自己年轻的人的调遗)forwhomhehadnorespect.
最新回复
(
0
)