Question
Your task in this homework assignment is to overload four operators ( ,+,-) in the Money class. The money class stores the value in *dollars*
Your task in this homework assignment is to overload four operators (<,>,+,-) in the Money class.
The money class stores the value in *dollars* and *cents*.
An example overloaded operator has been provided for equal (==).
## Solution Specifications
Your solution to this problem must meet the following criteria.
1. Write a functions to overload __less than__, __greater than__, __addition__, and __subtraction__.
2. The functions should be written as part of the money class.
Using C++ language:
Money.h:
#include
#include
#include
using namespace std;
// Class for amounts of money in U.S. currency.
class Money {
public:
Money();
Money(double amount);
Money(int theDollars, int theCents);
Money(int theDollars);
double getAmount() const;
int getDollars() const;
int getCents() const;
bool operator==(const Money &amount2);
bool operator<(const Money &amount2);
bool operator>(const Money &amount2);
const Money operator+(const Money &amount2);
const Money operator-(const Money &amount2);
friend ostream &operator<<(ostream &outputStream, const Money &amount);
private:
// negative amounts are represented as negative dollars
// and negative cents. Negative $4.50 is represented as -4
//(dollars) and -50 (cents)
int dollars;
int cents;
int dollarsPart(double amount) const;
int centsPart(double amount) const;
int round(double number) const;
};
Money.cpp:
#include "Money.h"
#include
#include
#include
using namespace std;
bool Money::operator==(const Money &amount2) {
return ((dollars == amount2.dollars) &&
(cents == amount2.cents));
}
// TODO Add overloading < operator here
// TODO Add overloading > operator here
// TODO Add overloading + operator here
// TODO Add overloading - operator here
Money::Money() {
dollars = 0;
cents = 0;
}
Money::Money(double amount) {
dollars = dollarsPart(amount);
cents = centsPart(amount);
}
Money::Money(int theDollars) {
dollars = theDollars;
cents = 0;
}
// Uses cstdlib:
Money::Money(int theDollars, int theCents) {
if ((theDollars < 0 && theCents > 0) || (theDollars > 0 && theCents < 0)) {
cout << "Inconsistent money data. ";
exit(1);
}
dollars = theDollars;
cents = theCents;
}
double Money::getAmount() const { return (dollars + cents * 0.01); }
int Money::getDollars() const { return dollars; }
int Money::getCents() const { return cents; }
int Money::dollarsPart(double amount) const { return static_cast
int Money::centsPart(double amount) const {
double doubleCents = amount * 100;
int intCents = (round(fabs(doubleCents))) % 100; //% can misbehave
// for negatives
if (amount < 0)
intCents = -intCents;
return intCents;
}
int Money::round(double number) const { return floor(number + 0.5); }
ostream &operator<<(ostream &outputStream, const Money &amount) {
int absDollars = abs(amount.dollars);
int absCents = abs(amount.cents);
if (amount.dollars < 0 || amount.cents < 0)
// accounts for dollars == 0 or cents == 0
outputStream << "$-";
else
outputStream << '$';
outputStream << absDollars;
if (absCents >= 10)
outputStream << '.' << absCents;
else
outputStream << '.' << '0' << absCents;
return outputStream;
}
Main.cpp:
#include
#include
using namespace std;
#include "Money.h"
int main() {
Money yourAmount(16.45), myAmount(10.09);
cout << "Your amount is " << yourAmount << endl;
cout << "My amount is " << myAmount << endl;
if (yourAmount == myAmount) {
cout << "We have the same amounts. ";
} else {
cout << "One of us is richer. ";
}
Money ourAmount = yourAmount + myAmount;
cout << yourAmount << " + " << myAmount << " equals " << ourAmount << endl;
Money diffAmount = yourAmount - myAmount;
cout << yourAmount << " - " << myAmount << " equals " << diffAmount << endl;
if (ourAmount > diffAmount) {
cout << "The sum of our Amounts is greater than the difference. ";
} else {
cout << "Something is terribly wrong. ";
}
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started