Question

C++ Redo Practice program 1 from chapter 11, but this time define the Money ADT class...

C++

Redo Practice program 1 from chapter 11, but this time define the Money ADT class in separate files for the interface and implementation so that the implementation can be compiled separately from any application program.

Submit three files:

  • YourLastNameFirstNameInitialMoney.cpp - This file contains only the Money class implementation
  • YourLastNameFirstNameInitialProj4.cpp - This file contains only the main function to use/test your money class
  • YourLastNameFirstNameInitialMoney.h - This file contains the header file information.  

Optional bonus (10%): Do some research on make files. Include a make file that will compile these three files.  

This is the original practice program 11.

#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.\n";

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;

return 0;

}

int Money::setDollars( int amount)

{

dollars=amount;

return 0;

}

//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.\n";

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.\n";

exit(1);

}

double amountAsDouble;

cin >> amountAsDouble;

amount2.setDollars(amount2.dollarsPart(amountAsDouble));

amount2.setCents( amount2.centsPart(amountAsDouble));

return 0;

}

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\n";

else

cout << "I am richer.\n";

if (myAmount > yourAmount)

cout << "I have more.\n";

else

cout << "you have more or we have the same.\n";

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;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1) File name : YourLastNameFirstNameInitialMoney.cpp

Possible Errors in your provided code:

check if return value of this method:{ istream &operator>>(istream &input, Money &amount2) } is correct.

Code:

#include <iostream>
#include <cstdlib>
#include <cmath>
#include "YourLastNameFirstNameInitialMoney.h"
using namespace std;

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.\n";
       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;
   return 0;
}

int Money::setDollars(int amount)
{
   dollars = amount;
   return 0;
}

//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.\n";
       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.\n";
       exit(1);
   }
   double amountAsDouble;
   cin >> amountAsDouble;
   amount2.setDollars(amount2.dollarsPart(amountAsDouble));
   amount2.setCents(amount2.centsPart(amountAsDouble));
   return 0;
}

1) File name : YourLastNameFirstNameInitialProj4.cpp

Possible errors in the code provided:

Looks like you forgot to implement Money - Money operation, do check that once.

Code:

#include <iostream>
#include <cstdlib>
#include <cmath>
#include "YourLastNameFirstNameInitialMoney.h"

using namespace std;

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\n";
   else
       cout << "I am richer.\n";
   if (myAmount > yourAmount)
       cout << "I have more.\n";
   else
       cout << "you have more or we have the same.\n";
   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;
}

3) Filename: YourLastNameFirstNameInitialMoney.h

Code:

#ifndef MONEY_H

#define MONEY_H

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;

};

#endif

4) Filename : Makefile

Instructions:

  • Save the file with name Makefile
  • To make the file open the project folder in terminal & type make and hit enter to compile.
  • To run the program type ./main and hit enter.

Code:

all:    #target name
    g++ YourLastNameFirstNameInitialProj4.cpp YourLastNameFirstNameInitialMoney.cpp -o main

Add a comment
Answer #2

Here is the completed code for this problem. I have only splitted the code into three files namely – money.h, money.cpp and main.cpp as requested. Did not change any logic of your original program, though fixed some minor bugs. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

//money.h file

#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);

    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);

#endif

//money.cpp file

#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.\n";

        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;

    return 0;

}

int Money::setDollars(int amount)

{

    dollars = amount;

    return 0;

}

//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.\n";

        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

        output << "$-";

    else

        output << '$';

    output << absDollars;

    if (absCents >= 10)

        output << '.' << absCents;

    else

        output << '.' << '0' << absCents;

    return output;

}

istream& operator>>(istream& input, Money& amount2)

{

    char dollarSign;

    input >> dollarSign; //hopefully

    if (dollarSign != '$')

    {

        cout << "No dollar sign in Money input.\n";

        exit(1);

    }

    double amountAsDouble;

    input >> amountAsDouble;

    amount2.setDollars(amount2.dollarsPart(amountAsDouble));

    amount2.setCents(amount2.centsPart(amountAsDouble));

    return input;

}

//main.cpp file

#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. or you have more\n";

    else

        cout << "I am richer.\n";

    if (myAmount > yourAmount)

        cout << "I have more.\n";

    else

        cout << "you have more or we have the same.\n";

               

                cout<<"Enter an amount ";

    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;

}

/*OUTPUT*/

Enter an amount of money: $2.5

Your amount is $2.50

My amount is $10.09

++myAmount is $11.10

I am richer.

I have more.

Enter an amount $15.03

$15.03

$2.50 + $15.03 equals $17.53

$2.50 - $15.03 equals $-12.53

//makefile (for linux based systems)

g++ -c money.h

g++ -c money.cpp

g++ -c main.cpp

g++ -o main main.o money.o

./main

Add a comment
Know the answer?
Add Answer to:
C++ Redo Practice program 1 from chapter 11, but this time define the Money ADT class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Here are the files I've made so far: /********************* * File: check05b.cpp *********************/ #include &lt...

    Here are the files I've made so far: /********************* * File: check05b.cpp *********************/ #include <iostream> using namespace std; #include "money.h" /**************************************************************** * Function: main * Purpose: Test the money class ****************************************************************/ int main() {    int dollars;    int cents;    cout << "Dollar amount: ";    cin >> dollars;    cout << "Cents amount: ";    cin >> cents;    Money m1;    Money m2(dollars);    Money m3(dollars, cents);    cout << "Default constructor: ";    m1.display();    cout << endl;    cout << "Non-default constructor 1: ";    m2.display();    cout << endl;   ...

  • Overview This checkpoint is intended to help you practice the syntax of operator overloading with member...

    Overview This checkpoint is intended to help you practice the syntax of operator overloading with member functions in C++. You will work with the same money class that you did in Checkpoint A, implementing a few more operators. Instructions Begin with a working (but simplistic) implementation of a Money class that contains integer values for dollars and cents. Here is my code: /********************* * File: check12b.cpp *********************/ #include using namespace std; #include "money.h" /**************************************************************** * Function: main * Purpose: Test...

  • The following code is for Chapter 13 Programming Exercise 21. I'm not sure what all is...

    The following code is for Chapter 13 Programming Exercise 21. I'm not sure what all is wrong with the code written. I get errors about stockSym being private and some others after that.This was written in the Dev C++ software. Can someone help me figure out what is wrong with the code with notes of what was wrong to correct it? #include <cstdlib> #include <iostream> #include <iomanip> #include <fstream> #include <cassert> #include <string> using namespace std; template <class stockType> class...

  • In this assignment you are required to complete a class for fractions. It stores the numerator...

    In this assignment you are required to complete a class for fractions. It stores the numerator and the denominator, with the lowest terms. It is able to communicate with iostreams by the operator << and >>. For more details, read the header file. //fraction.h #ifndef FRACTION_H #define FRACTION_H #include <iostream> class fraction { private: int _numerator, _denominator; int gcd(const int &, const int &) const; // If you don't need this method, just ignore it. void simp(); // To get...

  • Project 1 - Wallet Following the class diagram shown below, create the class Wallet. A Wallet...

    Project 1 - Wallet Following the class diagram shown below, create the class Wallet. A Wallet represents a carrier of bills and coins. You use your wallet to pay for things, which reduces the balance held inside. When you visit an ATM, you can fill it back up with cash again. A sample driver for this class is shown below. You should probably make a more thorough driver to test your class better. I will have my own driver which...

  • #include <iostream> #include <string> #include "hashT.h" #include "stateData.h" using namespace std; void stateData::setStateInfo(string sName, string sCapital,...

    #include <iostream> #include <string> #include "hashT.h" #include "stateData.h" using namespace std; void stateData::setStateInfo(string sName, string sCapital,    double stArea, int yAdm, int oAdm) {    stateName = sName; stateCapital = sCapital; stArea = stateArea; yAdm = yearOfAdmission; oAdm = orderOfAdmission;       } void stateData::getStateInfo(string& sName, string& sCapital,    double& stArea, int& yAdm, int& oAdm) {    sName = stateName; sCapital = stateCapital; stArea = stateArea; yAdm = yearOfAdmission; oAdm = orderOfAdmission;       } string stateData::getStateName() { return stateName;...

  • Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: --------------------...

    Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: ------------------------------------------------------------------------------------------------------------------------------------------ main.cpp #include #include "rectangleType.h" using namespace std; // part e int main() { rectangleType rectangle1(10, 5); rectangleType rectangle2(8, 7); rectangleType rectangle3; rectangleType rectangle4; cout << "rectangle1: " << rectangle1 << endl; cout << "rectangle2: " << rectangle2 << endl; rectangle3 = rectangle1 + rectangle2;    cout << "rectangle3: " << rectangle3 << endl; rectangle4 = rectangle1 * rectangle2;    cout << "rectangle4: " << rectangle4 << endl; if (rectangle1 > rectangle2) cout << "Area...

  • ////****what am i doing wrong? im trying to have this program with constructors convert inches to...

    ////****what am i doing wrong? im trying to have this program with constructors convert inches to feet too I don't know what im doing wrong. (the program is suppose to take to sets of numbers feet and inches and compare them to see if they are equal , greater, less than, or not equal using operator overload #include <stdio.h> #include <string.h> #include <iostream> #include <iomanip> #include <cmath> using namespace std; //class declaration class FeetInches { private:    int feet;   ...

  • In this lab, you will need to implement the following functions in Text ADT with C++...

    In this lab, you will need to implement the following functions in Text ADT with C++ language(Not C#, Not Java please!): PS: The program I'm using is Visual Studio just to be aware of the format. And I have provided all informations already! Please finish step 1, 2, 3, 4. Code is the correct format of C++ code. a. Constructors and operator = b. Destructor c. Text operations (length, subscript, clear) 1. Implement the aforementioned operations in the Text ADT...

  • Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and...

    Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and user_main.cpp. The programming language is C++. I will provide the Sample Test Driver Code as well as the codes given so far in text below. Sample Testing Driver Code: cs52::FlashDrive empty; cs52::FlashDrive drive1(10, 0, false); cs52::FlashDrive drive2(20, 0, false); drive1.plugIn(); drive1.formatDrive(); drive1.writeData(5); drive1.pullOut(); drive2.plugIn(); drive2.formatDrive(); drive2.writeData(2); drive2.pullOut(); cs52::FlashDrive combined = drive1 + drive2; // std::cout << "this drive's filled to " << combined.getUsed( )...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT