首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
阅读以下程序说明和C++程序,将程序段中(1)~(5)空缺处的语句填写完整。 【说明】 以下【C++程序】实现一个简单的小型复数类MiniComplex,该复数类能进行输入、输出、复数的加法、减法、乘法和除法运算,还可以进行复数的相等比较。
阅读以下程序说明和C++程序,将程序段中(1)~(5)空缺处的语句填写完整。 【说明】 以下【C++程序】实现一个简单的小型复数类MiniComplex,该复数类能进行输入、输出、复数的加法、减法、乘法和除法运算,还可以进行复数的相等比较。
admin
2009-02-15
75
问题
阅读以下程序说明和C++程序,将程序段中(1)~(5)空缺处的语句填写完整。
【说明】
以下【C++程序】实现一个简单的小型复数类MiniComplex,该复数类能进行输入、输出、复数的加法、减法、乘法和除法运算,还可以进行复数的相等比较。
【C++程序】
#ifndef H_MiniComplex
#define H_MiniComplex
#include <iostream>
using namespace std;
class MiniComplex{
public: //重载流插入和提取运算符
(1) ostream&operator<<(ostream &osObject,const MiniComplex&complex){
osObject<<"("<<complex.realPart<<"+"<<complex.imagPart<<"i"<<")";
return osObject;
}
(2) istream&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&otherComplex)const; //重载运算符*
MiniComplex operator/(const MiniComplex&otherComplex)const; //重载运算符/
bool operator==(const MiniComplex&otherComplex)const; //重载运算符==
private :
double (3);
double imagPart;
};
#end if
#include "MiniComplex.h"
bool MiniComplex::operator==(const MiniComplex&otherComplex)const{
return(realPart==otherComplex.realPart&&imagPart==ortherComplex.imagPart);
}
MiniComplex::MiniComplex(double real,double imag){
realPart== real; imagPart==imagPart;
}
MiniComplex MiniComplex::operator+(const MiniComplex&otherComplex)const{
MiniComplex temp;
temp.realPart = realPart+ortherComplex. realPart;
temp.imagPart = imagPart +ortherComplex. imagPart;
return temp;
}
(4)
{ MiniComplex temp;
temp.realPart= realPart-ortherComplex. realPart;
temp.imagPart = imagPart-ortherComplex. imagPart;
return temp;
}
MiniComplex MiniComplex::operator*(const MiniComplex&otherComplex)const{
MiniComplex temp;
temp.realPart = (realPart*ortherComplex. realPart)-(imagPart *ortherComplex.imagPart);
temp.imagPart = (realPart*ortherComplex. imagPart)+(imagPart *ortherComplex.realPart);
return temp;
}
MiniComplex MiniComplex::operator/(const MiniComplex&otherComplex)const{
MiniComplex temp;
float tt;
tt=1/(ortherComplex.realPart*ortherComplex.realPart+ortherComplex.imagPart *ortherComplex. imagPart);
temp.realPart=((realPart*ortherComplex, realPart)+(imagPart *ortherComplex. imagPart))*tt;
temp.imagPart =((imagPart *ortherComplex. realPart)-(realPart*ortherComplex. imagPart))*tt;
return temp;
}
#include <iostream>
#include <MiniComplex.h>
using namespace std;
int main(){
MiniComplex numl(23, 34),num2(56, 35);
cout<<"Initial Value of num1="<<num1<<"\n Initial 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; //使用重载的除号运算符
(5);
}
选项
答案
(1)friend (2)friend (3)RealPart (4)MiniComplex MiniComplex::operator-(const MiniComplex&otherComplex)const (5)return 0
解析
这是一道要求读者掌握操作符重载的程序设计题。本题的解答思路如下。
运算符重载是对已有的运算符赋予多重含义,使同一个运算符作用别:不同类型的数据时导致不同的行为。运算符重载的实质就是函数重载,在实现过程中,首先把指定的运算表达式转化为对运算符函数的调用,运算对象转化为运算符函数的实参,然后根据实参的类型来确定需要调用的函数,这个过程是在编译过程中完成的。
运算符的重载形式有重载为类的成员函数和重载为类的友元函数两种。其中,运算符重载为类的成员函数的一般语法形式如下。
函数类型 operator 运算符(形参表)
{ 函数体; }
运算符重载为类的友元函数要在类中声明友元函数的原型:
friend 函数类型 operator 运算符(形参表);
然后,在类外实现函数。也可在类体中写成内联形式:
friend 函数类型 operator 运算符(形参表)
{ 函数体; }
函数类型重载为类的成员函数时,函数的参数个数比原来的操作数个数要少一个(后置“++”、“--”除外),因为有一个操作数是对象本身,由this指针指出。后置“++”、“--”运算符重载时,为了与相应的前置运算符区别,需要一个int参数。
当重载为类的友元函数时,参数个数与原操作数个数相同,运算所需要的操作数都需要通过函数的形参表来传递,在形参表中形参从左到右的顺序就是运算符操作数的顺序。运算符重载的规则如下。
1)只能重载C++语言中已经定义过的运算符。
2)重载之后运算符的优先级和结合性都不会改变。
3)不能改变原运算符的操作对象个数,同时至少要有一个操作对象是自定义类型。
对于本试题,在该小型复数类MiniComplex中,函数成员主要是重载进行复数运算的各种操作符。由于各种运算符功能的限制,将输入和输出运算符重载为友元函数,使得复数的输入和输出更加方便。因此(1)、(2)空缺处所填写的内容都足“friend"。
使用成员函数形式重载“+、-、*、/”运算符来进行复数的加法、减法、乘法和除法运算。重载逻辑运算符“==”进行复数的相等比较。数据成员包含:实部(RealPart)和虚部(ImagPart)。因此(3)空缺处所填写的内容是“RealPart”。
(4)空缺处是重载减号的函数,所以应填入“MiniComplex MiniComplex::operator-(const MiniComplex &otherComplex)const”。
由于主函数main()没有返回值,因此(5)空缺处所填写的内容是“return 0”。
转载请注明原文地址:https://kaotiyun.com/show/ybjZ777K
本试题收录于:
程序员下午应用技术考试题库软考初级分类
0
程序员下午应用技术考试
软考初级
相关试题推荐
____________是微机最基本最重要的部件之一,其类型和档次决定着整个微机系统的类型和档次,其性能影响着整个微机系统的性能。CPU模块就插在其上面。
以下关于Excel单元格操作的叙述,(52)是错误的。
黑屏是微机显示器常见的故障现象。发生黑屏时需要检查的项目不包括(27)________________。
下列关于索引的叙述中,正确的是________________。
在Word2010中,()快捷键可以选定当前文档中的全部内容。
Win7控制面板中,可通过()查看系统的一些关键信息,并可进行调整视觉效果、调整索引选项、调整电源设置及打开磁盘清理等操作。
在浏览网页时,当鼠标指针移至某些文字或某些图片时,会出现手形状,通常是由于网页在这个地方做了(17)。
在Excel中,若单元格C5=1000、D5=50、C6=6000、D6=40,在单元格E5中输入公式“=C5*$D$5”,再将此公式复制到F6单元格中,则F6单元格的值为(54)。
阅读下列说明,根据网页显示的效果图,回答问题1至问题3。【说明】某商务网站用ASP实现了一个在线手机性能评价投票网页,主页文件名为“index.asp”,用IE打开该网页后的效果如图4-9所示。程序中使用的Access数据表结构如表4-1所示
阅读下列说明和HTML文本,分析其中嵌入的JavaScrlpt脚本,将应填入<u>(n)</u>处的语句写在对应栏内。[说明]本题实现用鼠标拖拽图片在Web页内移动的功能。将鼠标放在图片上,按下左键,移动鼠标便可带动图片一起移动。[
随机试题
关于生物利用度和生物等效性试验设计的错误表述是
患者男性,25岁,因受凉后突然畏寒、高热伴右胸部疼痛1天入院。胸部透视,见右中肺有大片浅淡的阴影。住院后经青霉素肌注,3天后体温接近正常,病人尚有轻度咳嗽,咳痰,稍感憋气。目前对该病人的主要护理措施是
甲欲购买乙的汽车。经协商,甲同意3天后签订正式的买卖合同,并先交1000元给乙,乙出具的收条上写明为“收到甲订金1000元”。3天后,甲了解到乙故意隐瞒了该车证照不齐的情况,故拒绝签订合同。下列哪一个说法是正确的?()
公民、法人或者其他组织对行政机关所给予的行政处罚,享有陈述权、申辩权;对行政机关作出的行政处罚,有依法申诉、检举、申请行政复议或提起行政诉讼等救济权。()
支票上的金额为绝对记载事项,因此,支票出票时如果未记载确定的金额,该支票无效。()
从决策的基本属性来看,决策是()。
某市举办文化创意展,但是很多群众反映没创意,为此领导让你搜集群众关于创意展的意见,请问你会如何开展?
AimlessnesshashardlybeentypicalofthepostwarJapanwhoseproductivityandsocialharmonyaretheenvyoftheUnitedStates
以下选项中,不合法的C语言用户标识符是
WhenpeoplesaythatCambridgeisauniversitytown,theydonotmeanthatitisatownwithauniversityinit.Auniversityto
最新回复
(
0
)