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
<string> No indirect include of <errno.h>
Apple is not convertible to itself (clang 3.8.0)
Simple Generic Data Type Example
Copy uint64 data into uint32 with padding
Move Construction
Array-Based Heap Example Starter Code
Test titlu
Dynamically allocated array in unique_ptr with custom deleter.
Struct memory ordering
Code Rush Game Group 1