热门

最新

红包

立Flag

投票

同城

我的

发布
hua_xiaolan
伴城柳涩
3 年前
truehua_xiaolan

//定义一个钱包类Wallet,有三个浮点数成员变量,rmb,jpy,usd分别代表人民币,日元和美元。重载运算符“-”,使之能用于浮点数(人民币)的减法运算。
//参加运算的两个运算量可以都是类对象,也可以其中有一个是浮点数,顺序任意。
//例如:w1 - w2,2 - w1 , w1 - 1.2均合法。
#include<iostream>
using namespace std;
class Wallet{
public:
Wallet(){rmb=0;jpy=0;usd=0;}
friend Wallet operator-(Wallet &c1,Wallet &c2);
friend Wallet operator-(Wallet &c1,float x);
friend Wallet operator-(float x,Wallet &c1);
void display();
void input();
private:
float rmb,jpy,usd;
};
void Wallet::display(){cout<<"("<<rmb<<","<<jpy<<","<<usd<<")"<<endl;}
void Wallet::input(){cin>>rmb>>jpy>>usd;}
Wallet operator-(Wallet &c1,Wallet &c2){
Wallet c;
c.rmb=c1.rmb-c2.rmb;
c.jpy=c1.jpy-c2.jpy;
c.usd=c1.usd-c2.usd;
return c;
}
Wallet operator-(Wallet &c1,float x){
Wallet c;
c.rmb=c1.rmb-x;
c.jpy=c1.jpy;
c.usd=c1.usd;
return c;
}
Wallet operator-(float x,Wallet &c1){
Wallet c;
c.rmb=x-c1.rmb;
c.jpy=c1.jpy;
c.usd=c1.usd;
return c;
}

CSDN App 扫码分享
分享
评论
2
打赏
  • 复制链接
  • 举报
下一条:
最近想整一个Androidstudio的表白代码,谁有好的句子推荐一下,这只是一个设计图纸。星期天晚上出完整代码。请各位多多指教。
立即登录