C++
1int cat::getSum(void) {
2 return a+b;
3}
cat::dog()で変数cat::sumを計算して、それを返す設計の方が良いのかもしれませんが。
...catクラスにメンバ関数dogがあるのは突っ込んじゃいけないですよね。
覚えていました。
C++
1#include <cstdlib>
2#include <iostream>
3#include <string>
4#include <vector>
5
6namespace py_like_io {
7 template<typename T>
8 T input(const std::string& message) {
9 T ret;
10 std::cout << message;
11 std::cin >> ret;
12 return ret;
13 }
14
15 template<typename T>
16 void print(const T& arg) {
17 std::cout << arg << std::endl;
18 }
19 template<typename First, typename... Rest>
20 void print(const First& first, const Rest&... rest) {
21 std::cout << first << " ";
22 print(rest...);
23 }
24}
25
26namespace mol {
27
28 class Hoge {
29 private:
30 const int hogeA_;
31 const int hogeB_;
32
33 public:
34 Hoge(void)
35 : hogeA_(py_like_io::input<int>("Input a: ")),
36 hogeB_(py_like_io::input<int>("Input b: ")) {
37 }
38 Hoge(int a, int b)
39 : hogeA_(a), hogeB_(b) {
40 }
41
42 inline int getHogeA(void) const noexcept { return hogeA_; }
43 inline int getHogeB(void) const noexcept { return hogeB_; }
44
45 void printInfo(void) const {
46 py_like_io::print(getHogeA(), getHogeB());
47 }
48 };
49}
50
51int main(int argc, char** argv)
52{
53 int count = py_like_io::input<int>("回数 => ");
54 std::vector<mol::Hoge> hogeVec;
55 hogeVec.reserve(count);
56
57 for(int i = 0; i < count; i++) {
58 py_like_io::print("Set params", i, "th Hoge.");
59 hogeVec.emplace_back(mol::Hoge());
60 }
61
62 int sumA = 0, sumB = 0;
63 for(const auto& hoge: hogeVec) {
64 sumA += hoge.getHogeA();
65 sumB += hoge.getHogeB();
66 }
67 py_like_io::print("sumA is", sumA);
68 py_like_io::print("sumB is", sumB);
69
70 return EXIT_SUCCESS;
71}
実行結果
回数 => 3
Set params 0 th Hoge.
Input a: 1
Input b: 2
Set params 1 th Hoge.
Input a: 4
Input b: 3
Set params 2 th Hoge.
Input a: 7
Input b: 8
sumA is 12
sumB is 13
なんかもう好き放題やっています。読み捨ててください。
最近Pythonをよくいじるので、Python-likeなIO関数も勝手に追加。
わからない部分は聞いてください。
また、すごい人にはご批判をいただけるとこちらも勉強になります。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/03 05:38
2017/09/03 05:39
2017/09/03 05:43
2017/09/03 05:48
2017/09/03 05:57
2017/09/03 06:05
2017/09/03 13:35
2017/09/03 14:17
2017/09/03 14:20
2017/09/03 14:22
2017/09/03 14:24