Skip to content

Instantly share code, notes, and snippets.

@deque-blog
Last active August 15, 2017 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deque-blog/d0faeaa147a0ee4a6c2ff2cdd61649de to your computer and use it in GitHub Desktop.
Save deque-blog/d0faeaa147a0ee4a6c2ff2cdd61649de to your computer and use it in GitHub Desktop.
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