Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'ve been stuck on this for days... I\'ve also included the money.h and money.c

ID: 3635833 • Letter: I

Question

I've been stuck on this for days...


I've also included the money.h and money.cpp


Create a money class using the following UML diagram:
money
-dollars
-cents
+money(int = 0, int = 0) :
+setMoney(int, int) : void
+getMoney(int &, int &) : void
+dollarsToCents() const : int

operator<<(ostream&, money) : ostream&
operator>>(istream&, money&) : istream&
operator+(money, money) : money
operator==(money, money) : bool
operator!=(money, money) : bool

2. You may wish to start with files money.h and money.cpp.

3. Create a driver program, lastname8.cpp that tests each of the functions in your class. Be sure that the output of the driver program is documented, easy to read, and easy to understand

money.cpp
#include <iostream>
#include "money.h"

using namespace std;

money::money(int d, int c)
{
dollars = d;
cents = c;
}

void money::setMoney(int d, int c)
{
dollars = d;
cents = c;
}

void money::getMoney(int & d, int & c) const
{
d = dollars;
c = cents;
}

void money::print() const
{
cout << "$" << dollars << ".";

if (cents < 10)
cout << "0";

cout << cents << endl;
}

int money::dollarsToCents() const
{
return (dollars * 100 + cents);
}

bool money::equalMoney(money otherMoney) const
{
return (dollars == otherMoney.dollars && cents == otherMoney.cents);
}


money.h
// Filename: money.h

#ifndef MONEY_H
#define MONEY_H

class money
{
public:
money(int = 0, int = 0);
void setMoney(int, int);
void getMoney(int&, int&) const;
void print() const; // in the form $1.05
int dollarsToCents() const; // converts $1.05 to 105 cents
bool equalMoney(money otherMoney) const; // checks if two money objects are equal
private:
int dollars;
int cents;
};

#endif

Explanation / Answer

This should get you most of the way there. Not been compiled/debugged etc. You need to do some work on it, and write your test routines etc.

money.h

#ifndef MONEY_H

#include <iostream>

class money
{
private:
    int dollars, cents;

    void rationalize();
   
public:
    money(int d = 0, int c = 0);
    void setMoney(int d, int c);
    void getMoney( int &d, int &c);
    int dollarsToCents(void) const;
   
    money& operator+(const money &rhs);
    bool operator==(const money &rhs);
    bool operator!=(const money &rhs) { return !(*this == rhs); }
   
};

extern std::ostream & operator<<(std::ostream &, const money &);
extern std::istream & operator>>(std::istream &, const money &);

#endif

money.cpp

#include "money.h"
using namespace std;

void money::rationalize()
{
    if (cents > 100)
    {
        dollars += cents / 100;
        cents %= 100;
    }
}
   
money::money(int d = 0, int c = 0)
{
    setMoney(d,c);
}

void money::setMoney(int d, int c)
{
    dollars = d;
    cents = c;
    rationalize();
}

void money::getMoney( int &d, int &c)
{
    d = dollars;
    c = cents;
}

int money::dollarsToCents(void) const
{
    return dollars*100 + cents;
}
   
money money::operator+(const money &rhs)
{
    money m;
    m.dollars = dollars + rhs.dollars;
    m.cents = cents + rhs.cents;
    m.rationalize();
    return m;
}

bool money::operator==(const money &rhs)
{
    return (dollars == rhs.dollars) && (cents == rhs.cents);
}

ostream & operator<<(std::ostream &out, const money &m)
{
    int d, c;
    m.getMoney(d,c);
   
    out << "$" << d << ".";
   
    if (c == 0)
        out << "00";
    else if (c < 10)
        out << "0" << c;
    else
        out << c;
       
    return out;
}

istream & operator>>(std::istream &in, const money &m)
{
    // left as exercise
    return in;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote