首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
有如下程序: #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
79
问题
有如下程序:
#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://kaotiyun.com/show/Bfyp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
有如下程序;#inc1ude<iostream>usingnamespacestd;classB{public:virtualvoidshow(){cout
有如下程序:#inc1ude<iostream>usingnamespacestd;classA{public:A(){cout<<’A’;}~A(){cout<<’C’;}};
已知主函数中通过如下语句序列实现对函数模板swap的调用:inta[10],b[10];swap(a,b,10);下列对函数模板swap的声明中,会导致上述语句序列发生编译错误的是()。
有两个关系R和S如下:由关系R通过运算得到关系S,则所使用的运算为()。
在长度为64的有序线性表中进行顺序查找,最坏情况下需要比较的次数为()。
在下面的类定义中,横线处应填入的内容是()。classFred{public:voidprint(){cout
结构化程序设计中,下面对goto语句使用描述正确的是()。
Sample是一个类,执行下面语句后,调用Sample类的构造函数的次数是()。Samplea[2],*P=newSample;
if语句的语法格式可描述为:格式1:if()或格式2:if()else关于上面的语法格式,下列表述中错误的是()。
执行下列语句段后,输出字符“*”的个数是()。for(inti=50;i>l;i-=2)cout
随机试题
杜甫《兵车行》:边庭流血成海水,_________。
少尿期一般持续的时间为()
急性脓胸最常见的致病菌是
有关蛋白质α螺旋结构,错误的是()
口服液体铁剂的正确方法是
(2007年)忽略质量的细杆OC=l,其端部固结均质圆盘。杆上点C为圆盘圆心。盘质量为m,半径为r。系统以角速度ω绕轴O转动(见图4—60)。系统的动能是()。
回购业务的结算中,第二次结算称为()。
在不兑现的信用货币制度下,货币发行权主要集中在()。
Oneofthemisconceptionsaboutelitesportisthatgreatathleteshavenothingincommonwithnormalhumanbeings.【C1】______th
StrategiesforPublicSpeakingWhat’sgreataboutpublicspeakingisthatit’salearnableskill.Assuch,wecanusethef
最新回复
(
0
)