質問編集履歴

5

表示ミスの修正

2016/01/22 03:33

投稿

saito.kaz
saito.kaz

スコア76

test CHANGED
File without changes
test CHANGED
@@ -20,7 +20,9 @@
20
20
 
21
21
 
22
22
 
23
+ ###発生している問題・エラーメッセージ
24
+
23
- ###発生している問題・エラーメッセージPhone.cpp:40:21: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
25
+ Phone.cpp:40:21: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
24
26
 
25
27
  Phone p2(false, "p2","noraml2");
26
28
 

4

*修正 : 回答を元に、エラー文、Workerクラスのプログラムを修正を行いました。

2016/01/22 03:33

投稿

saito.kaz
saito.kaz

スコア76

test CHANGED
File without changes
test CHANGED
@@ -6,6 +6,10 @@
6
6
 
7
7
 
8
8
 
9
+ *修正 : 回答を元に、エラー文、Workerクラスのプログラムを修正を行いました。
10
+
11
+
12
+
9
13
  ###前提・実現したいこと
10
14
 
11
15
  下記のコードでは、phoneクラスを使って、Workerクラスでコンポジションを実装しています。
@@ -16,9 +20,7 @@
16
20
 
17
21
 
18
22
 
19
- ###発生している問題・エラーメッセージ
20
-
21
- Phone.cpp:40:21: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
23
+ ###発生している問題・エラーメッセージPhone.cpp:40:21: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
22
24
 
23
25
  Phone p2(false, "p2","noraml2");
24
26
 
@@ -32,346 +34,352 @@
32
34
 
33
35
  2 warnings generated.
34
36
 
37
+ In file included from Worker.cpp:2:
38
+
39
+ ./Worker.h:8:4: error: unknown type name 'Phone'
40
+
41
+ Phone phone;
42
+
43
+ ^
44
+
45
+ Worker.cpp:16:61: error: use of undeclared identifier 'i'
46
+
47
+ Worker::Worker(int number, char* name, double salary):Phone(i,plan,pInfo){
48
+
49
+ ^
50
+
51
+ Worker.cpp:16:63: error: use of undeclared identifier 'plan'
52
+
53
+ Worker::Worker(int number, char* name, double salary):Phone(i,plan,pInfo){
54
+
55
+ ^
56
+
57
+ Worker.cpp:16:68: error: use of undeclared identifier 'pInfo'
58
+
59
+ Worker::Worker(int number, char* name, double salary):Phone(i,plan,pInfo){
60
+
61
+ ^
62
+
35
- Phone.cpp:40:21: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
63
+ Worker.cpp:32:9: error: exception specification in declaration does not match previous declaration
64
+
65
+ Worker::~Worker(){
66
+
67
+ ^
68
+
69
+ ./Worker.h:12:4: note: previous declaration is here
70
+
71
+ ~Worker();
72
+
73
+ ^
74
+
75
+ 5 errors generated.
76
+
77
+ ###ソースコード
78
+
79
+ ```
80
+
81
+ <Phone.h>
82
+
83
+ #include <string.h>
84
+
85
+
86
+
87
+ class Phone{
88
+
89
+ public:
90
+
91
+ Phone();
92
+
93
+ Phone(bool i, char* plan, std::string pInfo);
94
+
95
+ ~Phone();
96
+
97
+ bool i;
98
+
99
+ char* plan;
100
+
101
+ std::string pInfo;
102
+
103
+ void ShowPhone();
104
+
105
+ virtual void ShowData();
106
+
107
+ };
108
+
109
+
110
+
111
+
112
+
113
+
114
+
115
+ <Phone.cpp>
116
+
117
+ #include <iostream>
118
+
119
+ #include <string.h>
120
+
121
+ #include "Phone.h"
122
+
123
+
124
+
125
+ using namespace std;
126
+
127
+
128
+
129
+ Phone::Phone(){
130
+
131
+ plan = new char[100];
132
+
133
+ cout << "this is constrcuter "<< "\n";
134
+
135
+ this->i = false;
136
+
137
+ strcpy(this->plan, "normal");
138
+
139
+ this->pInfo = "nokia";
140
+
141
+ }
142
+
143
+
144
+
145
+ Phone::Phone(bool i, char* plan, string pInfo){
146
+
147
+ this->plan = new char[100];
148
+
149
+ this->i = i;
150
+
151
+ strcpy(this->plan, plan);
152
+
153
+ this->pInfo = "nokia";
154
+
155
+ }
156
+
157
+
158
+
159
+
160
+
161
+ Phone::~Phone(){
162
+
163
+ }
164
+
165
+
166
+
167
+
168
+
169
+ void Phone::ShowPhone(){
170
+
171
+ cout << " i = " << this->i << "\n";
172
+
173
+ cout << " plan = " << this->plan <<"\n";
174
+
175
+ cout << " pInfo = " << this->pInfo <<"\n";
176
+
177
+ }
178
+
179
+
180
+
181
+ void Phone::ShowData(){
182
+
183
+ cout << " This is ShowData " << "\n";
184
+
185
+ }
186
+
187
+
188
+
189
+
190
+
191
+ int main(){
192
+
193
+ Phone p1;
36
194
 
37
195
  Phone p2(false, "p2","noraml2");
38
196
 
39
- ^
40
-
41
- Phone.cpp:41:21: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
42
-
43
197
  Phone p3(false, "p3","noraml3");
44
198
 
199
+ cout << "----------p2.ShowPhone();-------------------" << "\n";
200
+
201
+ p2.ShowPhone();
202
+
203
+ cout << "----------p3.ShowPhone();-------------------" << "\n";
204
+
205
+ p3.ShowPhone();
206
+
207
+ cout << "-----------------------------" << "\n";
208
+
209
+
210
+
211
+ p1.i = false;
212
+
213
+ strcpy(p1.plan,"normal");
214
+
215
+ p1.pInfo = "nokia";
216
+
217
+ p1.ShowPhone();
218
+
219
+ cout << "-----------------------------" << "\n";
220
+
221
+ p2.ShowData();
222
+
223
+
224
+
225
+ return 0;
226
+
45
- ^
227
+ }
228
+
229
+
230
+
231
+
232
+
233
+
234
+
46
-
235
+ <Worker.h>
236
+
237
+ class Worker{
238
+
239
+ public:
240
+
241
+ int number;
242
+
243
+ char* name;
244
+
245
+ double salary;
246
+
247
+ Phone phone;
248
+
249
+ Worker();
250
+
251
+ Worker(int number, char* name, double salary);
252
+
253
+ Worker(const Worker &obj);
254
+
255
+ ~Worker();
256
+
257
+ };
258
+
259
+
260
+
261
+
262
+
263
+
264
+
265
+ #include <iostream>
266
+
267
+ #include "Worker.h"
268
+
269
+ #include "Phone.h"
270
+
271
+
272
+
273
+
274
+
47
- 2 warnings generated.
275
+ using namespace std;
276
+
277
+
278
+
48
-
279
+ Worker::Worker(){
280
+
281
+ name = new char[80];
282
+
49
- Worker.cpp:14:61: error: use of undeclared identifier 'i'
283
+ cout<< " This is Constructor " << "\n";
284
+
285
+ strcpy(name, "undifined");
286
+
287
+ number = 0;
288
+
289
+ salary = 0;
290
+
291
+ }
292
+
293
+
50
294
 
51
295
  Worker::Worker(int number, char* name, double salary):Phone(i,plan,pInfo){
52
296
 
53
- ^
54
-
55
- Worker.cpp:14:63: error: use of undeclared identifier 'plan'
56
-
57
- Worker::Worker(int number, char* name, double salary):Phone(i,plan,pInfo){
297
+ cout<< " This is Constructor called Worker(int number, char* name, double salary):Phone(bool i, char* plan, string pInfo) " << "\n";
298
+
58
-
299
+ this->number = number;
300
+
301
+ this->name = new char[100];
302
+
303
+ strcpy(this->name, name);
304
+
305
+ this->salary = salary;
306
+
59
- ^
307
+ }
308
+
309
+
310
+
60
-
311
+ Worker::Worker(const Worker &obj){
312
+
313
+ cout<< " This is Copy Constructor " << "\n";
314
+
315
+ name = new char[80];
316
+
317
+ strcpy(name,obj.name);
318
+
319
+ this->number =obj.number;
320
+
321
+ this->salary = obj.salary;
322
+
323
+ };
324
+
325
+
326
+
327
+ Worker::~Worker(){
328
+
329
+ delete[] name;
330
+
331
+ cout << "デコンストラクタ" << "\n";
332
+
333
+ }
334
+
335
+
336
+
337
+ /*
338
+
339
+ void ShowData(Worker w2){
340
+
341
+ cout << "name = " << w2.name << "\n";
342
+
343
+ strcpy(w2.name,"AVD");
344
+
345
+ cout << "name = " << w2.name << "\n";
346
+
347
+ cout << "number = " << w2.number << "\n";
348
+
349
+ cout << "salary = " << w2.salary << "\n";
350
+
351
+ }
352
+
353
+ */
354
+
355
+ int main(){
356
+
357
+ Worker w1;
358
+
359
+ strcpy(w1.name,"Takayuki");
360
+
361
+ w1.number =10;
362
+
363
+ w1.salary = 200;
364
+
61
- Worker.cpp:14:68: error: use of undeclared identifier 'pInfo'
365
+ cout << " Before shwodata" << "\n";
366
+
62
-
367
+ //ShowData(w1);
368
+
63
- Worker::Worker(int number, char* name, double salary):Phone(i,plan,pInfo){
369
+ cout << " After shwodata" << "\n";
370
+
371
+
372
+
64
-
373
+ return 0;
374
+
65
- ^
375
+ }
66
-
67
- 3 errors generated.
376
+
68
-
69
- ###ソースコード
377
+
378
+
379
+
70
380
 
71
381
  ```
72
382
 
73
- <Phone.h>
74
-
75
- #include <string.h>
76
-
77
-
78
-
79
- class Phone{
80
-
81
- public:
82
-
83
- Phone();
84
-
85
- Phone(bool i, char* plan, std::string pInfo);
86
-
87
- ~Phone();
88
-
89
- bool i;
90
-
91
- char* plan;
92
-
93
- std::string pInfo;
94
-
95
- void ShowPhone();
96
-
97
- virtual void ShowData();
98
-
99
- };
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
- <Phone.cpp>
108
-
109
- #include <iostream>
110
-
111
- #include <string.h>
112
-
113
- #include "Phone.h"
114
-
115
-
116
-
117
- using namespace std;
118
-
119
-
120
-
121
- Phone::Phone(){
122
-
123
- plan = new char[100];
124
-
125
- cout << "this is constrcuter "<< "\n";
126
-
127
- this->i = false;
128
-
129
- strcpy(this->plan, "normal");
130
-
131
- this->pInfo = "nokia";
132
-
133
- }
134
-
135
-
136
-
137
- Phone::Phone(bool i, char* plan, string pInfo){
138
-
139
- this->plan = new char[100];
140
-
141
- this->i = i;
142
-
143
- strcpy(this->plan, plan);
144
-
145
- this->pInfo = "nokia";
146
-
147
- }
148
-
149
-
150
-
151
-
152
-
153
- Phone::~Phone(){
154
-
155
- }
156
-
157
-
158
-
159
-
160
-
161
- void Phone::ShowPhone(){
162
-
163
- cout << " i = " << this->i << "\n";
164
-
165
- cout << " plan = " << this->plan <<"\n";
166
-
167
- cout << " pInfo = " << this->pInfo <<"\n";
168
-
169
- }
170
-
171
-
172
-
173
- void Phone::ShowData(){
174
-
175
- cout << " This is ShowData " << "\n";
176
-
177
- }
178
-
179
-
180
-
181
-
182
-
183
- int main(){
184
-
185
- Phone p1;
186
-
187
- Phone p2(false, "p2","noraml2");
188
-
189
- Phone p3(false, "p3","noraml3");
190
-
191
- cout << "----------p2.ShowPhone();-------------------" << "\n";
192
-
193
- p2.ShowPhone();
194
-
195
- cout << "----------p3.ShowPhone();-------------------" << "\n";
196
-
197
- p3.ShowPhone();
198
-
199
- cout << "-----------------------------" << "\n";
200
-
201
-
202
-
203
- p1.i = false;
204
-
205
- strcpy(p1.plan,"normal");
206
-
207
- p1.pInfo = "nokia";
208
-
209
- p1.ShowPhone();
210
-
211
- cout << "-----------------------------" << "\n";
212
-
213
- p2.ShowData();
214
-
215
-
216
-
217
- return 0;
218
-
219
- }
220
-
221
-
222
-
223
-
224
-
225
-
226
-
227
- <Worker.h>
228
-
229
- class Worker{
230
-
231
- public:
232
-
233
- int number;
234
-
235
- char* name;
236
-
237
- double salary;
238
-
239
- Phone phone;
240
-
241
- Worker();
242
-
243
- Worker(int number, char* name, double salary);
244
-
245
- Worker(const Worker &obj);
246
-
247
- ~Worker();
248
-
249
- };
250
-
251
-
252
-
253
-
254
-
255
-
256
-
257
-
258
-
259
- <Worker.cpp>
260
-
261
- #include <iostream>
262
-
263
- #include "Worker.h"
264
-
265
- #include "Phone.h"
266
-
267
-
268
-
269
- using namespace std;
270
-
271
-
272
-
273
- Worker::Worker(){
274
-
275
- name = new char[80];
276
-
277
- cout<< " This is Constructor " << "\n";
278
-
279
- strcpy(name, "undifined");
280
-
281
- number = 0;
282
-
283
- salary = 0;
284
-
285
- }
286
-
287
-
288
-
289
- Worker::Worker(int number, char* name, double salary):Phone(i,plan,pInfo){
290
-
291
- cout<< " This is Constructor called Worker(int number, char* name, double salary):Phone(bool i, char* plan, string pInfo) " << "\n";
292
-
293
- this->number = number;
294
-
295
- this->name = new char[100];
296
-
297
- strcpy(this->name, name);
298
-
299
- this->salary = salary;
300
-
301
- }
302
-
303
-
304
-
305
- Worker::Worker(const Worker &obj){
306
-
307
- cout<< " This is Copy Constructor " << "\n";
308
-
309
- name = new char[80];
310
-
311
- strcpy(name,obj.name);
312
-
313
- this->number =obj.number;
314
-
315
- this->salary = obj.salary;
316
-
317
- };
318
-
319
-
320
-
321
- Worker::~Worker(){
322
-
323
- delete[] name;
324
-
325
- cout << "デコンストラクタ" << "\n";
326
-
327
- }
328
-
329
-
330
-
331
- /*
332
-
333
- void ShowData(Worker w2){
334
-
335
- cout << "name = " << w2.name << "\n";
336
-
337
- strcpy(w2.name,"AVD");
338
-
339
- cout << "name = " << w2.name << "\n";
340
-
341
- cout << "number = " << w2.number << "\n";
342
-
343
- cout << "salary = " << w2.salary << "\n";
344
-
345
- }
346
-
347
- */
348
-
349
- int main(){
350
-
351
- Worker w1;
352
-
353
- strcpy(w1.name,"Takayuki");
354
-
355
- w1.number =10;
356
-
357
- w1.salary = 200;
358
-
359
- cout << " Before shwodata" << "\n";
360
-
361
- //ShowData(w1);
362
-
363
- cout << " After shwodata" << "\n";
364
-
365
-
366
-
367
- return 0;
368
-
369
- }
370
-
371
-
372
-
373
- ```
374
-
375
383
 
376
384
 
377
385
  ###補足情報(言語/FW/ツール等のバージョンなど)

3

Worker コンストラクタにWorker::を追加し、それに伴うエラー変更を追加

2016/01/22 03:30

投稿

saito.kaz
saito.kaz

スコア76

test CHANGED
File without changes
test CHANGED
@@ -32,334 +32,346 @@
32
32
 
33
33
  2 warnings generated.
34
34
 
35
+ Phone.cpp:40:21: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
36
+
35
- In file included from Worker.cpp:3:
37
+ Phone p2(false, "p2","noraml2");
38
+
36
-
39
+ ^
40
+
41
+ Phone.cpp:41:21: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
42
+
43
+ Phone p3(false, "p3","noraml3");
44
+
45
+ ^
46
+
47
+ 2 warnings generated.
48
+
49
+ Worker.cpp:14:61: error: use of undeclared identifier 'i'
50
+
51
+ Worker::Worker(int number, char* name, double salary):Phone(i,plan,pInfo){
52
+
53
+ ^
54
+
37
- ./Phone.h:3:7: error: redefinition of 'Phone'
55
+ Worker.cpp:14:63: error: use of undeclared identifier 'plan'
56
+
57
+ Worker::Worker(int number, char* name, double salary):Phone(i,plan,pInfo){
58
+
59
+ ^
60
+
61
+ Worker.cpp:14:68: error: use of undeclared identifier 'pInfo'
62
+
63
+ Worker::Worker(int number, char* name, double salary):Phone(i,plan,pInfo){
64
+
65
+ ^
66
+
67
+ 3 errors generated.
68
+
69
+ ###ソースコード
70
+
71
+ ```
72
+
73
+ <Phone.h>
74
+
75
+ #include <string.h>
76
+
77
+
38
78
 
39
79
  class Phone{
40
80
 
41
- ^
42
-
43
- ./Phone.h:3:7: note: previous definition is here
44
-
45
- class Phone{
46
-
47
- ^
48
-
49
- Worker.cpp:7:18: error: type 'Phone' is not a direct or virtual base of 'Worker'
50
-
51
- Worker::Worker():Phone(){
52
-
53
- ^~~~~
54
-
55
- 2 errors generated.
56
-
57
- ###ソースコード
81
+ public:
82
+
83
+ Phone();
84
+
85
+ Phone(bool i, char* plan, std::string pInfo);
86
+
87
+ ~Phone();
88
+
89
+ bool i;
90
+
91
+ char* plan;
92
+
93
+ std::string pInfo;
94
+
95
+ void ShowPhone();
96
+
97
+ virtual void ShowData();
98
+
99
+ };
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+ <Phone.cpp>
108
+
109
+ #include <iostream>
110
+
111
+ #include <string.h>
112
+
113
+ #include "Phone.h"
114
+
115
+
116
+
117
+ using namespace std;
118
+
119
+
120
+
121
+ Phone::Phone(){
122
+
123
+ plan = new char[100];
124
+
125
+ cout << "this is constrcuter "<< "\n";
126
+
127
+ this->i = false;
128
+
129
+ strcpy(this->plan, "normal");
130
+
131
+ this->pInfo = "nokia";
132
+
133
+ }
134
+
135
+
136
+
137
+ Phone::Phone(bool i, char* plan, string pInfo){
138
+
139
+ this->plan = new char[100];
140
+
141
+ this->i = i;
142
+
143
+ strcpy(this->plan, plan);
144
+
145
+ this->pInfo = "nokia";
146
+
147
+ }
148
+
149
+
150
+
151
+
152
+
153
+ Phone::~Phone(){
154
+
155
+ }
156
+
157
+
158
+
159
+
160
+
161
+ void Phone::ShowPhone(){
162
+
163
+ cout << " i = " << this->i << "\n";
164
+
165
+ cout << " plan = " << this->plan <<"\n";
166
+
167
+ cout << " pInfo = " << this->pInfo <<"\n";
168
+
169
+ }
170
+
171
+
172
+
173
+ void Phone::ShowData(){
174
+
175
+ cout << " This is ShowData " << "\n";
176
+
177
+ }
178
+
179
+
180
+
181
+
182
+
183
+ int main(){
184
+
185
+ Phone p1;
186
+
187
+ Phone p2(false, "p2","noraml2");
188
+
189
+ Phone p3(false, "p3","noraml3");
190
+
191
+ cout << "----------p2.ShowPhone();-------------------" << "\n";
192
+
193
+ p2.ShowPhone();
194
+
195
+ cout << "----------p3.ShowPhone();-------------------" << "\n";
196
+
197
+ p3.ShowPhone();
198
+
199
+ cout << "-----------------------------" << "\n";
200
+
201
+
202
+
203
+ p1.i = false;
204
+
205
+ strcpy(p1.plan,"normal");
206
+
207
+ p1.pInfo = "nokia";
208
+
209
+ p1.ShowPhone();
210
+
211
+ cout << "-----------------------------" << "\n";
212
+
213
+ p2.ShowData();
214
+
215
+
216
+
217
+ return 0;
218
+
219
+ }
220
+
221
+
222
+
223
+
224
+
225
+
226
+
227
+ <Worker.h>
228
+
229
+ class Worker{
230
+
231
+ public:
232
+
233
+ int number;
234
+
235
+ char* name;
236
+
237
+ double salary;
238
+
239
+ Phone phone;
240
+
241
+ Worker();
242
+
243
+ Worker(int number, char* name, double salary);
244
+
245
+ Worker(const Worker &obj);
246
+
247
+ ~Worker();
248
+
249
+ };
250
+
251
+
252
+
253
+
254
+
255
+
256
+
257
+
258
+
259
+ <Worker.cpp>
260
+
261
+ #include <iostream>
262
+
263
+ #include "Worker.h"
264
+
265
+ #include "Phone.h"
266
+
267
+
268
+
269
+ using namespace std;
270
+
271
+
272
+
273
+ Worker::Worker(){
274
+
275
+ name = new char[80];
276
+
277
+ cout<< " This is Constructor " << "\n";
278
+
279
+ strcpy(name, "undifined");
280
+
281
+ number = 0;
282
+
283
+ salary = 0;
284
+
285
+ }
286
+
287
+
288
+
289
+ Worker::Worker(int number, char* name, double salary):Phone(i,plan,pInfo){
290
+
291
+ cout<< " This is Constructor called Worker(int number, char* name, double salary):Phone(bool i, char* plan, string pInfo) " << "\n";
292
+
293
+ this->number = number;
294
+
295
+ this->name = new char[100];
296
+
297
+ strcpy(this->name, name);
298
+
299
+ this->salary = salary;
300
+
301
+ }
302
+
303
+
304
+
305
+ Worker::Worker(const Worker &obj){
306
+
307
+ cout<< " This is Copy Constructor " << "\n";
308
+
309
+ name = new char[80];
310
+
311
+ strcpy(name,obj.name);
312
+
313
+ this->number =obj.number;
314
+
315
+ this->salary = obj.salary;
316
+
317
+ };
318
+
319
+
320
+
321
+ Worker::~Worker(){
322
+
323
+ delete[] name;
324
+
325
+ cout << "デコンストラクタ" << "\n";
326
+
327
+ }
328
+
329
+
330
+
331
+ /*
332
+
333
+ void ShowData(Worker w2){
334
+
335
+ cout << "name = " << w2.name << "\n";
336
+
337
+ strcpy(w2.name,"AVD");
338
+
339
+ cout << "name = " << w2.name << "\n";
340
+
341
+ cout << "number = " << w2.number << "\n";
342
+
343
+ cout << "salary = " << w2.salary << "\n";
344
+
345
+ }
346
+
347
+ */
348
+
349
+ int main(){
350
+
351
+ Worker w1;
352
+
353
+ strcpy(w1.name,"Takayuki");
354
+
355
+ w1.number =10;
356
+
357
+ w1.salary = 200;
358
+
359
+ cout << " Before shwodata" << "\n";
360
+
361
+ //ShowData(w1);
362
+
363
+ cout << " After shwodata" << "\n";
364
+
365
+
366
+
367
+ return 0;
368
+
369
+ }
370
+
371
+
58
372
 
59
373
  ```
60
374
 
61
- <Phone.h>
62
-
63
- #include <string.h>
64
-
65
-
66
-
67
- class Phone{
68
-
69
- public:
70
-
71
- Phone();
72
-
73
- Phone(bool i, char* plan, std::string pInfo);
74
-
75
- ~Phone();
76
-
77
- bool i;
78
-
79
- char* plan;
80
-
81
- std::string pInfo;
82
-
83
- void ShowPhone();
84
-
85
- virtual void ShowData();
86
-
87
- };
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
- <Phone.cpp>
96
-
97
- #include <iostream>
98
-
99
- #include <string.h>
100
-
101
- #include "Phone.h"
102
-
103
-
104
-
105
- using namespace std;
106
-
107
-
108
-
109
- Phone::Phone(){
110
-
111
- plan = new char[100];
112
-
113
- cout << "this is constrcuter "<< "\n";
114
-
115
- this->i = false;
116
-
117
- strcpy(this->plan, "normal");
118
-
119
- this->pInfo = "nokia";
120
-
121
- }
122
-
123
-
124
-
125
- Phone::Phone(bool i, char* plan, string pInfo){
126
-
127
- this->plan = new char[100];
128
-
129
- this->i = i;
130
-
131
- strcpy(this->plan, plan);
132
-
133
- this->pInfo = "nokia";
134
-
135
- }
136
-
137
-
138
-
139
-
140
-
141
- Phone::~Phone(){
142
-
143
- }
144
-
145
-
146
-
147
-
148
-
149
- void Phone::ShowPhone(){
150
-
151
- cout << " i = " << this->i << "\n";
152
-
153
- cout << " plan = " << this->plan <<"\n";
154
-
155
- cout << " pInfo = " << this->pInfo <<"\n";
156
-
157
- }
158
-
159
-
160
-
161
- void Phone::ShowData(){
162
-
163
- cout << " This is ShowData " << "\n";
164
-
165
- }
166
-
167
-
168
-
169
-
170
-
171
- int main(){
172
-
173
- Phone p1;
174
-
175
- Phone p2(false, "p2","noraml2");
176
-
177
- Phone p3(false, "p3","noraml3");
178
-
179
- cout << "----------p2.ShowPhone();-------------------" << "\n";
180
-
181
- p2.ShowPhone();
182
-
183
- cout << "----------p3.ShowPhone();-------------------" << "\n";
184
-
185
- p3.ShowPhone();
186
-
187
- cout << "-----------------------------" << "\n";
188
-
189
-
190
-
191
- p1.i = false;
192
-
193
- strcpy(p1.plan,"normal");
194
-
195
- p1.pInfo = "nokia";
196
-
197
- p1.ShowPhone();
198
-
199
- cout << "-----------------------------" << "\n";
200
-
201
- p2.ShowData();
202
-
203
-
204
-
205
- return 0;
206
-
207
- }
208
-
209
-
210
-
211
-
212
-
213
-
214
-
215
- <Worker.h>
216
-
217
- class Worker{
218
-
219
- public:
220
-
221
- int number;
222
-
223
- char* name;
224
-
225
- double salary;
226
-
227
- Phone phone;
228
-
229
- Worker();
230
-
231
- Worker(int number, char* name, double salary);
232
-
233
- Worker(const Worker &obj);
234
-
235
- ~Worker();
236
-
237
- };
238
-
239
-
240
-
241
-
242
-
243
-
244
-
245
-
246
-
247
- <Worker.cpp>
248
-
249
- #include <iostream>
250
-
251
- #include "Worker.h"
252
-
253
- #include "Phone.h"
254
-
255
-
256
-
257
- using namespace std;
258
-
259
-
260
-
261
- Worker::Worker(){
262
-
263
- name = new char[80];
264
-
265
- cout<< " This is Constructor " << "\n";
266
-
267
- strcpy(name, "undifined");
268
-
269
- number = 0;
270
-
271
- salary = 0;
272
-
273
- }
274
-
275
-
276
-
277
- Worker(int number, char* name, double salary):Phone(i,plan,pInfo){
278
-
279
- cout<< " This is Constructor called Worker(int number, char* name, double salary):Phone(bool i, char* plan, string pInfo) " << "\n";
280
-
281
- this->number = number;
282
-
283
- this->name = new char[100];
284
-
285
- strcpy(this->name, name);
286
-
287
- this->salary = salary;
288
-
289
- }
290
-
291
-
292
-
293
- Worker::Worker(const Worker &obj){
294
-
295
- cout<< " This is Copy Constructor " << "\n";
296
-
297
- name = new char[80];
298
-
299
- strcpy(name,obj.name);
300
-
301
- this->number =obj.number;
302
-
303
- this->salary = obj.salary;
304
-
305
- };
306
-
307
-
308
-
309
- Worker::~Worker(){
310
-
311
- delete[] name;
312
-
313
- cout << "デコンストラクタ" << "\n";
314
-
315
- }
316
-
317
-
318
-
319
- /*
320
-
321
- void ShowData(Worker w2){
322
-
323
- cout << "name = " << w2.name << "\n";
324
-
325
- strcpy(w2.name,"AVD");
326
-
327
- cout << "name = " << w2.name << "\n";
328
-
329
- cout << "number = " << w2.number << "\n";
330
-
331
- cout << "salary = " << w2.salary << "\n";
332
-
333
- }
334
-
335
- */
336
-
337
- int main(){
338
-
339
- Worker w1;
340
-
341
- strcpy(w1.name,"Takayuki");
342
-
343
- w1.number =10;
344
-
345
- w1.salary = 200;
346
-
347
- cout << " Before shwodata" << "\n";
348
-
349
- //ShowData(w1);
350
-
351
- cout << " After shwodata" << "\n";
352
-
353
-
354
-
355
- return 0;
356
-
357
- }
358
-
359
-
360
-
361
- ```
362
-
363
375
 
364
376
 
365
377
  ###補足情報(言語/FW/ツール等のバージョンなど)

2

Workerコンストラクタにthis->name = new char\[100\]; strcpy\(this->name, name\);を追加。

2016/01/22 02:22

投稿

saito.kaz
saito.kaz

スコア76

test CHANGED
File without changes
test CHANGED
@@ -282,6 +282,8 @@
282
282
 
283
283
  this->name = new char[100];
284
284
 
285
+ strcpy(this->name, name);
286
+
285
287
  this->salary = salary;
286
288
 
287
289
  }

1

Worker\.cppの仮想関数の設定を変更しました。

2016/01/22 02:15

投稿

saito.kaz
saito.kaz

スコア76

test CHANGED
@@ -1 +1 @@
1
- C++ 継承とイシャライザについて
1
+ C++ コンポジション、仮想関数とイシャライザについて
test CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  C++の学習を初めて1週間なので、質問が多くて申し訳ありません。
4
4
 
5
+ 質問なのですが、コンポジションの場合、仮想関数をオーバーライドすることはできないのでしょうか。
6
+
5
7
 
6
8
 
7
9
  ###前提・実現したいこと
@@ -84,6 +86,12 @@
84
86
 
85
87
  };
86
88
 
89
+
90
+
91
+
92
+
93
+
94
+
87
95
  <Phone.cpp>
88
96
 
89
97
  #include <iostream>
@@ -200,83 +208,151 @@
200
208
 
201
209
 
202
210
 
211
+
212
+
213
+
214
+
203
215
  <Worker.h>
204
216
 
217
+ class Worker{
218
+
219
+ public:
220
+
221
+ int number;
222
+
223
+ char* name;
224
+
225
+ double salary;
226
+
227
+ Phone phone;
228
+
229
+ Worker();
230
+
231
+ Worker(int number, char* name, double salary);
232
+
233
+ Worker(const Worker &obj);
234
+
235
+ ~Worker();
236
+
237
+ };
238
+
239
+
240
+
241
+
242
+
243
+
244
+
245
+
246
+
247
+ <Worker.cpp>
248
+
205
- #include <string.h>
249
+ #include <iostream>
250
+
251
+ #include "Worker.h"
206
252
 
207
253
  #include "Phone.h"
208
254
 
209
255
 
210
256
 
257
+ using namespace std;
258
+
259
+
260
+
211
- class Worker{
261
+ Worker::Worker(){
212
-
262
+
213
- public:
263
+ name = new char[80];
264
+
214
-
265
+ cout<< " This is Constructor " << "\n";
266
+
267
+ strcpy(name, "undifined");
268
+
215
- int number;
269
+ number = 0;
216
-
270
+
217
- char* name;
271
+ salary = 0;
218
-
219
- double salary;
272
+
220
-
221
- Phone phone;
222
-
223
- Worker();
273
+ }
224
-
274
+
275
+
276
+
225
- Worker(int number, char* name, double salary);
277
+ Worker(int number, char* name, double salary):Phone(i,plan,pInfo){
278
+
226
-
279
+ cout<< " This is Constructor called Worker(int number, char* name, double salary):Phone(bool i, char* plan, string pInfo) " << "\n";
280
+
281
+ this->number = number;
282
+
283
+ this->name = new char[100];
284
+
285
+ this->salary = salary;
286
+
287
+ }
288
+
289
+
290
+
227
- Worker(const Worker &obj);
291
+ Worker::Worker(const Worker &obj){
292
+
228
-
293
+ cout<< " This is Copy Constructor " << "\n";
294
+
295
+ name = new char[80];
296
+
229
- virtual void ShowDate();
297
+ strcpy(name,obj.name);
230
-
298
+
231
- ~Worker();
299
+ this->number =obj.number;
300
+
301
+ this->salary = obj.salary;
232
302
 
233
303
  };
234
304
 
235
- <Worker.cpp>
305
+
236
-
237
- #include <iostream>
306
+
238
-
239
- #include "Worker.h"
240
-
241
- #include "Phone.h"
242
-
243
-
244
-
245
- using namespace std;
246
-
247
-
248
-
249
- Worker::Worker():Phone(){
307
+ Worker::~Worker(){
250
-
308
+
251
- name = new char[80];
309
+ delete[] name;
252
-
310
+
253
- cout<< " This is Constructor " << "\n";
311
+ cout << "デコンストラクタ" << "\n";
254
-
255
- strcpy(name, "undifined");
312
+
256
-
257
- number = 0;
258
-
259
- salary = 0;
260
-
261
- }
313
+ }
314
+
315
+
316
+
262
-
317
+ /*
263
-
264
-
318
+
265
- Worker::Worker(const Worker &obj){
319
+ void ShowData(Worker w2){
320
+
266
-
321
+ cout << "name = " << w2.name << "\n";
322
+
323
+ strcpy(w2.name,"AVD");
324
+
325
+ cout << "name = " << w2.name << "\n";
326
+
327
+ cout << "number = " << w2.number << "\n";
328
+
329
+ cout << "salary = " << w2.salary << "\n";
330
+
331
+ }
332
+
333
+ */
334
+
335
+ int main(){
336
+
337
+ Worker w1;
338
+
339
+ strcpy(w1.name,"Takayuki");
340
+
341
+ w1.number =10;
342
+
343
+ w1.salary = 200;
344
+
267
- cout<< " This is Copy Constructor " << "\n";
345
+ cout << " Before shwodata" << "\n";
268
-
269
- name = new char[80];
346
+
270
-
271
- strcpy(name,obj.name);
347
+ //ShowData(w1);
272
-
273
- this->number =obj.number;
348
+
274
-
275
- this->salary = obj.salary;
349
+ cout << " After shwodata" << "\n";
350
+
351
+
352
+
276
-
353
+ return 0;
277
-
278
-
354
+
279
- };
355
+ }
280
356
 
281
357
 
282
358