作业 - 运算符 - 超载货币类别 - 卡住/丢失
我需要创建运算符重载函数方面的帮助。我尝试了 2 但我被困住了。 (谢谢大家上次的帮助!我能够完全完成:])。
问题 1: 运算符 +(const 货币 &rhs) 将添加 2 美元金额,但不会添加 2 美分金额,尽管它会保留其中之一的美分金额。因此,40.20 + 40.20 = 80.20(40 美元和 20 美分分别以“int”形式输入,出于可读性显示的目的,如上所示写入......抱歉造成混淆!) // 删除减法 (如果删除重载运算符,程序将正确加/减)。
问题2: 之前我有 int Dollars 和 int Cents,现在我只有“Amount”。我猜我需要传递这两个,并将它们加在一起作为一个成本[货币/金额],并将它们作为“金额”返回并使用它?*我不太确定,这是我陷入困境的另一个原因: (。
如果需要,我可以发布之前作业中的原始公共成员。 我暂时保留了先前分配的公共成员的所有旧功能。
class Currency
{
private:
int Dollars;
int Cents;
//string Amount;
民众:
// 构造函数
货币(int 美元 = 0, int 美分 = 0);
friend 货币运算符+(Currency,Currency const &);
// 添加
// Get
int GetDollars();// Need to be removed
int GetCents();// Need to be removed
};
货币::货币(int 美元, int 美分)
{
this->美元=美元;
this-> 美分 = 美分;
if(this->Cents >= 100)
{
this->Dollars += 1;
this->Cents -= 100;
}
}
货币运算符+(货币左、货币常量和右)
{
左.美元 += 右.美元;
左.分 += 右.分;
while (左.分>= 100)
{
左.分 -= 100;
左.美元 += 1;
}
向左返回;
}
int 货币::GetDollars()
{
退回美元;
}
int 货币::GetCents()
{
返回美分;
}
int main()
{
int currDollars;
int currCents;
//char answer;
cout << "Please enter a dollar amount" << endl;
cin >> currDollars;
cout << "Please enter a cents amount:" << endl;
cin >> currCents;
// Creating and initalizing objects instances of Currency class
Currency payroll(currDollars, currCents);
Currency payroll2(currDollars, currCents);
Currency payroll3;
// Testing overloaded opertator+
payroll3 = payroll + payroll2;
// Displaying test results
cout << "Payroll3 Amount:$ " << payroll3.GetDollars() << "."
<< payroll3.GetCents() << endl << endl;
return 0;
}
</pre></code>
I need help with creating Operator-Overloaded functions please. I tried 2 but I am stuck.
(Thank you all for your help last time! I was able to completely finish :] ).
Problem 1:
The operator+(const Currency &rhs) will add the 2 dollar amounts, but not the 2 cent amounts, though it keeps the cents from one of them. So, 40.20 + 40.20 = 80.20 (40 Dollars and the 20 cents are entered in separately being "int", wrote it as above for readability display purposes...sorry for the confusion!)
// Removed subtraction
(Program adds/subtracts correctly if overload operators are removed).
Problem 2:
Before I had int Dollars and int Cents, now I just have "Amount". I'm guessing I need to pass in those two, and add them together as one cost [Currency/Amount] and return them as "Amount" and use that?* I'm not too sure, another reason I'm stuck :(.
I can post original public members from previous assignment if needed.
I've left in all the old functions of the previous assignment's public members for now.
class Currency
{
private:
int Dollars;
int Cents;
//string Amount;
public:
// Constructor
Currency(int Dollars = 0, int Cents = 0);
friend Currency operator+(Currency, Currency const &);
// Addition
// Get
int GetDollars();// Need to be removed
int GetCents();// Need to be removed
};
Currency::Currency(int Dollars, int Cents)
{
this->Dollars = Dollars;
this->Cents = Cents;
if(this->Cents >= 100)
{
this->Dollars += 1;
this->Cents -= 100;
}
}
Currency operator+(Currency left, Currency const &right)
{
left.Dollars += right.Dollars;
left.Cents += right.Cents;
while (left.Cents >= 100)
{
left.Cents -= 100;
left.Dollars += 1;
}
return left;
}
int Currency::GetDollars()
{
return Dollars;
}
int Currency::GetCents()
{
return Cents;
}
int main()
{
int currDollars;
int currCents;
//char answer;
cout << "Please enter a dollar amount" << endl;
cin >> currDollars;
cout << "Please enter a cents amount:" << endl;
cin >> currCents;
// Creating and initalizing objects instances of Currency class
Currency payroll(currDollars, currCents);
Currency payroll2(currDollars, currCents);
Currency payroll3;
// Testing overloaded opertator+
payroll3 = payroll + payroll2;
// Displaying test results
cout << "Payroll3 Amount:$ " << payroll3.GetDollars() << "."
<< payroll3.GetCents() << endl << endl;
return 0;
}
</pre></code>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,现在我们已经没有足够的代码来真正查看了,有一些建议。首先,我想我只有一个加法运算符:
将右侧的美元和美分相加,然后返回结果。如果允许的话,使用全局运算符重载可能会更好(大概这是家庭作业,所以你可能不会......):
请注意,这使用了一个小“技巧”——它有左操作数按值传递,因此我们收到的“左”是作为左操作数传递的值的副本。然后我们修改并返回该对象。
我也只有一个构造函数,使用金额的默认参数:
这样,如果您不指定金额,您将获得 0.00 美元,但您可以指定美元或美元和美分,而无需处理三个重复的代码三种可能性。
通过组合这两者,您仍然可以执行添加
1
之类的操作,但处理方式略有不同 - 而不是直接使用采用单个 int 的operator+
,它采用 int 并使用 ctor 将其转换为Currency
对象,然后将该Currency
对象添加到另一个对象中。您的代码变得更短,并且(特别是)重复性更少。您无需尝试测试/验证三个不同的加法运算符,只需在一处处理一段代码。我想在真实版本中添加一件事,但在此处的精简版本中并不明显。我将上面的
while
循环分离成一个名为normalize
的单独(私有)函数或类似的函数,然后从operator+< 中使用它/code> 和
operator-
(如果您添加operator*
和/或operator/
,也可能是它们)。我还会消除
GetCents
和GetDollars
成员,而是添加一个重载的operator<<
来直接处理货币对象:这样,您可以替换此代码块:
用更短、更易读的内容:
编辑:鉴于它仅用于金钱,您可以让货币对象打印出
$
本身。如果您想更详细,您可以让它从区域设置中检索正确的货币指示符和小数分隔符。Okay, now that we have little enough code to really look at, a few suggestions. First, I think I'd have only one addition operator:
Have that add both the dollars and cents of the right side, and return the result. It might be even better to use a global operator overload, if you're allowed to (presumably this is homework, so you may not be...):
Note that this uses a little "trick" -- it has the left operand passed by value, so the "left" we receive is a copy of the value that was passed as the left operand. We than modify and return that object.
I'd also have only one constructor, using default parameters for the amount:
This way if you don't specify an amount, you get $0.00, but you can specify dollars, or dollars and cents, without three duplicates of the code to handle the three possibilities.
By combining the two of these, you can still do things like adding
1
, but it's handled a little bit differently -- instead of directly using anoperator+
that takes a single int, it take the int and converts it to aCurrency
object using the ctor, then adds thatCurrency
object to the other. Your code gets a lot shorter, and (particularly) a lot less repetitive. Instead of trying to test/verify three different addition operators, you have only one piece of code in one place to deal with.There is one thing I'd add to the real version that's not apparent in the stripped down version here. I'd separate out the
while
loop that's above into a separate (private) function namednormalize
or something like that, which would then be used from bothoperator+
andoperator-
(and if you add anoperator*
and/oroperator/
, probably them as well).I'd also eliminate the
GetCents
andGetDollars
members, instead adding an overloadedoperator<<
to handle a Currency object directly:With this, you can replace this block of code:
With something a bit shorter and more readable:
Edit: Given that it's intended to be used only for money, you could have the Currency object print out the
$
itself. If you wanted to get more elaborate, you could have it retrieve the correct currency designator and decimal separator from the locale.