Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, now that we have little enough code to really look at, a few suggestions. First, I think I'd have only <em>one</em> addition operator:</p> <pre><code>Currency operator+(Currency const &amp;c); </code></pre> <p>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...):</p> <pre><code>Currency operator+(Currency left, Current const &amp;right) { left.Dollars += right.Dollars; left.Cents += right.Cents; while (left.Cents &gt;= 100) { left.Cents -= 100; left.Dollars += 1; } return left; } </code></pre> <p>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.</p> <p>I'd also have only one constructor, using default parameters for the amount:</p> <pre><code>Currency(int dollars = 0, int cents = 0); </code></pre> <p>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.</p> <p>By combining the two of these, you can still do things like adding <code>1</code>, but it's handled a little bit differently -- instead of directly using an <code>operator+</code> that takes a single int, it take the int and converts it to a <code>Currency</code> object using the ctor, then adds that <code>Currency</code> 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.</p> <p>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 <code>while</code> loop that's above into a separate (private) function named <code>normalize</code> or something like that, which would then be used from both <code>operator+</code> and <code>operator-</code> (and if you add an <code>operator*</code> and/or <code>operator/</code>, probably them as well).</p> <p>I'd also eliminate the <code>GetCents</code> and <code>GetDollars</code> members, instead adding an overloaded <code>operator&lt;&lt;</code> to handle a Currency object directly:</p> <pre><code>std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, Currency const &amp;c) { return os &lt;&lt; c.Dollars &lt;&lt; "." &lt;&lt; std::setfill('0') &lt;&lt; std::setw(2) &lt;&lt; std::setprecision(2) &lt;&lt; c.Cents; } </code></pre> <p>With this, you can replace this block of code:</p> <pre><code> cout &lt;&lt; "Current Amount is:$ " &lt;&lt; payroll.GetDollars() &lt;&lt; "."; if(payroll.GetCents() &lt; 10) {cout &lt;&lt; "0";} else cout &lt;&lt; payroll.GetCents() &lt;&lt; endl; cout &lt;&lt; endl; </code></pre> <p>With something a bit shorter and more readable:</p> <pre><code> cout &lt;&lt; "Current amount is: $" &lt;&lt; payroll &lt;&lt; endl; </code></pre> <p>Edit: Given that it's intended to be used only for money, you could have the Currency object print out the <code>$</code> itself. If you wanted to get more elaborate, you could have it retrieve the correct currency designator and decimal separator from the locale.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload