首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
有如下程序: #include using namespace std; class Goods{//货物类 double unit_price;//单价 int quantity;//数量 public:
有如下程序: #include using namespace std; class Goods{//货物类 double unit_price;//单价 int quantity;//数量 public:
admin
2020-10-21
81
问题
有如下程序:
#include
using namespace std;
class Goods{//货物类
double unit_price;//单价
int quantity;//数量
public:
Goods(double u=0.0,int q=0):unit_price(u),quantity(q){}
double getPrice()const{return unit_price;}//返回单价
int getQuantity()const{return quantity;}//返回数量
//增加数量
Goods operator+(int q)const{return Goods(unit_price,quantity+q);}
};
//增加数量
Goods operator+(________){return g+q;}
//输出货物信息
ostream&operator<<(ostream& os,Goods g){
cout<<"单价:"<
return os;
};
int main(){
Goods g(35.6,100);
cout<
return 0;
}
若运行后的输出结果是:
单价:35.6,数量:100
单价:35.6,数量:133
单价:35.6,数量:125
则程序中下划线处缺失部分应该是( )。
选项
A、Goods q,Goods g
B、Goods g,Goods q
C、int q,Goods g
D、Goods g,int q
答案
C
解析
程序中,语句Goods g(35.6,100);定义了Goods类的对象g,通过构造函数可知单价unit_price赋值为35.6,数量quantity赋值为100,所以cout<
单价:35.6,数量:100
由于重载的输出运算符<<返回类型为ostream引用,所以继续输出g+33和25+g,对于程序中重载的加法运算符operator+(int q)const可知,将对象g与整数33相加时,重新构造一个新的对象,使用旧对象的单价unit_price初始化新对象的unit_price,使用旧对象的数量quantity与33相加初始化新对象的quantity,所以g+33输出结果为:
单价:35.6,数量:133
而整数25作为第一操作数执行25+g时,使用非成员重载运算符+,间接的调用g+25,所以题意中缺失部分应该将整数作为第一操作数,将类对象作为第二操作数,选项A、B、D错误,只有选项C正确,程序输出:单价:35.6,数量:125。本题答案为C。
转载请注明原文地址:https://www.kaotiyun.com/show/Bfyp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
下列叙述中正确的是()。
耦合性和内聚性是对模块独立性度量的两个标准。下列叙述中正确的是()。
有如下程序:#includeusingnamespacestd;voidfun(int&x,inty){intt=x;x=y;y=t;)intmain(){inta[2]={1,2}
有如下类声明:classTestClass{intn;public:TestClass(inti=0):n(i){}voidsetValue(intn0);
有如下程序#includeusingnamespacestd;classTest{public:Test(){)Test(constTest&t){cout
数据库应用系统中的核心问题是()。
下列符号中能够作为C++标识符的是()。
下列模板声明中,有语法错误的是()。
使用VC6打开考生文件夹下的源程序文件modi1.cpp,该程序运行时有错误,请改正错误,使得程序输出:Hellotest注意:不要改动main函数,不能增行或删行,也不能更改程序的结构,错误的语句在//********er
某二叉树共有7个结点,其中叶子结点只有1个,则该二叉树的深度为(假设根结点在第1层)()。
随机试题
中度以上龋,银汞合金充填时需垫底,是因为银汞合金有
A、二丙酸倍氯米松B、可待因C、沙丁胺醇D、氨茶碱E、色甘酸钠具有强大的抗炎及免疫抑制作用的,用于哮喘发作的药物是()。
妇科门诊带教林护士向护生提问有关"双合诊"检查法的概念,正确的是
期货从业人员必须遵守()。
专利权可以作为经济法律关系的内容,但不可以作为经济法律关系的客体。()
完成某项工程,甲单独工作需要18小时,乙需要24小时,丙需要30小时。现按甲、乙、丙的顺序轮班工作,每人工作一小时换班。当工程完工时。乙总共干了()。
第三代移动通信技术3G是指支持高速数据传输的蜂窝移动通信技术。目前3G主要存在4种国际标准,其中______为中国自主研发的3G标准。
TheInternationalLaborOrganizationhasstudiedreportsonworkers’privacyinnineteenindustrialnations.Thestudyshowstha
DeterminingtheAgeofthePlanetsandtheUniverseP1:Asthesolarnebula,alargerotatingcloudofinterstellardustandgas
Traditionalplantbreedinginvolvescrossedvarietiesofthe【M1】______samespeciesinwaystheycouldcrossnaturally.Forexa
最新回复
(
0
)