首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
阅读以下说明和C++代码(代码13-1),将应填入(n)处的字句写在对应栏内。 【说明】 软件设计师东方飞龙利用UML设计了一个迷你小型复数类,其类图如图13-11所示。 【代码13-l】 /*________________________
阅读以下说明和C++代码(代码13-1),将应填入(n)处的字句写在对应栏内。 【说明】 软件设计师东方飞龙利用UML设计了一个迷你小型复数类,其类图如图13-11所示。 【代码13-l】 /*________________________
admin
2010-01-15
62
问题
阅读以下说明和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://www.kaotiyun.com/show/TcDZ777K
本试题收录于:
软件设计师下午应用技术考试题库软考中级分类
0
软件设计师下午应用技术考试
软考中级
相关试题推荐
在各种不同的软件需求中,(36)描述了用户使用产品必须要完成的任务,可以用UML建模语言的(37)表示。(36)
数据库系统通常采用三级模式结构:外模式、模式和内模式。这三级模式分别对应数据库的__________。
下面是路由表的4个表项,与地址220.112.179.92匹配的表项是________。
A类网络是很大的网络,每个A类网络中可以有(26)个网络地址。实际使用中必须把A类网络划分为子网,如果指定的子网掩码为255.255.192.0,则该网络被划分为(27)个子网。(26)
假设磁盘块与缓冲区大小相同,每个盘块读入缓冲区的时间为10μs,由缓冲区送至用户区的时间是5μs,系统对每个磁盘块数据的处理时间为2μs。若用户需要将大小为10个磁盘块的Docl文件逐块从磁盘读入缓冲区,并送至用户区进行处理,那么采用单缓冲区需要花费的时间
为检验某Web系统并发用户数是否满足性能要求,应进行()。
CMM模型将软件过程的成熟度分为5个等级。在(21)使用定量分析来不断地改进和管理软件过程。
下述说法错误的是(46)。
某算术表达式用二叉树表示如下,该算术表达式的中缀式为________________,其后缀式为________________。
下图是①设计模式的类图,该设计模式的目的是②,图中,Abstraction和RefinedAbstraction之间是③关系,Abstraction和Implementor之间是④关系。④处应填入?
随机试题
患者,男,36岁。数日大便不解,胸胁苦满,呕吐不止,郁郁微烦,寒热往来,心下痞硬,舌苔黄,脉弦而有力。治疗应首选()
急性炎症性脱髓鞘性多发性神经病的首发症状通常为
最上层支架距其他设备装置的净距不得小于(),当无法满足时应设置防护板。
甲企业为增值税一般纳税人,主要生产一字牌电视机,本年度实现会计利润800万元,部分财务资料如下:(1)销售一字牌电视机取得不含增值税销售收入6000万元,同时收取送货费用58.5万元,取得国债利息收入50万元,企业债券利息收入12万元;(2)发生财务费
支票的提示付款期限为自出票日起()日。
资产负债表中“预付账款”项目的金额应包括( )。
人教版高中语文必修2《在马克思墓前的讲话》课后给出了马克思中学毕业时写的文章,设计的问题是:“联系课文进行比较阅读,想一想,马克思是否实现了当年的职业理想?你从中受到哪些启发?与同学交流一下。”对该练习的设计意图,分析不正确的是()。
某甲故意在某乙所有的马身边按车喇叭,致使拴在木桩上的马受惊挣脱绳子,冲出去撞伤了行人某丙。则()。
我们党的思想路线是一切从实际出发,理论联系实际,实事求是,在实践中检验真理和发展真理,其实质内容是解放思想,实事求是,与时俱进。党的思想路线体现了()
某宾馆中有单人间和双人间两种客房,按照规定,每位入住该宾馆的客人都要进行身份登记。宾馆数据库中有客房信息表(房间号,……)和客人信息表(身份证号,姓名,来源,……):为了反映客人入住客房的情况,客房信息表与客人信息表之间的联系应设计为
最新回复
(
0
)