首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
阅读以下说明和C++代码(代码13-1),将应填入(n)处的字句写在对应栏内。 【说明】 软件设计师东方飞龙利用UML设计了一个迷你小型复数类,其类图如图13-11所示。 【代码13-l】 /*________________________
阅读以下说明和C++代码(代码13-1),将应填入(n)处的字句写在对应栏内。 【说明】 软件设计师东方飞龙利用UML设计了一个迷你小型复数类,其类图如图13-11所示。 【代码13-l】 /*________________________
admin
2010-01-15
63
问题
阅读以下说明和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
软件设计师下午应用技术考试
软考中级
相关试题推荐
“面向对象技术中,类之间共享属性与行为的机制称为()。
数据库系统通常采用三级模式结构:外模式、模式和内模式。这三级模式分别对应数据库的__________。
为了解系统在何种服务级别下会崩溃,应进行___________。
某企业研发信息系统的过程中,______不属于数据库管理员(DBA)的职责。
______不属于易用性测试范畴。A.软件产品使用户能理解软件是否合适以及如何能将软件用于特定的任务和使用条件的能力B.软件产品使用户能操作和控制它的能力C.对软件中的缺陷或失效原因进行诊断,或识别待修改部分的能力D.软件产品吸引用户的能力
下图是一个软件项目的活动图,其中顶点表示项目里程碑,连接顶点的边表示包含的活动,边上的值表示完成活动所需要的时间,则关键路径长度为______。
银行系统数据流图中,某个加工根据客户的多个不同属性的值来执行不同的操作,则对该加工最适宜采用()描述。
传统编译器进行词法分析、语法分析、代码生成等步骤的处理时,前一阶段处理的输出是后一阶段处理的输入,则采用的软件体系结构风格是①。该体系结构的优点不包括②。②处应填入?
Bug记录信息包括________________。①被测软件名称②被测软件版本③测试人④错误等级⑤开发人⑥详细步骤
某汽车维修公司有部门、员工和顾客等实体,各实体对应的关系模式如下:部门(部门代码,部门名称,电话)员工(员工代码,姓名,部门代码)顾客(顾客号,姓名,年龄,性别)维修(顾客号,故障情况,维修日期,员工代码)假设每个部门允许有多部电话,则电话属性为
随机试题
下列说法正确的是
水蛭的功效是()。
世界上第一台计算机使用的电子器件是()。
(操作人员:张主管;账套:101账套;操作日期:2014年1月1日)设置账套的凭证类型,其中银付凭证类型设置如下:编码:银付名称:银行付款凭证格式:付款凭证贷方必有科目:1002
下列叙述中,有关()的说法是不正确的。
请简述中国法律起源的特点。
Asiftheyneededanymoreexcuse,newresearchsuggestsmenneedtheirsleepifthey’retolivealonglife.Women,ontheothe
多数监管机构、财政部门和税务当局都未处理过与家族办公室相关的事宜。(whenitcomesto…)
Allwordscontaina
洛阳,因位于洛河之阳而得名,是我国的历史文化名城和七大古都之一。洛阳是中华文明的发祥地之一,在相当长的历史时期内曾经是我国重要的政治、经济、文化中心,亦是重要的交通枢纽。各种历史文化遗迹遍布洛阳,如中国三大石刻艺术宝库之一的龙门石窟(LongmenGro
最新回复
(
0
)