HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

Money class with overloaded operators

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
overloadedmoneywithclassoperators

Problem

I have program that uses a Money class which has several constructors for different methods of input and overloaded operators to add, subtract, and compare values.

I am having difficulty defining my int asCents(const Money& amount) const; method so that it can be used in other functions. Used correctly, is it best practice to return the integer it produces as opposed to creating two new ones equivalent to it (amount.getCents() + amount.getDollars() * 100;)? How else can this program be improved?

```
#include
#include
#include
#include
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ostream;
using std::istream;

class Money {

public:

Money();
Money(double asMoney);
Money(int justDollars);
Money(int withDollars, int withCents);

friend const Money operator +(const Money& amount1, const Money& amount2);
friend const Money operator -(const Money& amount1, const Money& amount2);

friend const Money operator %(const Money& amount, double percentage);

friend bool operator ==(const Money& amount1, const Money& amount2);
friend bool operator (const Money& amount1, const Money& amount2);
friend const Money operator -(const Money& amount1);
friend ostream& operator >(istream& inputStream, Money& amount);

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

//int asCents(const Money& amount) const; // unused
Money percent(const Money& amount, double percentage) const;

private:

int dollars;
int cents;
int dollarsPart(double amount) const;
int centsPart(double amount) const;
int round(double number) const;
};

int Money::centsPart(double amount) const {
double doubleCents = amount * 100;
int intCents = (round(fabs(doubleCents))) % 100;
if (amount (amount); }
int Money::round(double amount) const { return static_cast(floor(amount + 0.5)); }

const int asCents(const Money& amount) {
int

Solution

This is way too complicated. You could have just the cents as a member and have some helper functions that converts it to the respective parts. This will simplify most of your functions a lot.

const Money operator +(const Money& amount1, const Money& amount2) 
{
    return Money(amount1.cents + amount2.cents);
}

const Money operator -(const Money& amount1, const Money& amount2)
{  
    return Money(amount1.cents - amount2.cents)
}

int dolarsFloor() const
{
    return cents / 100;
}

int centsReminder() const
{

    return cents % 100 ;
}


There are other things that can be improved but I just wanted to leave my 2 cents ;).

Code Snippets

const Money operator +(const Money& amount1, const Money& amount2) 
{
    return Money(amount1.cents + amount2.cents);
}

const Money operator -(const Money& amount1, const Money& amount2)
{  
    return Money(amount1.cents - amount2.cents)
}

int dolarsFloor() const
{
    return cents / 100;
}

int centsReminder() const
{

    return cents % 100 ;
}

Context

StackExchange Code Review Q#105794, answer score: 13

Revisions (0)

No revisions yet.