Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4564270
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:28:37+00:00 2026-05-21T18:28:37+00:00

I need help with creating Operator-Overloaded functions please. I tried 2 but I am

  • 0

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>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T18:28:38+00:00Added an answer on May 21, 2026 at 6:28 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help understanding some C++ operator overload statements. The class is declared like
I need help to replace all \n (new line) caracters for in a String,
I need help creating a method that allows me to create a bunch of
I need help creating the php for my contact form, here is the form
I need help creating an object for this declare UIWebView *webView; I thought it
I need help creating a self extracting zip file that does not display anything.
I need help on creating an application like Visio in Silverlight, of course a
I need some help creating this extension method. My view inherits from <%@ Page
Ok need some help, I have a system I'm creating that will allow users
Hi need help creating a script to convert the extension of a tif image

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.