回答編集履歴

4

ソース修正

2018/10/11 09:42

投稿

cateye
cateye

スコア6851

test CHANGED
@@ -252,6 +252,52 @@
252
252
 
253
253
  ```c++
254
254
 
255
+ #include <iostream>
256
+
257
+ #include <string>
258
+
259
+ #include <iomanip>
260
+
261
+
262
+
263
+ using namespace std;
264
+
265
+
266
+
267
+ class Person
268
+
269
+ {
270
+
271
+ private:
272
+
273
+ string name;
274
+
275
+
276
+
277
+ public:
278
+
279
+ Person(){}
280
+
281
+
282
+
283
+ Person(string n) { setName(n); }
284
+
285
+
286
+
287
+ Person(const string &pName) { setName(pName); }
288
+
289
+
290
+
291
+ void setName(const string &pName) { name = pName; }
292
+
293
+
294
+
295
+ string getName() const { return name; }
296
+
297
+ };
298
+
299
+
300
+
255
301
  class Publication
256
302
 
257
303
  {
@@ -276,11 +322,7 @@
276
322
 
277
323
  Publication()
278
324
 
279
- : pubNumber(0), title("War and Peace"
325
+ : pubNumber(0)
280
-
281
- "Object-Oriented Programming"
282
-
283
- "Grapes of Wrath")
284
326
 
285
327
  {
286
328
 
@@ -300,7 +342,7 @@
300
342
 
301
343
  }
302
344
 
303
- Publication(string &t)
345
+ Publication(const string &t)
304
346
 
305
347
  {
306
348
 
@@ -334,4 +376,84 @@
334
376
 
335
377
 
336
378
 
379
+ int Publication::count = 0;
380
+
381
+
382
+
383
+ int main()
384
+
385
+ {
386
+
387
+ Publication pr[3]=
388
+
389
+ {
390
+
391
+ string("War and Peace"),
392
+
393
+ string("Object-Oriented"),
394
+
395
+ string("Programming")
396
+
397
+ };
398
+
399
+ //
400
+
401
+ Person person;
402
+
403
+ //
404
+
405
+ person.setName("Lady gaga");
406
+
407
+ pr[0].setBorrower(person);
408
+
409
+ //
410
+
411
+ person.setName("Albert Einstein");
412
+
413
+ pr[1].setBorrower(person);
414
+
415
+ //
416
+
417
+ person.setName("Muhammed Ali");
418
+
419
+ pr[2].setBorrower(person);
420
+
421
+
422
+
423
+ cout << "Publication Number" << setw(5) << "Title" << setw(15)
424
+
425
+ << "Borrower Name" << endl;
426
+
427
+
428
+
429
+ cout << "------------------" << setw(5) << "-----" << setw(15)
430
+
431
+ << "-------------" << endl;
432
+
433
+
434
+
435
+ for( int i= 0; i < 3; i++ ){
436
+
437
+ pr[i].display();
438
+
439
+ }
440
+
441
+
442
+
443
+ return 0;
444
+
445
+ }
446
+
447
+
448
+
449
+ void Publication::display()
450
+
451
+ {
452
+
453
+ cout << title << " " << loaner.getName() << endl << endl;
454
+
455
+ // cout << "count:" << count << endl;
456
+
457
+ }
458
+
337
459
  ```

3

追記

2018/10/11 09:42

投稿

cateye
cateye

スコア6851

test CHANGED
@@ -1,6 +1,6 @@
1
1
  とりあえず、エラーとワーニングは取ってあります。d^^
2
2
 
3
-
3
+ [main()をいじった](https://zawazawa.jp/it0u32xynouu070v/topic/29)
4
4
 
5
5
  ```c++
6
6
 

2

追記

2018/10/11 03:25

投稿

cateye
cateye

スコア6851

test CHANGED
@@ -225,3 +225,113 @@
225
225
 
226
226
 
227
227
  Publicationの数をカウントするならコンストラクタでカウントアップ、デストラクタでカウントダウンです。main()では参照するだけですね。
228
+
229
+ //
230
+
231
+ >下記の様なコードは違いますでしょうか?
232
+
233
+ Publication (int pub) {
234
+
235
+ pubNumber = pub; count++;
236
+
237
+ }
238
+
239
+
240
+
241
+ int getPubNumber() const {
242
+
243
+ return pubNumber;
244
+
245
+ }
246
+
247
+ ちょっとはしょちゃいましたが、わかりますか?
248
+
249
+ コンストラクタでの共通部分はprivate関数にしてコンストラクタから呼び出したほうが分かりやすいです。
250
+
251
+
252
+
253
+ ```c++
254
+
255
+ class Publication
256
+
257
+ {
258
+
259
+ private:
260
+
261
+ static int count;
262
+
263
+ int pubNumber;
264
+
265
+ string title;
266
+
267
+ Person loaner;
268
+
269
+ // Person();
270
+
271
+
272
+
273
+ public:
274
+
275
+ // コンストラクタ
276
+
277
+ Publication()
278
+
279
+ : pubNumber(0), title("War and Peace"
280
+
281
+ "Object-Oriented Programming"
282
+
283
+ "Grapes of Wrath")
284
+
285
+ {
286
+
287
+ count++;
288
+
289
+ }
290
+
291
+
292
+
293
+ Publication(int pub)
294
+
295
+ {
296
+
297
+ pubNumber = pub;
298
+
299
+ count++;
300
+
301
+ }
302
+
303
+ Publication(string &t)
304
+
305
+ {
306
+
307
+ title = t;
308
+
309
+ count++;
310
+
311
+ }
312
+
313
+ // デストラクタ
314
+
315
+ ~Publication() { count--; }
316
+
317
+ //
318
+
319
+ int getPubNumber() const { return pubNumber; }
320
+
321
+
322
+
323
+ void setBorrower(Person p) { loaner = p; }
324
+
325
+
326
+
327
+ Person getBorrower() const { return loaner; }
328
+
329
+
330
+
331
+ void display();
332
+
333
+ };
334
+
335
+
336
+
337
+ ```

1

追記

2018/10/11 00:35

投稿

cateye
cateye

スコア6851

test CHANGED
@@ -217,3 +217,11 @@
217
217
 
218
218
 
219
219
  ```
220
+
221
+ 「追記」
222
+
223
+ > main関数でcoutするのか
224
+
225
+
226
+
227
+ Publicationの数をカウントするならコンストラクタでカウントアップ、デストラクタでカウントダウンです。main()では参照するだけですね。