練習用です。いろんなものがごちゃまぜです。
(empty log message)
| @@ -18,6 +18,8 @@ | ||
| 18 | 18 | coord operator--(); |
| 19 | 19 | coord operator--(int notused); |
| 20 | 20 | coord operator-(); |
| 21 | + coord operator<<(int x); | |
| 22 | + coord operator>>(int x); | |
| 21 | 23 | friend coord operator+(coord ob1, int i); |
| 22 | 24 | friend coord operator+(int i, coord ob1); |
| 23 | 25 | }; |
| @@ -44,6 +46,20 @@ | ||
| 44 | 46 | return (x && ob2.x) && (y && ob2.y); |
| 45 | 47 | } |
| 46 | 48 | |
| 49 | +coord coord::operator<<(int n) | |
| 50 | +{ | |
| 51 | + coord temp; | |
| 52 | + temp.x = x << n; | |
| 53 | + temp.y = y << n; | |
| 54 | + return temp; | |
| 55 | +} | |
| 56 | +coord coord::operator>>(int n) | |
| 57 | +{ | |
| 58 | + coord temp; | |
| 59 | + temp.x = x >> n; | |
| 60 | + temp.y = y >> n; | |
| 61 | + return temp; | |
| 62 | +} | |
| 47 | 63 | coord coord::operator+(coord &ob2) |
| 48 | 64 | { |
| 49 | 65 | coord temp; |
| @@ -126,5 +142,9 @@ | ||
| 126 | 142 | print(o3); |
| 127 | 143 | o3 = o3 + 2; |
| 128 | 144 | print(o3); |
| 145 | + o3 = o3 << 2; | |
| 146 | + print(o3); | |
| 147 | + o3 = o3 >> 1; | |
| 148 | + print(o3); | |
| 129 | 149 | return 0; |
| 130 | 150 | } |
| \ No newline at end of file |
| @@ -0,0 +1,74 @@ | ||
| 1 | +#include <iostream> | |
| 2 | +using namespace std; | |
| 3 | + | |
| 4 | +class three_d { | |
| 5 | + int x, y, z; | |
| 6 | +public: | |
| 7 | + three_d(int i, int j, int k) | |
| 8 | + { | |
| 9 | + x = i;y = j; z = k; | |
| 10 | + } | |
| 11 | + three_d(){ x = z = y = 0;} | |
| 12 | + void get(int &i, int &j, int &k) | |
| 13 | + { | |
| 14 | + i = x;j = y;k = z; | |
| 15 | + } | |
| 16 | + three_d operator+(three_d ob); | |
| 17 | + three_d operator-(three_d ob); | |
| 18 | + three_d operator++(); | |
| 19 | + three_d operator--(); | |
| 20 | +}; | |
| 21 | + | |
| 22 | +three_d three_d::operator+(three_d ob) | |
| 23 | +{ | |
| 24 | + three_d temp; | |
| 25 | + temp.x = x + ob.x; | |
| 26 | + temp.y = y + ob.y; | |
| 27 | + temp.z = z + ob.z; | |
| 28 | + return temp; | |
| 29 | +} | |
| 30 | + | |
| 31 | +three_d three_d::operator-(three_d ob) | |
| 32 | +{ | |
| 33 | + three_d temp; | |
| 34 | + temp.x = x - ob.x; | |
| 35 | + temp.y = y - ob.y; | |
| 36 | + temp.z = z - ob.z; | |
| 37 | + return temp; | |
| 38 | +} | |
| 39 | +three_d three_d::operator++() | |
| 40 | +{ | |
| 41 | + x++; | |
| 42 | + y++; | |
| 43 | + z++; | |
| 44 | + return *this; | |
| 45 | +} | |
| 46 | +three_d three_d::operator--() | |
| 47 | +{ | |
| 48 | + x--; | |
| 49 | + y--; | |
| 50 | + z--; | |
| 51 | + return *this; | |
| 52 | +} | |
| 53 | +void print(three_d ob) | |
| 54 | +{ | |
| 55 | + int x,y,z; | |
| 56 | + ob.get(x,y,z); | |
| 57 | + cout << x << ',' << y << ',' << z << "\n"; | |
| 58 | +} | |
| 59 | + | |
| 60 | +int main() | |
| 61 | +{ | |
| 62 | + three_d a(10,5,15),b(1,1,2); | |
| 63 | + print(a); | |
| 64 | + ++a; | |
| 65 | + print(a); | |
| 66 | + three_d c = a + b; | |
| 67 | + print(c); | |
| 68 | + --c; | |
| 69 | + print(c); | |
| 70 | + c = a-b; | |
| 71 | + print(c); | |
| 72 | + | |
| 73 | + return 0; | |
| 74 | +} | |
| \ No newline at end of file |