回答編集履歴
1
追記
answer
CHANGED
@@ -42,4 +42,48 @@
|
|
42
42
|
2+3= 6
|
43
43
|
last result = 6
|
44
44
|
*/
|
45
|
+
```
|
46
|
+
|
47
|
+
[追記] こゆこと?
|
48
|
+
|
49
|
+
```C++
|
50
|
+
#include <iostream>
|
51
|
+
|
52
|
+
class Operation {
|
53
|
+
int last_;
|
54
|
+
public:
|
55
|
+
int add(int x, int y) { return last_ = x+y; }
|
56
|
+
int sub(int x, int y) { return last_ = x-y; }
|
57
|
+
int mul(int x, int y) { return last_ = x*y; }
|
58
|
+
int last() const { return last_; }
|
59
|
+
};
|
60
|
+
|
61
|
+
class Foo {
|
62
|
+
Operation* opr_;
|
63
|
+
public:
|
64
|
+
void set(Operation* opr) {
|
65
|
+
opr_ = opr;
|
66
|
+
}
|
67
|
+
int exec_add(int x, int y) {
|
68
|
+
return opr_->add(x, y);
|
69
|
+
}
|
70
|
+
};
|
71
|
+
|
72
|
+
int main() {
|
73
|
+
using namespace std;
|
74
|
+
|
75
|
+
Operation op;
|
76
|
+
Foo foo;
|
77
|
+
|
78
|
+
foo.set(&op);
|
79
|
+
cout << "1+2= " << foo.exec_add(1,2) << endl;
|
80
|
+
cout << "2+3= " << foo.exec_add(2,3) << endl;
|
81
|
+
cout << "last result = " << op.last() << endl;
|
82
|
+
}
|
83
|
+
|
84
|
+
/* 実行結果
|
85
|
+
1+2= 3
|
86
|
+
2+3= 5
|
87
|
+
last result = 5
|
88
|
+
*/
|
45
89
|
```
|