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