Created
September 12, 2013 00:48
-
-
Save retep998/6531892 to your computer and use it in GitHub Desktop.
coin bank
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <int64_t V> | |
class coin { | |
public: | |
coin() : num {0} {} | |
coin(coin const &) = default; | |
explicit coin(int64_t n) : num {n} {} | |
coin & operator=(coin const &) = default; | |
void add(coin const & o) { | |
num += o.num; | |
} | |
void remove(coin const & o) { | |
num -= o.num; | |
} | |
int64_t count() { | |
return num; | |
} | |
void set(int64_t n) { | |
num = n; | |
} | |
private: | |
int64_t num; | |
}; | |
typedef coin<1> pennies; | |
typedef coin<5> nickels; | |
typedef coin<10> dimes; | |
typedef coin<25> quarters; | |
typedef coin<50> half_dollars; | |
typedef coin<100> dollar_coins; | |
template <typename...C> | |
class coin_bank { | |
public: | |
template <typename T, typename...R> | |
void add(T v, R...r) { | |
std::get<T>(coins).add(v); | |
add(std::forward<R>(r...)); | |
} | |
void add() {} | |
template <typename T, typename...R> | |
void remove(T v, R...r) { | |
std::get<T>(coins).subtract(v); | |
subtract(std::forward<R>(r...)); | |
} | |
void remove() {} | |
template <typename T, typename...R> | |
void set(T v, R...r) { | |
std::get<T>(coins) = v; | |
set(std::forward<R>(r...)); | |
} | |
void set() {} | |
template <typename T> | |
T get() { | |
return std::get<T>(coins); | |
} | |
private: | |
std::tuple<C...> coins; | |
}; | |
typedef coin_bank<pennies, nickels, dimes, quarters, half_dollars, dollar_coins> american_coin_bank; | |
int main() { | |
american_coin_bank wut; | |
wut.add(pennies {5}, nickels {7}); | |
wut.remove(pennies {2}); | |
wut.set(nickels {5}); | |
std::cout << "pennies: " << wut.get<pennies>().count() << endl; | |
std::cout << "nickels: " << wut.get<nickels>().count() << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment