首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
阅读以下说明和C++代码(代码13-1),将应填入(n)处的字句写在对应栏内。 【说明】 软件设计师东方飞龙利用UML设计了一个迷你小型复数类,其类图如图13-11所示。 【代码13-l】 /*________________________
阅读以下说明和C++代码(代码13-1),将应填入(n)处的字句写在对应栏内。 【说明】 软件设计师东方飞龙利用UML设计了一个迷你小型复数类,其类图如图13-11所示。 【代码13-l】 /*________________________
admin
2010-01-15
40
问题
阅读以下说明和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
软件设计师下午应用技术考试
软考中级
相关试题推荐
下图中,类Product和ConcreteProduct的关系是(45),类ConcreteCreator和ConcreteProduct的关系是(46)。(46)
对高级语言源程序进行编译时,可发现源程序中的(21)错误。
______不属于易用性测试范畴。A.软件产品使用户能理解软件是否合适以及如何能将软件用于特定的任务和使用条件的能力B.软件产品使用户能操作和控制它的能力C.对软件中的缺陷或失效原因进行诊断,或识别待修改部分的能力D.软件产品吸引用户的能力
在编码阶段对系统执行的测试类型主要包括单元测试和集成测试,(40)属于单元测试的内容。
为说明某一问题,在学术论文中需要引用某些资料。以下叙述中,()是不正确的。
对于逻辑表达式(((a>0)&&(b>0))‖c<5),需要______个测试用例才能完成条件组合覆盖。
以下不属于软件编码规范评测内容的是______。
按照开发阶段划分,软件测试可以分为______。①单元测试②集成测试③系统测试④确认测试⑤用户测试⑥验收测试⑦第三方测试
某算术表达式用二叉树表示如下,该算术表达式的中缀式为________________,其后缀式为________________。
针对下列程序段,对于(A,B)的取值,以下(57)测试用例组合能够满足条件覆盖的要求。IF((A-10)=20AND(B+20)>10)THENC=0IP((A-30)<10AND(B-30)<0)THENB=30①A=5
随机试题
心源性呼吸困难的主要原因是
肝硬化病人使用利尿药,应特别注意维持_______和_______平衡。
下列内分泌因素与系统性红斑狼疮的发病有关的是
慢性再生障碍性贫血应首选()。
该项目土地使用权出让合同由()与甲公司签订。甲公司实施该项目的项目资本金不得少于()万元。
下列说法中正确的是()。
到期一次还本付息法一般适用于期限在()的贷款。
有甲、乙、丙三种浓度的过氧化氢,按甲与乙体积之比2:1混合,得到浓度为13%的过氧化氢;按甲与乙体积之比1:2混合,得到浓度为14%的过氧化氢。如果甲、乙、丙体积之比为1:1:3,混合成的过氧化氢浓度为10.2%,则同体积的甲、乙、丙三种浓度的过氧化氢混合
若串s="Program",则其子串的数目是【】。
WheneverIhearaweatherreportdeclaringit’sthehottestJune10onrecordorwhatever,Ican’ttakeittooseriously,becaus
最新回复
(
0
)