作业-操作符-重载货币类-卡住/丢失
Homework - Operator-Overloading Currency Class - Stuck/Lost
我需要帮助创建操作符重载函数请。我试过了,但是卡住了。谢谢大家上次的帮助!我能够完全完成:])。
问题1:操作符+(const Currency &rhs)将添加2美元的金额,但不添加2美分的金额,尽管它保留了其中一个的美分。因此,40.20 + 40.20 = 80.20(40美元和20美分分别输入为"int",出于可读性的考虑,如上所述……很抱歉造成了混乱!)//删除减法(如果删除了重载操作符,程序可以正确地进行加减运算)。
问题2:之前我有美元和美分,现在我只有"金额"。我猜我需要传递这两个,并将它们加在一起作为一个成本[货币/金额],并返回它们作为"金额",并使用它?我不太确定,这是我被困住的另一个原因:
如果需要,我可以发布以前任务的原始公共成员。我现在保留了前一个赋值的公共成员的所有旧函数。
class Currency
{
private:
int Dollars;
int Cents;
//string Amount;
公众://构造函数货币(int dollar = 0, int Cents = 0);friend货币操作符+(货币,货币常量&);//添加
// Get
int GetDollars();// Need to be removed
int GetCents();// Need to be removed
};
Currency::Currency(int Dollars, int Cents){this-> $ = $;this->Cents = Cents;
if(this->Cents >= 100)
{
this->Dollars += 1;
this->Cents -= 100;
}
}
货币操作符+(货币左,货币常量&右){离开了。$ += right. $;离开了。Cents += right.Cents;而(左。美分>= 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;
}
& lt;/pre> & lt;/code>
好了,现在我们有足够少的代码来真正查看,下面是一些建议。首先,我认为我只有一个加法运算符:
Currency operator+(Currency const &c);
将右边的美元和美分相加,并返回结果。如果允许的话,使用全局操作符重载可能会更好(假设这是作业,所以您可能不会…):
Currency operator+(Currency left, Current const &right) {
left.Dollars += right.Dollars;
left.Cents += right.Cents;
while (left.Cents >= 100) {
left.Cents -= 100;
left.Dollars += 1;
}
return left;
}
注意,这里使用了一个小"技巧"——它的左操作数是按值传递的,所以我们接收到的"左"是作为左操作数传递的值的副本。然后修改并返回该对象。
我也只有一个构造函数,使用默认参数:
Currency(int dollars = 0, int cents = 0);
这样,如果你不指定金额,你得到$0.00,但你可以指定美元,或美元和美分,而不需要重复三遍代码来处理这三种可能性。
通过结合这两者,您仍然可以做诸如添加1
之类的事情,但它的处理方式略有不同—而不是直接使用接受单个int的operator+
,它将int转换为使用tor的Currency
对象,然后将Currency
对象添加到另一个对象。您的代码变得更短,而且(特别是)更少重复。而不是试图测试/验证三个不同的加法运算符,您只需要在一个地方处理一段代码。
有一件事我要添加到真实版本中,这在这里的剥离版本中并不明显。我会把上面的while
循环分离成一个单独的(私有)函数,名为normalize
或类似的东西,然后从operator+
和operator-
中使用(如果您添加operator*
和/或operator/
,可能也会使用它们)。
我还会消除GetCents
和GetDollars
成员,而是添加一个重载的operator<<
来直接处理货币对象:
std::ostream &operator<<(std::ostream &os, Currency const &c) {
return os << c.Dollars << "."
<< std::setfill('0') << std::setw(2) << std::setprecision(2)
<< c.Cents;
}
用这个,你可以替换这段代码:
cout << "Current Amount is:$ "
<< payroll.GetDollars()
<< ".";
if(payroll.GetCents() < 10)
{cout << "0";}
else
cout
<< payroll.GetCents()
<< endl;
cout << endl;
加上一些更短更易读的内容:
cout << "Current amount is: $" << payroll << endl;
编辑:考虑到它仅用于货币,您可以让Currency对象打印出$
本身。如果需要更详细地说明,可以让它从区域设置检索正确的货币指示符和小数分隔符。
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 为什么与常规GCC不同,即使有"学究性错误",MinGW-GCC也能容忍丢失的返回类型
- ISearchJob->Release() 卡住
- 为什么 BMP 图像上的 imwrite 会卡住/不返回?
- 为什么 std::next 在以空地图开头馈送时卡住
- 从'size_t'转换为"常量双倍",可能会丢失数据
- vcruntime.h 从 Windows 包含文件夹中丢失?
- Eventloop 卡住:NAO C++ SDK OnFaceDetection 示例
- Valgrind:可以处理更多可能丢失的字节吗?
- 当对套接字 send() 的同步调用由于连接另一端丢失而被阻止时,如何恢复?
- 截取屏幕截图后程序卡住
- 更改其类型后,丢失对 std::variant 对象的引用
- C++丢失了参考
- 安装 zlib 库仍然丢失错误
- 如何解决隐式转换丢失整数精度:'size_t'(又名"无符号长")到'int'警告?
- 处理编译器关于可能丢失数据的警告的最优雅方法是什么
- C++键盘挂钩 CTRL 键卡住
- 从 lambda 返回的对象将丢失属性值
- 向量成员在管理类指针C++队列时丢失
- 作业-操作符-重载货币类-卡住/丢失