質問編集履歴

1

書式の改善、タイトルの変更、内容の追加

2019/07/01 05:43

投稿

退会済みユーザー
test CHANGED
@@ -1 +1 @@
1
- インスタンスが生成できない
1
+ c++ インスタンスが生成できない
test CHANGED
@@ -14,25 +14,13 @@
14
14
 
15
15
  ```
16
16
 
17
-
18
-
19
- ### 該当のソースコード
20
-
21
-
22
-
23
- ```
24
-
25
- ソースコード
26
-
27
- ```言語:c++
28
-
29
- #main.cpp
17
+ //main.cpp
30
-
18
+
31
- include "Circle.h"
19
+ #include "Circle.h"
32
-
20
+
33
- include "ColorCircle.h"
21
+ #include "ColorCircle.h"
34
-
22
+
35
- include "LineCircle.h"
23
+ #include "LineCircle.h"
36
24
 
37
25
 
38
26
 
@@ -54,7 +42,7 @@
54
42
 
55
43
  }
56
44
 
57
- else {// i が奇数の場合
45
+ else {// i が奇数の場合 ここでエラー
58
46
 
59
47
  circ = new LineCircle(20 * (i + 1), 20, 10, "red", 3);
60
48
 
@@ -78,18 +66,58 @@
78
66
 
79
67
  }
80
68
 
81
-
69
+ ``````
70
+
82
-
71
+ ```c++
83
-
84
-
72
+
85
- #LineCircle.cpp
73
+ //LineCircle.h
74
+
86
-
75
+ #pragma once
76
+
87
- include"LineCircle.h"
77
+ #include "ColorCircle.h"
88
78
 
89
79
  using namespace std;
90
80
 
91
81
 
92
82
 
83
+ class LineCircle : public ColorCircle {
84
+
85
+ protected:
86
+
87
+ string LineColor;
88
+
89
+ int width;
90
+
91
+ public:
92
+
93
+ LineCircle();
94
+
95
+ LineCircle(int cx, int cy, int r, string c1, int w);
96
+
97
+ virtual void draw(svg*);
98
+
99
+ void setLineColor(string c1);
100
+
101
+ void setwidth(int w);
102
+
103
+ };
104
+
105
+
106
+
107
+ ```
108
+
109
+
110
+
111
+ ```
112
+
113
+ //LinceCircle.cpp
114
+
115
+ #include"LineCircle.h"
116
+
117
+ using namespace std;
118
+
119
+
120
+
93
121
  LineCircle::LineCircle() : ColorCircle() {
94
122
 
95
123
  LineColor = "black";
@@ -136,14 +164,56 @@
136
164
 
137
165
 
138
166
 
167
+
168
+
169
+ ``````
170
+
171
+
172
+
173
+ ```
174
+
139
- #ColorCircle.cpp
175
+ //ColorCircle.h
176
+
140
-
177
+ #pragma once
178
+
141
- include "ColorCircle.h"
179
+ #include"Circle.h"
142
180
 
143
181
  using namespace std;
144
182
 
145
183
 
146
184
 
185
+ class ColorCircle : public Circle {
186
+
187
+ protected:
188
+
189
+ string color; //描画色
190
+
191
+ public:
192
+
193
+ ColorCircle(); //コンストラクタ
194
+
195
+ ColorCircle(int cx, int cy, int r, string c); //引数ありコンストラクタ
196
+
197
+
198
+
199
+ virtual void draw(svg*); //Circleクラスのdrawをオーバーライド
200
+
201
+ void setColor(string c); //色の設定
202
+
203
+ };
204
+
205
+ ```
206
+
207
+ ```
208
+
209
+ //ColorCircle.cpp
210
+
211
+ #include "ColorCircle.h"
212
+
213
+ using namespace std;
214
+
215
+
216
+
147
217
  ColorCircle::ColorCircle() : Circle() {
148
218
 
149
219
  color = "black";
@@ -178,11 +248,63 @@
178
248
 
179
249
 
180
250
 
251
+
252
+
253
+ ``````
254
+
255
+ ```
256
+
257
+ //Circle.h
258
+
259
+ #pragma once
260
+
261
+ #include<iostream>
262
+
263
+ #include"svg.h"
264
+
265
+
266
+
267
+ class Circle {
268
+
269
+ protected:
270
+
271
+ int x, y;
272
+
273
+ int rad;
274
+
275
+
276
+
277
+ public:
278
+
181
- #Circle.cpp
279
+ Circle();
280
+
182
-
281
+ Circle(int cx, int cy, int r = 10);
282
+
283
+ void setPosition(int x, int y);
284
+
285
+ void setRadius(int rad);
286
+
287
+ int getRadius();
288
+
289
+ void getPosition(int xy[]);
290
+
291
+ virtual void draw(svg*) = 0;
292
+
293
+ //bool checkOverlap(Circle circ);
294
+
295
+ };
296
+
297
+
298
+
299
+ ```
300
+
301
+ ``````
302
+
303
+ //Circle.cpp
304
+
183
- include"Circle.h"
305
+ #include"Circle.h"
306
+
184
-
307
+ #include<cmath>
185
-
186
308
 
187
309
 
188
310
 
@@ -254,6 +376,134 @@
254
376
 
255
377
 
256
378
 
379
+ /*bool Circle::checkOverlap(Circle circ) {
380
+
381
+ int txy1[2];
382
+
383
+ int txy2[2];
384
+
385
+ int trad;
386
+
387
+ double d;
388
+
389
+
390
+
391
+ trad = circ.getRadius() + this->getRadius(); //2つの半径の和
392
+
393
+ circ.getPosition(txy1);
394
+
395
+ this->getPosition(txy2);
396
+
397
+ //中心の距離
398
+
399
+ d = sqrt((txy1[0] - txy2[0]) * (txy1[0] - txy2[0]) + (txy1[1] - txy2[1]) * ((txy1[1] - txy2[1])));
400
+
401
+
402
+
403
+ if (d > trad) {
404
+
405
+ return false;
406
+
407
+ }
408
+
409
+ else {
410
+
411
+ return true;
412
+
413
+ }
414
+
415
+
416
+
417
+ }*/
418
+
419
+ `````````
420
+
421
+ ```
422
+
423
+ //svg.h
424
+
425
+ #pragma once
426
+
427
+ #include<fstream>
428
+
429
+ #include<string>
430
+
431
+ using namespace std;
432
+
433
+
434
+
435
+ class svg {
436
+
437
+ private:
438
+
439
+ ofstream ofs;
440
+
441
+ public:
442
+
443
+ void open(string filename, int width, int height);
444
+
445
+ void close();
446
+
447
+ void drawCircle(int x, int y, int rad, string color = "black", int width = 0);
448
+
449
+
450
+
451
+ };
452
+
453
+
454
+
455
+
456
+
457
+ `````````
458
+
459
+ ```
460
+
461
+ //svg.cpp
462
+
463
+ #include"svg.h"
464
+
465
+
466
+
467
+ void svg::open(string filename, int width, int height) {
468
+
469
+ ofs.open(filename);
470
+
471
+ ofs << "<svg width='" << width << "' height='" << height << "' >" << endl;
472
+
473
+ }
474
+
475
+
476
+
477
+ void svg::close() {
478
+
479
+ ofs << "</svg>";
480
+
481
+ ofs.close();
482
+
483
+ }
484
+
485
+
486
+
487
+ void svg::drawCircle(int x, int y, int rad, std::string color, int width) {
488
+
489
+ if (width > 0) // 線幅が指定されていれば輪郭線を描画する (draw only contour for non-zero width)
490
+
491
+ ofs << "<circle cx='" << x << "' cy='" << y << "' r='" << rad
492
+
493
+ << "' stroke='" << color << "' stroke-width='" << width << "' fill='none' />";
494
+
495
+ else // 線幅が指定されていないか、値が0以下であれば、円の内部を塗りつぶす (fill inside circle)
496
+
497
+ ofs << "<circle cx='" << x << "' cy='" << y << "' r='" << rad
498
+
499
+ << "' fill='" << color << "' />";
500
+
501
+ ofs << std::endl; // 改行
502
+
503
+ }
504
+
505
+ ```
506
+
257
507
 
258
508
 
259
509