Skip to content

Instantly share code, notes, and snippets.

@deque-blog deque-blog/MoneyFowler.cpp
Last active Aug 15, 2017

Embed
What would you like to do?
class Money {
Amount m_amount;
Currency m_currency;
public:
Money(Amount const& amount, Currency const& currency)
: m_amount(amount)
, m_currency(currency)
{}
Amount amount() const { return m_amount; }
Currency currency() const { return m_currency; }
};
bool operator==(Money const& lhs, Money const& rhs)
{
return lhs.amount() == rhs.amount() && lhs.currency() == rhs.currency();
}
bool operator!=(Money const& lhs, Money const& rhs)
{
return !(lhs == rhs);
}
Money add(Money const& lhs, Money const& rhs)
{
if (lhs.currency() != rhs.currency())
throw std::logic_error("Currency should be equal");
return Money(lhs.amount() + rhs.amount(), lhs.currency());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.