Note that there are some explanatory texts on larger screens.

plurals
  1. POHomework - Operator-Overloading Currency Class - Stuck/Lost
    primarykey
    data
    text
    <p>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 :] ).</p> <p><strong>Problem 1:</strong> The operator+(const Currency &amp;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).</p> <p><strong>Problem 2:</strong> 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 :(.</p> <p>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.</p> <p><pre><code> class Currency { private:</p> <pre><code>int Dollars; int Cents; //string Amount; </code></pre> <p>public: // Constructor Currency(int Dollars = 0, int Cents = 0); friend Currency operator+(Currency, Currency const &amp;); // Addition</p> <pre><code>// Get int GetDollars();// Need to be removed int GetCents();// Need to be removed </code></pre> <p>}; </p> <p>Currency::Currency(int Dollars, int Cents) { this->Dollars = Dollars; this->Cents = Cents;</p> <pre><code>if(this-&gt;Cents &gt;= 100) { this-&gt;Dollars += 1; this-&gt;Cents -= 100; } </code></pre> <p>}</p> <p>Currency operator+(Currency left, Currency const &amp;right) { left.Dollars += right.Dollars; left.Cents += right.Cents; while (left.Cents >= 100) { left.Cents -= 100; left.Dollars += 1; } return left; }</p> <p>int Currency::GetDollars() { return Dollars; }</p> <p>int Currency::GetCents() { return Cents; }</p> <p>int main() {</p> <pre><code>int currDollars; int currCents; //char answer; cout &lt;&lt; "Please enter a dollar amount" &lt;&lt; endl; cin &gt;&gt; currDollars; cout &lt;&lt; "Please enter a cents amount:" &lt;&lt; endl; cin &gt;&gt; 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 &lt;&lt; "Payroll3 Amount:$ " &lt;&lt; payroll3.GetDollars() &lt;&lt; "." &lt;&lt; payroll3.GetCents() &lt;&lt; endl &lt;&lt; endl; return 0; </code></pre> <p>}</p> &lt;/pre&gt;&lt;/code&gt; </code></pre>
    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.
 

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