//定义一个钱包类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;
}