Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
C++ Operator Overloading
//clang 3.8.0 #include <iostream> using namespace std; struct USCurrency { int dollars; int cents; USCurrency operator+(const USCurrency o) { USCurrency tmp= {0,0}; tmp.cents = cents + o.cents; tmp.dollars = dollars + o.dollars; if(tmp.cents >= 100) { tmp.dollars +=1; tmp.cents -=100; } return tmp; } }; /*USCurrency operator+(const USCurrency m, const USCurrency o) { USCurrency tmp = {0,0}; tmp.cents = m.cents + o.cents; tmp.dollars = m.dollars + o.dollars; if (tmp.cents >= 100) { tmp.dollars +=1; tmp.cents -=100; } return tmp; }*/ ostream& operator<<(ostream &output, const USCurrency &o) { output << "$" << o.dollars << "." << o.cents; return output; } int main() { USCurrency a = {2, 50}; USCurrency b = {1, 75}; USCurrency c = a + b; cout << c << endl; return 0; }
run
|
edit
|
history
|
help
0
uniq ptr
Throttle Example using a circular queue (Push all but 2 less than maxSize; then pop all but 2 of current size)
6 7
enum operator
numeric_limits
Check if a year is leap year or not
Narrowing error
Pascals Triangle
regex
Variadic Template: Make Index Sequence