开发者

Homework - Operator-Overloading Currency Class - Stuck/Lost

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 A开发者_StackOverflow社区mount:$ " << payroll3.GetDollars() << "."
<< payroll3.GetCents() << endl << endl;

return 0;

}

</pre></code>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜