首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
有如下程序: #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
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()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全国计算机二级
相关试题推荐
下面关于虚函数的描述中正确的是()。
派生类的成员函数不能访问基类的
下列关于运算符重载的描述中,错误的是
有如下程序: #include<iostream> void fun(int&x,int y){int t=x;x=y;y=t;} int main() { int a[2]={23,42}; fun(a
在一个派生类对象结束其生命周期时
下列有关运算符重载的叙述中,正确的是
下列关于运算符重载的描述中,正确的是
下列有关继承和派生的叙述中,正确的是
使用VC6打开考生文件夹下的源程序文件modi3.cpp。其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。完成以下功能:每卖出一个瓜,则计算瓜的重量,还要计算所有卖出瓜的总重量以及总个数,同时允许退货,请按照以下的操作,把类补充完整
随机试题
冲突规范
急性白血病外周血白细胞计数可
作业指导书也称作标准操作规程(SOP),是质量管理体系的重要组成部分,对其阐述不正确的是哪项
下列病人可给予鼻饲饮食的是
发现艾滋病时,城镇应当发出报告的时间是发现艾滋病时,农村应当发出报告的时间是
下列属于我国法律效力范围的有()。
人类社会最早诞生的教育学是以()作为自己的研究对象的。
Factorsleadingtothecrisesincludedpoorregulationmismanagementanddeceptionintheindustry,andcompetitionfromothert
______stormswreakedhavoconthearea,leavingthreepeopledeadanddozensinjured.
Mostpeopleconsiderbacteriadangerous.Afterall,thesemicroorganismscauseahostofserioushumandiseases,includingtuber
最新回复
(
0
)