作业-操作符-重载货币类-卡住/丢失

Homework - Operator-Overloading Currency Class - Stuck/Lost

本文关键字:卡住 丢失 货币 操作符 重载 作业      更新时间:2023-10-16

我需要帮助创建操作符重载函数请。我试过了,但是卡住了。谢谢大家上次的帮助!我能够完全完成:])。

问题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/,可能也会使用它们)。

我还会消除GetCentsGetDollars成员,而是添加一个重载的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对象打印出$本身。如果需要更详细地说明,可以让它从区域设置检索正确的货币指示符和小数分隔符。