Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help correcting the errors in my C++ program and completing it please. I need help in overloading the following operators (-, ) I

I need help correcting the errors in my C++ program and completing it please. I need help in overloading the following operators (-, <, and >) I am unsure on how to do those, but I have the (+, ==, >>, and <<) completed already. I am also getting some errors referring to my class that I need corrected. I am including the code and program instructions. Thank you.

Create a class called Money that will contain two integer components, dollars and cents. Write the necessary setters and getters so you can access Money objects. Also, add appropriate constructors to initialize variables of Money. Create a private simplify() method that will ensure that the cents member variable is always in the range [1..99]. Make a call to simplify() in the appropriate class methods. Overload the following operators: (-, <, >, +, ==, >>, <<) The input stream operator (>>) should read whitespace, then read the $, and finally read a floating point number. It should then store the respective values to dollars and cents. For example, if the input was " $45.80", the function would store 45 to dollars and 80 to cents. The output stream operator (<<) should simply display a dollar sign, the dollars amount, a period, followed by the cents amount. Also, write a main driver program to test your class. Include enough examples so that it is obvious that all of the operations defined for the class are working correctly.

My code:

#include #include using namespace std; class Money; ostream &operator << (ostream &, const Money &); ifstream &operator >> (istream&, Money &); class Money { private: int dollars; int cents; void simplify(); public: void setCents(int); void setDollars(int); int getCents() const; int getDollars() const; //void print() const; Money(); Money(int, int ); bool operator == (const Money &); Money operator + (const Money &); //friends friend ostream &operator << (ostream &, const Money &); friend ifstream &operator >> (istream&, Money &); };

using namespace std;

int main() { Money first, second, third; cin >> first; cin >> second;

cout << first << " and " << second << endl; if (first == second) cout << "They are equal" << endl; else cout << "They are not equal" << endl; third = first + second; cout << third;

return 0; } Money::Money (int d, int c) { dollars =d; cents = c; simplify(); }

Money::Money() { dollars = 0; cents = 0; }

void Money::setCents(int c) { cents = c; simplify(); }

void Money::setDollars(int d) { dollars = d; }

int Money::getCents() const { return cents; } int Money::getDollars() const { return dollars; }

void Money::simplify() { if (cents >= 100) { dollars += cents / 100; cents = cents % 100; } else if (cents < 0) { dollars -= 1 + (abs(cents) / 100); cents = 100 - (abs(cents) % 100); } } bool Money :: operator == (const Money &right) // equals less than (convert to inches and then compare inches) { bool status; if (dollars == right.dollars && cents == right.cents) status = true; else status = false; return status; } Money Money::operator + (const Money &right) { Money temp; temp.dollars = dollars + right.dollars; temp.cents + cents + right.cents; temp.simplify(); return temp; }

ostream &operator << (ostream &strm, const Money &obj) { strm << obj.dollars << "dollars, " << obj.cents << "cents, "; return strm; }

istream &operator >> (istream &strm, Money &obj) { cout << "Please enter the feet"; strm >> obj.dollars; cout << "Please enter the inches"; strm >> obj.cents; obj.simplify(); return strm; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Expert Oracle9i Database Administration

Authors: Sam R. Alapati

1st Edition

1590590228, 978-1590590225

More Books

Students also viewed these Databases questions

Question

Explain IN DETAIL how a revolving check credit with a bank works.

Answered: 1 week ago

Question

What do you believe was the cause of the turnover problem?

Answered: 1 week ago