1

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>
9
  • 1
    @Riotson: It is too big a code for anyone to follow, Please remove out the irrelevant parts and post a minimilistic code where you are facing a problem. Commented May 2, 2011 at 4:36
  • Sorry, will do that right now! Commented May 2, 2011 at 4:36
  • Hope that's better, if not let me know and I'll try and reduce it more! Commented May 2, 2011 at 4:45
  • 1
    @Riotson: If you see a chance of reducing it more..Do it! Makie it minimilistic, You will notice It helps people help you better and quicker and saves everyones efforts. Commented May 2, 2011 at 4:47
  • I think I'd try to reduce it to a class with a single overloaded operator (one that's giving a problem), and main that just creates an object, uses the overloaded operator, and (for example) prints out the result to show the problem. Commented May 2, 2011 at 4:47

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

6 Comments

@Jerry Coffin : I must be doing something wrong, I did everything you said (Done up to and did the constructor part), and it tells me within the "Currency operator+(Currency left, Current const &right)" that "Dollars" and "Cents" are inaccessible. Sorry if it's something simple I'm missing...very tired from cramming. (Also I went down to where I have payroll3= payroll + payroll2; and it says that the " + " matches more than 1 operand...I don't see how, I removed everything except for the code you typed, to have less code and only use one addition operator as you recommended).
Sorry -- I should have mentioned that if you create a global overerload, you'll need to declare it as a friend of the class: friend Currrency operator+(Currency, Currency const &);
@Jerry Coffin : Ah thank you! That part works now :]. My next problem is...how do I pass in currDollars and currCents into "payroll" object? Whenever I try it gives me an error telling me " error LNK2001: unresolved external symbol "public: __thiscall Currency::Currency(int,int)" (??0Currency@@QAE@HH@Z):"
And thank you for taking the time to write all this out and help me! I appreciate it!!! I've also updated my code...I've never worked with global variables, so hopefully I took out all the right stuff.
I included a declaration of the ctor above, but didn't include its implementation (since this is homework, I'm trying to leave a bit for you to figure out/deal with). It should be pretty simple though, just initializing Dollars and Cents from what you pass in...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.