• R/O
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

練習用です。いろんなものがごちゃまぜです。


Commit MetaInfo

Revision52 (tree)
Time2015-10-12 11:23:54
Authorbellyoshi

Log Message

(empty log message)

Change Summary

Incremental Difference

--- TeachYourSelf/arraytype.cpp (nonexistent)
+++ TeachYourSelf/arraytype.cpp (revision 52)
@@ -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
--- TeachYourSelf/strtype.cpp (revision 51)
+++ TeachYourSelf/strtype.cpp (revision 52)
@@ -9,9 +9,12 @@
99 public:
1010 strtype(char *ptr);
1111 strtype(const strtype &s);
12+ strtype &operator=(strtype &ob);
13+ char &operator[](int i){return p[i];}
1214 ~strtype();
1315 char *get(){return p;}
1416 int getlen(){return len;}
17+
1518 };
1619
1720 strtype::strtype(char *ptr)
@@ -29,6 +32,7 @@
2932
3033 strtype::strtype(const strtype &s)
3134 {
35+ cout << "コピーコンストラクタ\n";
3236 len = s.len;
3337 p = new char[len];
3438 if (!p){
@@ -37,7 +41,21 @@
3741 }
3842 strncpy(p, s.p,len);
3943 }
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+}
4159 strtype::~strtype()
4260 {
4361 cout << "pを解放する。\n";
@@ -52,8 +70,15 @@
5270 int main()
5371 {
5472 strtype s1("This is a test."), s2("I like C++.");
73+ strtype s3 = s1;
74+ s3 = s1;
5575 show(s1);
5676 show(s2);
77+ show(s3);
78+ for(int i = 0; i < s1.getlen(); i++){
79+ cout << s1[i] << " ";
80+ }
81+ cout << "\n";
5782
5883 return 0;
5984 }
\ No newline at end of file
--- TeachYourSelf/dynaarray.cpp (nonexistent)
+++ TeachYourSelf/dynaarray.cpp (revision 52)
@@ -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
--- TeachYourSelf/coord.cpp (revision 51)
+++ TeachYourSelf/coord.cpp (revision 52)
@@ -18,7 +18,23 @@
1818 coord operator--();
1919 coord operator--(int notused);
2020 coord operator-();
21+ friend coord operator+(coord ob1, int i);
22+ friend coord operator+(int i, coord ob1);
2123 };
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+}
2238 int coord::operator==(coord ob2)
2339 {
2440 return x == ob2.x && y == ob2.y;
@@ -108,5 +124,7 @@
108124 print(o3);
109125 o3--;
110126 print(o3);
127+ o3 = o3 + 2;
128+ print(o3);
111129 return 0;
112130 }
\ No newline at end of file