Im having a really hard time understanding how this section of code works. I get everything in the program besides the adding part and it seems no matter how many time a re-read the section in my book or watch videos I simply dont get it. Please try to explain it as simple as possible.

Money Money::operator+(Money& amount2)
{
double sum = dollars + amount2.dollars;
return Money(sum);

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` `````` #include "stdafx.h" #include #include #include using namespace std; //Class for amounts of money in U.S. currency. class Money { public: Money(); Money(double amount); double getDollars(); void setDollars(double amount); Money operator+(Money& amount2); private: double dollars; }; int main() { Money yourAmount, myAmount(100.00); yourAmount.setDollars(3.5); cout << "Your amount is " << yourAmount.getDollars() << endl; cout << "My amount is " << myAmount.getDollars() << endl; Money ourAmount = yourAmount + myAmount; cout << yourAmount.getDollars() << " + " << myAmount.getDollars() << " equals " << ourAmount.getDollars() << endl; system("pause"); return 0; } Money Money::operator+(Money& amount2) { double sum = dollars + amount2.dollars; return Money(sum); } Money::Money() { dollars = 0; } Money::Money(double amount) //: dollars(dollarsPart(amount)), cents(centsPart(amount)) { dollars = amount; } double Money::getDollars() { return dollars; } void Money::setDollars(double amount) { dollars = amount; } ``````

Last edited on
An alternative way of writing `yourAmount + myAmount` is to write `yourAmount.operator+(myAmount)`. Think of operator+ as being the function. yourAmount is the object that the function is called on. myAmount is the argument that is passed to the function.