首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
有如下程序: #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-12-21
26
问题
有如下程序:
#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()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.getPriee()<<‘,’<<"数量:”<<g.getQuantity()<<endl;
return OS;
};
int main(){
Goods g(35.6,100);
cout<<g<<g+33<<25+g;
return 0;
}
若运行后的输出结果是:
单价:35.6,数量:100
单价:35.6,数量:133
单价:35.6,数量:1:25
则程序中下划线处缺失部分应该是( )。
选项
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,i 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初始化新对象的unil_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/ppyp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
下列关于运算符重载的描述中,正确的是()。
有如下函数模板: template<typename T,typename U> T cast(Uu){ return u;} 其功能是将U类型数据转换为T类型数据。已知i为int型变量,下列对模板函数cast的调用中正确的是
有如下程序: #include<iostream> using namespace std; class AA { int n; public: AA(int k);n(k){}
有如下语句序列:intx=100,&r=x;cout<<x<<’-’<<r<<end1:己知其中变量x的地址为0012FF7C,则执行该语句序列的输出结果为()。A)100-100B)100-0012FF7CC
有如下程序:#includeusingnamespacestd;classToy{public:Toy(char*_n){strcpy(name,_n);count++;}~Toy()}count--;}char*GetName(){r
有如下语句序列; int k=0; do{k+=5;couk<<’$’;) while(k<19); while(k-->0)cout<<’*’; 执行上面的语句序列输出字符$和。的个数分别是
有如下类定义:classSample{public;Sample(intx):ref(x){}//①private:Sample():ref(0){)//②staticintval=5;
下列关于对象初始化的叙述中,正确的是
使用VC6打开考生文件夹下的源程序文件modil.cpp,该程序运行时有错误,请改正其中的错误,使程序正确运行。并且使程序输出的结果为:OK注意:错误的语句在//*****error******的下面,修改该语句即可。#include
随机试题
如果指示剂的变色范围全部处于滴定突跃之内,则滴定误差最小。
黄疸的病理因素有湿邪、热邪、寒邪、疫毒、气滞、瘀血六种。但其中以哪个为主
两样本均数比较的检验,差别有显著性,时,P越小,说明
患者女,13岁,正在进行正畸治疗,医生建议她使用氟化凝胶防龋医生叮嘱患者下次复诊时间
大肠液亏证的主症是
王某,女,1990年出生,于2012年2月1日入职某公司,从事后勤工作,双方口头约定每月工资为人民币3000元,试用期1个月。2012年6月30日,王某因无法胜任经常性的夜间高处作业而提出离职,经公司同意,双方办理了工资结算手续,并于同日解除了劳动关系。同
案例F市某C钢铁有限公司年产生铁500×104t,各类型钢等400×104t。公司下设炼铁厂、炼钢厂、烧结球团厂等分厂及安全部、设备管理部、办公室等辅助部门。现有员工6600名,其中专职安全管理人员80名。主要生产设备设施包括2×500m3、3×880m
对需要结汁本月发生额的账户,结计“过次页”的本页合计数应当为自本页第二行起至本页末止的发生额合计数。()
主办券商的代办业务包括()
用多水平法编制的弹性预算,主要特点是()。
最新回复
(
0
)