有如下程序: #include using namespace std; class Goods{//货物类 double unit_price;//单价 int quantity;//数量 public:

admin2020-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
0

相关试题推荐
最新回复(0)