首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
有如下程序: #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
46
问题
有如下程序:
#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全国计算机二级
相关试题推荐
执行下列语句,变量sum的值是______。intsum=0;for(inti=1;i<=3;i++)for(intj=1;j<=i;j++)sum++;
以下程序的输出结果是______。#include<iostream.h>#include<string.h>voidmain(){char*p1=",hello",*p2="world!",str[
有如下程序:#includeusingnamespacestd;intmain(){cout.fill(‘*’);cout.width(6);cout.fill(‘#’)
下列程序的输出结果是()。#include<iostream>usingnamespacestd;template<typenameT>Tfun(Ta,Tb){return(a>=b)?a:b;}
请按下面注释的提示将类B的构造函数定义补充完整。classA{inta;public:A(intaa=0){a=aa;}};classB:publicA{
对于结构中定义的成员,其隐含访问权限为______。
下面关于构造函数和析构函数的描述,错误的是()。
下列对重载函数的描述中,错误的是( )。
使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,完成空出部分程序,使函数fun()实现以下功能:找出一个整数,它加上100后是一个完全平方数,再加上268又是一个完全平方数,请问该数是多少?程序分析:在10万以内
随机试题
沙土应落肥勤施。
在建筑施工过程中产生的干扰周围生活环境的声音属于()。
对综合控制系统试验说法正确的是()。
在基层上用于浇洒透层的材料包括()。
以下关于劳动法基本原则的说法,正确的有()
认知能力的提高主要是由于()。
数字减影血管造影
把剩余价值转化为资本,或者说剩余价值的资本化,就是资本积累。资本积累导致的最终结果是
已知f(x,y)=设D为由x=0、y=0及x+y=t所围成的区域,求F(t)=f(x,y)dxdy.
设有如下程序:PrivateSubForm_Click()num=InputBox(”请输入一个实数”):P=InStr(num,",")IfP>0ThenP
最新回复
(
0
)