#include #include #include "coffee.h" using namespace std; coffee::coffee(int init_mg) : mg {init_mg} { if (mg < 0) { cerr << "Bad caffeine amount " << init_mg << "\n"; exit(EXIT_FAILURE); } } coffee::coffee() : mg {100} //default cup has 100 mg remaining { } coffee& coffee::operator+=(int n) { mg += n; if (mg < 0) { //if someone added a negative number too large mg = 0; } return *this; } coffee& coffee::operator-=(int n) { mg -= n; if (mg < 0) { mg = 0; } return *this; } coffee& coffee::operator++() //prefix { *this += 1; return *this; } const coffee coffee::operator++(int) //postfix { coffee old {*this}; //copy ++(*this); //call prefix return old; } coffee& coffee::operator--() //prefix { *this -= 1; return *this; } const coffee coffee::operator--(int) //postfix { coffee old {*this}; --(*this); return old; } ostream& operator<<(ostream& os, const coffee& c) { return os << c.mg << "mg"; } int operator-(const coffee& a, const coffee& b) { return a.mg - b.mg; } bool operator==(const coffee& a, const coffee& b) { return a.mg == b.mg; } bool operator<(const coffee& a, const coffee& b) { return a.mg < b.mg; }