Homework - Operator-Overloading Currency Class - Stuck/Lost

邮差的信 提交于 2019-12-07 11:34:25

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:

Currency operator+(Currency const &c);

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...):

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;
}

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:

Currency(int dollars = 0, int cents = 0);

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 an operator+ that takes a single int, it take the int and converts it to a Currency object using the ctor, then adds that Currency 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 named normalize or something like that, which would then be used from both operator+ and operator- (and if you add an operator* and/or operator/, probably them as well).

I'd also eliminate the GetCents and GetDollars members, instead adding an overloaded operator<< to handle a Currency object directly:

std::ostream &operator<<(std::ostream &os, Currency const &c) { 
    return os << c.Dollars << "." 
              << std::setfill('0') << std::setw(2) << std::setprecision(2) 
              << c.Cents;
}

With this, you can replace this block of code:

     cout << "Current Amount is:$ " 
     << payroll.GetDollars() 
     << ".";
     if(payroll.GetCents() < 10)
     {cout << "0";}
     else
     cout 
     << payroll.GetCents()
     << endl;
     cout << endl;

With something a bit shorter and more readable:

    cout << "Current amount is: $" << payroll << endl;

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!