Modify the definition of the class Money so that the following are added: - Over
ID: 3621603 • Letter: M
Question
Modify the definition of the class Money so that the following are added:- Overload the operators > and >= as member functions for the Money class.
- Overload the << operator as a friend function for the Money class.
/*
Name:
Copyright:
Author:
Date: 15/10/08 20:25
Description:
*/
#ifndef _MONEY_H
#define _MONEY_H
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
//Class for amounts of money in U.S. currency.
class Money
{
public:
friend const Money operator +(const Money& amount1, const Money& amount2);
Money( );
Money(double amount);
Money(int theDollars, int theCents);
Money(int theDollars);
double getAmount( ) const;
int getDollars( ) const;
int getCents( ) const;
//void set(int, int); //
void input( ); //Reads the dollar sign as well as the amount number.
void output( ) const;
// const Money operator + (const Money& yourAmount) const;
const Money operator ++(); //increase dollars and cents by 1, prefix ++
private:
int dollars; //A negative amount is represented as negative dollars and
int cents; //negative cents. Negative $4.50 is represented as -4 and -50
int dollarsPart(double amount) const;
int centsPart(double amount) const;
int round(double number) const;
};
const Money operator -(const Money& amount1, const Money& amount2);
bool operator ==(const Money& amount1, const Money& amount2);
const Money operator -(const Money& amount);
#endif
/*
Name:
Copyright:
Author:
Date: 15/10/08 20:26
Description:
*/
#include "money.h"
Money::Money( ): dollars(0), cents(0)
{/*Body intentionally empty.*/}
Money::Money(double amount)
: dollars(dollarsPart(amount)), cents(centsPart(amount))
{/*Body intentionally empty*/}
Money::Money(int theDollars)
: dollars(theDollars), cents(0)
{/*Body intentionally empty*/}
//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;
}
//Uses iostream and cstdlib:
void Money::output( ) const
{
int absDollars = abs(dollars);
int absCents = abs(cents);
if (dollars < 0 || cents < 0)//accounts for dollars == 0 or cents == 0
cout << "$-";
else
cout << '$';
cout << absDollars;
if (absCents >= 10)
cout << '.' << absCents;
else
cout << '.' << '0' << absCents;
}
//Uses iostream and cstdlib:
void Money::input( )
{
char dollarSign;
cin >> dollarSign; //hopefully
if (dollarSign != '$')
{
cout << "No dollar sign in Money input. ";
exit(1);
}
double amountAsDouble;
cin >> amountAsDouble;
dollars = dollarsPart(amountAsDouble);
cents = centsPart(amountAsDouble);
}
int Money::dollarsPart(double amount) const
{
return static_cast<int>(amount);
}
int Money::centsPart(double amount) const
{
double doubleCents = amount*100;
int intCents = (round(fabs(doubleCents)))%100;//% can misbehave on negatives
if (amount < 0)
intCents = -intCents;
return intCents;
}
int Money::round(double number) const
{
return static_cast<int>(floor(number + 0.5));
}
/*
const Money Money::operator +(const Money & yourAmount) const
{
return Money(dollars+yourAmount.dollars, cents + yourAmount.cents);
}
*/
const Money Money::operator ++()
{
++dollars;
++cents;
return Money(dollars, cents);
}
const Money operator +(const Money& amount1, const Money& amount2)
{
//Money sum;
//int allCents1 = amount1.getCents( ) + amount1.getDollars( )*100;
int allCents1 = amount1.cents + amount1.dollars*100;
int allCents2 = amount2.getCents( ) + amount2.getDollars( )*100;
int sumAllCents = allCents1 + allCents2;
int absAllCents = abs(sumAllCents); //Money can be negative.
int finalDollars = absAllCents/100;
int finalCents = absAllCents%100;
if (sumAllCents < 0)
{
finalDollars = -finalDollars;
finalCents = -finalCents;
}
//sum.set(finalDollars, finalCents);
//return sum;
return Money(finalDollars, finalCents);
}
//Uses cstdlib:
const Money operator -(const Money& amount1, const Money& amount2)
{
int allCents1 = amount1.getCents( ) + amount1.getDollars( )*100;
int allCents2 = amount2.getCents( ) + amount2.getDollars( )*100;
int diffAllCents = allCents1 - allCents2;
int absAllCents = abs(diffAllCents);
int finalDollars = absAllCents/100;
int finalCents = absAllCents%100;
if (diffAllCents < 0)
{
finalDollars = -finalDollars;
finalCents = -finalCents;
}
return Money(finalDollars, finalCents);
}
bool operator ==(const Money& amount1, const Money& amount2)
{
return ((amount1.getDollars( ) == amount2.getDollars( ))
&& (amount1.getCents( ) == amount2.getCents( )));
}
const Money operator -(const Money& amount)
{
return Money(-amount.getDollars( ), -amount.getCents( ));
}
/*
Name:
Copyright:
Author:
Date: 15/10/08 20:27
Description:
*/
#include "money.h"
int main( )
{
Money yourAmount, myAmount(10, 9);
cout << "Enter an amount of money: ";
yourAmount.input( );
cout << "Your amount is ";
yourAmount.output( );
cout << endl;
cout << "My amount is ";
myAmount.output( );
cout << endl;
++myAmount;
cout << "++myAmount is ";
myAmount.output( );
cout << endl;
if (yourAmount == myAmount)
cout << "We have the same amounts. ";
else
cout << "One of us is richer. ";
Money ourAmount = yourAmount + myAmount;
yourAmount.output( ); cout << " + "; myAmount.output( );
cout << " equals "; ourAmount.output( ); cout << endl;
Money diffAmount = yourAmount - myAmount;
yourAmount.output( ); cout << " - "; myAmount.output( );
cout << " equals "; diffAmount.output( ); cout << endl;
return 0;
}
Explanation / Answer
please rate - thanks
sorry, I only work in 1 fike. you'll have to rebreak it apart
I think I highlighed all the changes
/*
Name:
Copyright:
Author:
Date: 15/10/08 20:26
Description:
*/
/*
Name:
Copyright:
Author:
Date: 15/10/08 20:27
Name:
Copyright:
Author:
Date: 15/10/08 20:25
Description:
*/
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
//Class for amounts of money in U.S. currency.
class Money
{
public:
friend const Money operator +(const Money& amount1, const Money& amount2);
friend ostream &operator<<( ostream &, const Money& );
friend istream &operator>>( istream &, Money& );
Money( );
Money(double amount);
Money(int theDollars, int theCents);
Money(int theDollars);
double getAmount( ) const;
int getDollars( ) const;
int getCents( ) const;
int setCents(int ) ;
int setDollars( int );
bool operator >( const Money& amount2);
bool operator >=( const Money& amount2);
//void set(int, int); //
void input( ); //Reads the dollar sign as well as the amount number.
void output( ) const;
// const Money operator + (const Money& yourAmount) const;
const Money operator ++(); //increase dollars and cents by 1, prefix ++
private:
int dollars; //A negative amount is represented as negative dollars and
int cents; //negative cents. Negative $4.50 is represented as -4 and -50
int dollarsPart(double amount) const;
int centsPart(double amount) const;
int round(double number) const;
};
const Money operator -(const Money& amount1, const Money& amount2);
bool operator ==(const Money& amount1, const Money& amount2);
const Money operator -(const Money& amount);
Money::Money( ): dollars(0), cents(0)
{/*Body intentionally empty.*/}
Money::Money(double amount)
: dollars(dollarsPart(amount)), cents(centsPart(amount))
{/*Body intentionally empty*/}
Money::Money(int theDollars)
: dollars(theDollars), cents(0)
{/*Body intentionally empty*/}
//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::setCents(int amount )
{
cents=amount;
}
int Money::setDollars( int amount)
{
dollars=amount;
}
//Uses iostream and cstdlib:
void Money::output( ) const
{
int absDollars = abs(dollars);
int absCents = abs(cents);
if (dollars < 0 || cents < 0)//accounts for dollars == 0 or cents == 0
cout << "$-";
else
cout << '$';
cout << absDollars;
if (absCents >= 10)
cout << '.' << absCents;
else
cout << '.' << '0' << absCents;
}
//Uses iostream and cstdlib:
void Money::input( )
{
char dollarSign;
cin >> dollarSign; //hopefully
if (dollarSign != '$')
{
cout << "No dollar sign in Money input. ";
exit(1);
}
double amountAsDouble;
cin >> amountAsDouble;
dollars = dollarsPart(amountAsDouble);
cents = centsPart(amountAsDouble);
}
int Money::dollarsPart(double amount) const
{
return static_cast<int>(amount);
}
int Money::centsPart(double amount) const
{
double doubleCents = amount*100;
int intCents = (round(fabs(doubleCents)))%100;//% can misbehave on negatives
if (amount < 0)
intCents = -intCents;
return intCents;
}
int Money::round(double number) const
{
return static_cast<int>(floor(number + 0.5));
}
/*
const Money Money::operator +(const Money & yourAmount) const
{
return Money(dollars+yourAmount.dollars, cents + yourAmount.cents);
}
*/
const Money Money::operator ++()
{
++dollars;
++cents;
return Money(dollars, cents);
}
const Money operator +(const Money& amount1, const Money& amount2)
{
//Money sum;
//int allCents1 = amount1.getCents( ) + amount1.getDollars( )*100;
int allCents1 = amount1.cents + amount1.dollars*100;
int allCents2 = amount2.getCents( ) + amount2.getDollars( )*100;
int sumAllCents = allCents1 + allCents2;
int absAllCents = abs(sumAllCents); //Money can be negative.
int finalDollars = absAllCents/100;
int finalCents = absAllCents%100;
if (sumAllCents < 0)
{
finalDollars = -finalDollars;
finalCents = -finalCents;
}
//sum.set(finalDollars, finalCents);
//return sum;
return Money(finalDollars, finalCents);
}
//Uses cstdlib:
const Money operator -(const Money& amount1, const Money& amount2)
{
int allCents1 = amount1.getCents( ) + amount1.getDollars( )*100;
int allCents2 = amount2.getCents( ) + amount2.getDollars( )*100;
int diffAllCents = allCents1 - allCents2;
int absAllCents = abs(diffAllCents);
int finalDollars = absAllCents/100;
int finalCents = absAllCents%100;
if (diffAllCents < 0)
{
finalDollars = -finalDollars;
finalCents = -finalCents;
}
return Money(finalDollars, finalCents);
}
bool operator ==(const Money& amount1, const Money& amount2)
{
return ((amount1.getDollars( ) == amount2.getDollars( ))
&& (amount1.getCents( ) == amount2.getCents( )));
}
bool Money::operator >( const Money& amount2)
{
if(getDollars( ) > amount2.getDollars( ))
return true;
if((getDollars( ) == amount2.getDollars( ))&&
(getCents( ) > amount2.getCents( )))
return true;
return false;
}
bool Money::operator >=(const Money& amount2)
{
if(getDollars( ) > amount2.getDollars( ))
return true;
if((getDollars( ) == amount2.getDollars( ))&&
(getCents( ) > amount2.getCents( )))
return true;
return ((getDollars( ) == amount2.getDollars( ))
&& (getCents( ) == amount2.getCents( )));
}
const Money operator -(const Money& amount)
{
return Money(-amount.getDollars( ), -amount.getCents( ));
}
ostream& operator<<( ostream &output, const Money& amount2 )
{int absDollars = abs(amount2.getDollars( ));
int absCents = abs(amount2.getCents( ));
if (amount2.getDollars( ) < 0 || amount2.getCents( ) < 0)//accounts for dollars == 0 or cents == 0
cout << "$-";
else
cout << '$';
cout << absDollars;
if (absCents >= 10)
cout << '.' << absCents;
else
cout << '.' << '0' << absCents;
return output;
}
istream &operator>>( istream & input, Money& amount2 )
{char dollarSign;
cin >> dollarSign; //hopefully
if (dollarSign != '$')
{
cout << "No dollar sign in Money input. ";
exit(1);
}
double amountAsDouble;
cin >> amountAsDouble;
amount2.setDollars(amount2.dollarsPart(amountAsDouble));
amount2.setCents( amount2.centsPart(amountAsDouble));
}
int main( )
{
Money yourAmount, myAmount(10, 9);
cout << "Enter an amount of money: ";
yourAmount.input( );
cout << "Your amount is ";
yourAmount.output( );
cout << endl;
cout << "My amount is ";
myAmount.output( );
cout << endl;
cout<<"Enter an amount ";
++myAmount;
cout << "++myAmount is ";
myAmount.output( );
cout << endl;
if (yourAmount >= myAmount)
cout << "We have the same amounts. or you have more ";
else
cout << "I am richer. ";
if (myAmount > yourAmount)
cout << "I have more. ";
else
cout << "you have more or we have the same. ";
cin>>myAmount;
cout<<myAmount<<endl;
Money ourAmount = yourAmount + myAmount;
yourAmount.output( ); cout << " + "; myAmount.output( );
cout << " equals "; ourAmount.output( ); cout << endl;
Money diffAmount = yourAmount - myAmount;
yourAmount.output( ); cout << " - "; myAmount.output( );
cout << " equals "; diffAmount.output( ); cout << endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.