Question: Do the operator overloading (+, and ==) in the next program. I already did the operator +. #include #include using namespace std; class AltMoney {
Do the operator overloading (+, <,> and ==) in the next program. I already did the operator +.
#include
class AltMoney { public: AltMoney(); AltMoney(int d, int c); friend AltMoney operator +(AltMoney m1, AltMoney m2); void display_money(); private: int dollars; int cents; };
void read_money(int& d, int& c);
int main() { int d, c; AltMoney m1, m2, sum;
read_money(d, c); m1 = AltMoney(d, c); cout << "The first money is:"; m1.display_money();
read_money(d, c); m2 = AltMoney(d, c); cout << "The second money is:"; m2.display_money();
sum = m1 + m2; cout << "The sum is:"; sum.display_money();
system("pause"); return 0; }
AltMoney::AltMoney() { }
AltMoney::AltMoney(int d, int c) { dollars = d; cents = c; }
void AltMoney::display_money() { cout << "$" << dollars << "."; if (cents <= 9) cout << "0"; //to display a 0 on the left for numbers less than 10 cout << cents << endl; }
AltMoney operator +(AltMoney m1, AltMoney m2) { AltMoney temp; int extra = 0; temp.cents = m1.cents + m2.cents; if (temp.cents >= 100) { temp.cents = temp.cents - 100; extra = 1; } temp.dollars = m1.dollars + m2.dollars + extra;
return temp; } void read_money(int& d, int& c) { cout << "Enter dollar "; cin >> d; cout << "Enter cents "; cin >> c; if (d < 0 || c < 0) { cout << "Invalid dollars and cents, negative values "; exit(1); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
