#include <iostream>
using namespace std;
class Wallet
{
public:
void input();
void display();
friend Wallet operator+(Wallet& w1, Wallet& w2);
friend Wallet operator-(Wallet& w1, Wallet& w2);
friend Wallet operator*(Wallet& w1, Wallet& w2);
friend Wallet operator/(Wallet& w1, Wallet& w2);
private:
float rmb;
float jpy;
float usd;
};
void Wallet::input()
{
cin >> rmb >> jpy >> usd;
}
void Wallet::display()
{
cout <<"("<< rmb <<","<< jpy <<","<< usd<<")"<<endl;
}
Wallet operator+(Wallet &w1, Wallet &w2)
{return Wallet(w1.rmb + w2.rmb,w1.jpy + w2.jpy,w1.usd + w2.usd);}
Wallet operator*(Wallet w1, Wallet w2)
{ return Wallet(w1.rmb * w2.rmb, w1.jpy * w2.jpy, w1.usd * w2.usd);}
Wallet operator/(Wallet& w1, Wallet& w2)
{ return Wallet(w1.rmb / w2.rmb, w1.jpy / w2.jpy, w1.usd / w2.usd);}
Wallet operator-(Wallet& w1, Wallet& w2)
{ return Wallet(w1.rmb - w2.rmb, w1.jpy - w2.jpy, w1.usd - w2.usd);}
int main()
{
Wallet w1, w2, w3;
w1.input();
w2.input();
w3 = w1 + w2;
cout << "w1+w2=";
w3.display();
w3 = w1 - w2;
cout << "w1-w2=";
w3.display();
w3 = w1 * w2;
cout << "w1*w2=";
w3.display();
w3 = w1 / w2;
cout << "w1/w2=";
w3.display();
return 0;
}
缺个构造函数Wallet(float,float,float);
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
1
class Wallet
{
public:
Wallet() {};//看你下面的代码,需要一个默认无参构造函数
Wallet(float rmb, float jpy, float usd);//带参构造函数
void input();
void display();
friend Wallet operator+(Wallet& w1, Wallet& w2);
friend Wallet operator-(Wallet& w1, Wallet& w2);
friend Wallet operator*(Wallet& w1, Wallet& w2);
friend Wallet operator/(Wallet& w1, Wallet& w2);
private:
float rmb;
float jpy;
float usd;
};
//带参构造函数实现
Wallet::Wallet(float rmb, float jpy, float usd) {
this->rmb = rmb;
this->jpy = jpy;
this->usd = usd;
}
//你的rmb等三个是私有参数,需要用引用的方式访问,不然你能读取到私有参数的。
Wallet operator*(Wallet &w1, Wallet &w2)
{ return Wallet(w1.rmb * w2.rmb, w1.jpy * w2.jpy, w1.usd * w2.usd);}