Skip to content

Instantly share code, notes, and snippets.

@MORTAL2000
Created December 21, 2015 20:45
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 MORTAL2000/12e14ff41ed3c72f8df9 to your computer and use it in GitHub Desktop.
Save MORTAL2000/12e14ff41ed3c72f8df9 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <exception>
class Money
{
public:
Money(uint64_t amount = 0);
// c++98
Money(const Money& other);
Money(Money&& other) noexcept;
~Money();
Money& operator=(const Money& other);
Money& operator=(Money&& other);
void swap(Money& other);
// logic operations
bool operator<(const Money& other) const
{
return m_amount < other.m_amount;
}
bool operator>(const Money& other) const
{
return other < *this;
}
bool operator>=(const Money& other) const
{
return !(*this < other);
}
bool operator<=(const Money& other) const
{
return !(other < *this);
}
bool operator==(const Money& other) const
{
return m_amount == other.m_amount;;
}
bool operator!=(const Money& other) const
{
return !(*this == other);
}
// arithmetic operations
Money operator+=(const Money& other)
{
m_amount += other.m_amount;
return *this;
}
friend Money operator+(Money lhs, const Money& rhs)
{
return lhs += rhs;
}
Money operator-=( const Money& other)
{
m_amount -= other.m_amount;
return *this;
}
friend Money operator-(Money lhs, const Money& rhs)
{
return lhs -= rhs;
}
Money operator*=(const Money& other)
{
m_amount *= other.m_amount;
return *this;
}
friend Money operator*(Money lhs, const Money& rhs)
{
return lhs *= rhs;
}
Money operator/=(const Money& other)
{
if (other.m_amount == 0)
throw std::invalid_argument("Division by zero");
m_amount /= other.m_amount;
return *this;
}
friend Money operator/(Money lhs, const Money& rhs)
{
return lhs /= rhs;
}
friend std::ostream& operator<<(std::ostream& os, const Money& money);
private:
uint64_t m_amount;
};
Money::Money(uint64_t amount)
: m_amount(amount)
{
}
Money::Money(const Money& other)
: m_amount(other.m_amount)
{
}
Money::Money(Money&& other) noexcept
: m_amount(std::move(other.m_amount))
{
}
Money::~Money()
{
}
Money& Money::operator=(const Money& other)
{
Money temp(other);
temp.swap(*this);
return *this;
}
Money& Money::operator=(Money&& other)
{
other.swap(*this);
return *this;
}
void Money::swap(Money& other)
{
using std::swap;
swap(m_amount, other.m_amount);
}
std::ostream& operator<<(std::ostream& os, const Money& money)
{
// correction factor
int cf = 1;
if (money.m_amount == 1)
{
return os << '$' << money.m_amount;
}
if (money.m_amount > 1000000)
{
cf *= 100;
}
uint64_t dollars = money.m_amount / (100 * cf);
uint64_t cents = money.m_amount % (100 * cf);
return os << '$' << dollars << '.' << cents;
}
int main()
{
Money your_money(170000);
Money my_money(130000);
Money his_money = std::move(your_money);
std::cout << your_money + my_money << '\n';
std::cout << your_money - my_money << '\n';
std::cout << std::endl;
std::cout << your_money << '\n';
std::cout << my_money << '\n';
std::cout << his_money << '\n';
std::cout << std::endl;
his_money = your_money - my_money;
if (my_money < your_money)
std::cout << "i'm poor (~.~) !\n";
if (my_money > his_money)
std::cout << "i'm rich ~~(^.^)~~ YEY!\n";
std::cout << (your_money = your_money * 170000) << '\n';
std::cout << (your_money = your_money / 170000) << '\n';
std::cout << (your_money *= 170000) << '\n';
std::cout << (your_money /= 170000) << '\n';
std::cout << (your_money * your_money) << '\n';
std::cout << (your_money / your_money) << '\n';
your_money = Money(170000);
std::cout << (your_money *= your_money) << '\n';
std::cout << (your_money /= your_money) << '\n';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment