練習用です。いろんなものがごちゃまぜです。
(empty log message)
| @@ -0,0 +1,35 @@ | ||
| 1 | +#include <iostream> | |
| 2 | +using namespace std; | |
| 3 | + | |
| 4 | +const int SIZE = 5; | |
| 5 | + | |
| 6 | +class arraytype{ | |
| 7 | + int a[SIZE]; | |
| 8 | +public: | |
| 9 | + arraytype(){ | |
| 10 | + for(int i = 0;i < SIZE; i++)a[i] = i; | |
| 11 | + } | |
| 12 | + int &operator[](int i); | |
| 13 | +}; | |
| 14 | + | |
| 15 | +int &arraytype::operator[](int i) | |
| 16 | +{ | |
| 17 | + if (i < 0 || i > SIZE -1){ | |
| 18 | + cout << "\n"; | |
| 19 | + cout << i << "は境界エラー\n"; | |
| 20 | + exit(1); | |
| 21 | + } | |
| 22 | + return a[i]; | |
| 23 | +} | |
| 24 | +int main() | |
| 25 | +{ | |
| 26 | + arraytype ob; | |
| 27 | + ob[2] = 13; | |
| 28 | + for(int i = 0; i < SIZE; i++){ | |
| 29 | + cout << ob[i] << " "; | |
| 30 | + } | |
| 31 | + ob[100] = 2; | |
| 32 | + cout << "\n"; | |
| 33 | + | |
| 34 | + return 0; | |
| 35 | +} | |
| \ No newline at end of file |
| @@ -9,9 +9,12 @@ | ||
| 9 | 9 | public: |
| 10 | 10 | strtype(char *ptr); |
| 11 | 11 | strtype(const strtype &s); |
| 12 | + strtype &operator=(strtype &ob); | |
| 13 | + char &operator[](int i){return p[i];} | |
| 12 | 14 | ~strtype(); |
| 13 | 15 | char *get(){return p;} |
| 14 | 16 | int getlen(){return len;} |
| 17 | + | |
| 15 | 18 | }; |
| 16 | 19 | |
| 17 | 20 | strtype::strtype(char *ptr) |
| @@ -29,6 +32,7 @@ | ||
| 29 | 32 | |
| 30 | 33 | strtype::strtype(const strtype &s) |
| 31 | 34 | { |
| 35 | + cout << "コピーコンストラクタ\n"; | |
| 32 | 36 | len = s.len; |
| 33 | 37 | p = new char[len]; |
| 34 | 38 | if (!p){ |
| @@ -37,7 +41,21 @@ | ||
| 37 | 41 | } |
| 38 | 42 | strncpy(p, s.p,len); |
| 39 | 43 | } |
| 40 | - | |
| 44 | +strtype &strtype::operator=(strtype &ob) | |
| 45 | +{ | |
| 46 | + cout << "=オペレータ\n"; | |
| 47 | + if(len < ob.len){ | |
| 48 | + delete [] p; | |
| 49 | + p = new char [ob.len]; | |
| 50 | + if (!p){ | |
| 51 | + cout << "メモリ割り当てエラー"; | |
| 52 | + exit(1); | |
| 53 | + } | |
| 54 | + } | |
| 55 | + len = ob.len; | |
| 56 | + strcpy(p, ob.p); | |
| 57 | + return *this; | |
| 58 | +} | |
| 41 | 59 | strtype::~strtype() |
| 42 | 60 | { |
| 43 | 61 | cout << "pを解放する。\n"; |
| @@ -52,8 +70,15 @@ | ||
| 52 | 70 | int main() |
| 53 | 71 | { |
| 54 | 72 | strtype s1("This is a test."), s2("I like C++."); |
| 73 | + strtype s3 = s1; | |
| 74 | + s3 = s1; | |
| 55 | 75 | show(s1); |
| 56 | 76 | show(s2); |
| 77 | + show(s3); | |
| 78 | + for(int i = 0; i < s1.getlen(); i++){ | |
| 79 | + cout << s1[i] << " "; | |
| 80 | + } | |
| 81 | + cout << "\n"; | |
| 57 | 82 | |
| 58 | 83 | return 0; |
| 59 | 84 | } |
| \ No newline at end of file |
| @@ -0,0 +1,51 @@ | ||
| 1 | +#include <iostream> | |
| 2 | +using namespace std; | |
| 3 | + | |
| 4 | +class dynarray { | |
| 5 | + int *p; | |
| 6 | + int size; | |
| 7 | +public: | |
| 8 | + dynarray(int a); | |
| 9 | + int &operator[](int i){return p[i];} | |
| 10 | + dynarray &operator=(dynarray &ob); | |
| 11 | +}; | |
| 12 | + | |
| 13 | +dynarray::dynarray(int a) | |
| 14 | +{ | |
| 15 | + p = new int[a]; | |
| 16 | + if (!p){ | |
| 17 | + cout << "メモリ割り当てエラー\n"; | |
| 18 | + exit(1); | |
| 19 | + } | |
| 20 | + size = a; | |
| 21 | +} | |
| 22 | +dynarray &dynarray::operator=(dynarray &ob) | |
| 23 | +{ | |
| 24 | + if(size < ob.size){ | |
| 25 | + delete [] p; | |
| 26 | + p = new int[ob.size]; | |
| 27 | + if (!p){ | |
| 28 | + cout << "メモリ割り当てエラー\n"; | |
| 29 | + exit(1); | |
| 30 | + } | |
| 31 | + } | |
| 32 | + size = ob.size; | |
| 33 | + for(int i = 0; i < size; i++){ | |
| 34 | + p[i] = ob.p[i]; | |
| 35 | + } | |
| 36 | + return *this; | |
| 37 | +} | |
| 38 | + | |
| 39 | +int main(){ | |
| 40 | + dynarray d1(10); | |
| 41 | + for(int i = 0; i < 10; i++){ | |
| 42 | + d1[i] = i * i; | |
| 43 | + } | |
| 44 | + d1[5] = 30; | |
| 45 | + dynarray d2(0); | |
| 46 | + d2 = d1; | |
| 47 | + for(int i = 0; i < 10; i++){ | |
| 48 | + cout << d2[i] << "\n"; | |
| 49 | + } | |
| 50 | + return 0; | |
| 51 | +} | |
| \ No newline at end of file |
| @@ -18,7 +18,23 @@ | ||
| 18 | 18 | coord operator--(); |
| 19 | 19 | coord operator--(int notused); |
| 20 | 20 | coord operator-(); |
| 21 | + friend coord operator+(coord ob1, int i); | |
| 22 | + friend coord operator+(int i, coord ob1); | |
| 21 | 23 | }; |
| 24 | +coord operator+(int i, coord ob1) | |
| 25 | +{ | |
| 26 | + coord temp; | |
| 27 | + temp.x = ob1.x + i; | |
| 28 | + temp.y = ob1.y + i; | |
| 29 | + return temp; | |
| 30 | +} | |
| 31 | +coord operator+(coord ob1, int i) | |
| 32 | +{ | |
| 33 | + coord temp; | |
| 34 | + temp.x = ob1.x + i; | |
| 35 | + temp.y = ob1.y + i; | |
| 36 | + return temp; | |
| 37 | +} | |
| 22 | 38 | int coord::operator==(coord ob2) |
| 23 | 39 | { |
| 24 | 40 | return x == ob2.x && y == ob2.y; |
| @@ -108,5 +124,7 @@ | ||
| 108 | 124 | print(o3); |
| 109 | 125 | o3--; |
| 110 | 126 | print(o3); |
| 127 | + o3 = o3 + 2; | |
| 128 | + print(o3); | |
| 111 | 129 | return 0; |
| 112 | 130 | } |
| \ No newline at end of file |