首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
有如下程序: #include<iostream> using namespaee std; class Goods{//货物类 double unit_price;//单价 int quantity;//数量
有如下程序: #include<iostream> using namespaee std; class Goods{//货物类 double unit_price;//单价 int quantity;//数量
admin
2020-06-29
32
问题
有如下程序:
#include<iostream>
using namespaee std;
class Goods{//货物类
double unit_price;//单价
int quantity;//数量
public:
Goods(double u=0.0,int q=0):unit_price(u),quantity(q){ }
double getPrice( )eonst{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<<"单价:"<<g.getPrice( )<<’,’<<"数量:"<<g.getQuantity( )<<end1;
return OS;
f;
int main( ) {
Goods g(35.6,100);
cout<<g<<g+33<<25+g;
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<<g使用重载的输出运算符<<输出结果为:
单价: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://kaotiyun.com/show/ii8p777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
构造函数的主要作用是【 】。
下列程序的招待结果是【 】。#include<iostream.h>floattemp;float&fn2(floatr){temp=r*r*3.14;returntemp;}voidmain(){
在一个函数的定义或声明前加上关键字【】时,该函数就声明为内联函数。
如果在表达式++x/y中,++是作为友元函数重载的,/是作为成员函数重载的,则该表达式还可为
设有以下定义和程序:#include<iostream.h>classA1{public:voidshow1(){cout<<"classA1"<<endl;}};classA2:publicA1{
如果要把返回值为void的函数A声明为类B的友元函数,则应在类B的定义中加入的语句是______。
语句ofstreamf("DATA.DAT",ios_base::applios_lios_base::binary);的功能是建立流对象f,并试图打开文件DATA.DAT并与之连接,而且()。
下列程序中划线处正确的语句是()。#include<iostream>usingnamespacestd;classBase{public:voidfun(){cout<<"Base:
下面关于构造函数和析构函数的描述,错误的是()。
使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能:(1)完成构造函数,设置数组元素的个数为0,请在注释//********1********之后添加语句。
随机试题
施行上腹部手术后出现顽固性呃逆,应高度警惕
A.稽留流产B.难免流产C.先兆流产D.不全流产E.习惯性流产
男,45岁,煤矿工人,被煤块砸伤腰背部后感腰痛,伴双下肢感觉运动障碍及大小便失禁24小时入院。体查:腰1椎体后突畸形,压痛,腹股沟以下平面感觉运动完全丧失。X线片示腰1椎体压缩1/2,向后成角畸形。最恰当的治疗方法是
枳术丸的功用是
某跨年度项目的合同总收10000万元,预计合同总成本8500万元,资产负债表日,以前会计年度累计已确认的收入为6000万元,该工程现已完成工程进度的80%,则当期应确认的合同收入为()万元。
下列不属于财务管理职能的有()。
下列经济业务所产生的现金流量中,属于“经营活动产生的现金流量”的是( )。
下列关于马略军事改革的叙述,不正确的是()。
《唐律疏议.断狱》“官司出入人罪”条:“诸官司入人罪者,若入全罪,以全罪论。从轻入重。以所剩论。刑名易者,从笞入杖,从徒入流,亦以所剩论。从笞杖入徒流,从徒流入死罪,亦以罪论。其出罪者各如之。即断罪失于入者,各减三等;失于出者,各减五等”。请运用中国法制史
关于分布式结构化P2P网络的描述中,正确的是()。
最新回复
(
0
)