{"id":2901,"date":"2019-01-16T18:53:17","date_gmt":"2019-01-16T09:53:17","guid":{"rendered":"https:\/\/www.chihayafuru.jp\/tech\/?p=2901"},"modified":"2019-01-20T18:15:35","modified_gmt":"2019-01-20T09:15:35","slug":"cpp-%e6%bc%94%e7%ae%97%e5%ad%90%e3%82%aa%e3%83%bc%e3%83%90%e3%83%bc%e3%83%ad%e3%83%bc%e3%83%89%e3%81%ae%e7%b3%96%e8%a1%a3%e6%a7%8b%e6%96%87%e7%9a%84%e8%a7%a3%e9%87%88%e3%81%a8%e3%83%95%e3%83%ac","status":"publish","type":"post","link":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/archives\/2901","title":{"rendered":"[CPP] \u6f14\u7b97\u5b50\u30aa\u30fc\u30d0\u30fc\u30ed\u30fc\u30c9\u306e\u7cd6\u8863\u69cb\u6587\u7684\u89e3\u91c8\u3068\u30d5\u30ec\u30f3\u30c9\u95a2\u6570\u306b\u3088\u308b\u89e3\u6c7a"},"content":{"rendered":"<p>\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e C++ \u306b\u304a\u3051\u308b\u6f14\u7b97\u5b50\u30aa\u30fc\u30d0\u30fc\u30ed\u30fc\u30c9\u306e\u7cd6\u8863\u69cb\u6587\u7684\u306a\u89e3\u91c8\u3068\u3001\u30d5\u30ec\u30f3\u30c9\u95a2\u6570\u306b\u3088\u308b\u89e3\u6c7a<\/p>\n<h2>1. \u6f14\u7b97\u5b50\u30aa\u30fc\u30d0\u30fc\u30ed\u30fc\u30c9\u306e\u7cd6\u8863\u69cb\u6587\u7684\u306a\u89e3\u91c8<\/h2>\n<pre class=\"lang:c++ decode:true \" >#include &lt;iostream&gt;\r\nusing namespace std;\r\n\r\nclass coord {\r\n\tint x, y;\r\npublic:\r\n\tcoord() : x(0), y(0) {}\r\n\tcoord(int i, int j) : x(i), y(j) {}\r\n\tvoid show() { cout &lt;&lt; \"x = \" &lt;&lt; x &lt;&lt; \", y = \" &lt;&lt; y &lt;&lt; endl; }\r\n\tcoord operator+(coord position);\r\n\tcoord add(coord position);\r\n};\r\n\r\ncoord coord::operator+(coord position)\r\n{\r\n\tcoord temp;\r\n\r\n\ttemp.x = this-&gt;x + position.x;\r\n\ttemp.y = this-&gt;y + position.y;\r\n\r\n\treturn temp;\r\n}\r\n\r\ncoord coord::add(coord position)\r\n{\r\n\tcoord temp;\r\n\r\n\ttemp.x = this-&gt;x + position.x;\r\n\ttemp.y = this-&gt;y + position.y;\r\n\r\n\treturn temp;\r\n}\r\n\r\nint main()\r\n{\r\n\tcoord position_a(10, 10), position_b(5, 3);\r\n\r\n\tcoord position_m = position_a + position_b;\r\n\tposition_m.show();\t\t\/\/ =&gt; x = 15, y = 13\r\n\r\n\tcoord position_n = position_a.add(position_b);\r\n\tposition_n.show();\t\t\/\/ =&gt; x = 15, y = 13\r\n\r\n\treturn 0;\r\n}<\/pre>\n<p>\u203b \u8aac\u660e\u3092\u30b7\u30f3\u30d7\u30eb\u306b\u3059\u308b\u305f\u3081\u306b\u53c2\u7167\u6e21\u3057\u3084const\u4fee\u98fe\u306f\u7701\u7565\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p>\u4e0a\u8a18\u306e\u30b3\u30fc\u30c9\u306b\u304a\u3044\u3066\u3001 +\u6f14\u7b97\u5b50\u306e\u30aa\u30fc\u30d0\u30fc\u30ed\u30fc\u30c9<\/p>\n<pre class=\"lang:c++ decode:true \" >position_a + position_b<\/pre>\n<p>\u306f<\/p>\n<pre class=\"lang:c++ decode:true \" >position_a.operator+(position_b)<\/pre>\n<p>\u306e\u7cd6\u8863\u69cb\u6587\uff08\u30b7\u30e5\u30ac\u30fc\u30b7\u30f3\u30bf\u30c3\u30af\u30b9\uff09\u3067\u3059\u3002<\/p>\n<p>\u30e1\u30f3\u30d0\u30fc\u95a2\u6570\u540d &quot;operator+&quot; \u3092 &quot;add&quot; \u3068\u610f\u8a33\u3059\u308b\u3068\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b\u66f8\u304d\u63db\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:c++ decode:true \" >position_a.add(position_b)<\/pre>\n<p>\u3055\u3089\u306b\u4e0b\u8a18\u306e\u3088\u3046\u306a\u6f14\u7b97\u5b50\u30aa\u30fc\u30d0\u30fc\u30ed\u30fc\u30c9\u3082\u8003\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:c++ decode:true \" >position + 10<\/pre>\n<pre class=\"lang:c++ decode:true \" >position.add(10)<\/pre>\n<p>\u3057\u304b\u3057\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306a\u6f14\u7b97\u5b50\u306e\u30aa\u30fc\u30d0\u30fc\u30ed\u30fc\u30c9\u306f\u300e\u30e1\u30f3\u30d0\u30fc\u95a2\u6570\u300f\u3068\u3057\u3066\u5b9a\u7fa9\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002<\/p>\n<pre class=\"lang:c++ decode:true \" >10 + position<\/pre>\n<pre class=\"lang:c++ decode:true \" >10.add(position)<\/pre>\n<p>&nbsp;<\/p>\n<h2>2. \u30d5\u30ec\u30f3\u30c9\u95a2\u6570\u306e\u6f14\u7b97\u5b50\u30aa\u30fc\u30d0\u30fc\u30ed\u30fc\u30c9<\/h2>\n<p>\u305d\u3053\u3067\u4e0b\u8a18\u306e\u3088\u3046\u306b\u30d5\u30ec\u30f3\u30c9\u95a2\u6570\u306e\u6f14\u7b97\u5b50\u30aa\u30fc\u30d0\u30fc\u30ed\u30fc\u30c9\u3092\u3064\u304b\u3048\u3070\u4e8c\u9805\u6f14\u7b97\u5b50\u306e\u4ea4\u63db\u6cd5\u5247\u3092\u6b63\u3057\u304f\u5b9f\u88c5\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u30d5\u30ec\u30f3\u30c9\u95a2\u6570\u3092\u4f7f\u3046\u76ee\u7684\u306fprivate\u306a\u30e1\u30f3\u30d0\u30fc\u5909\u6570\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3053\u3068\u306b\u3042\u308a\u307e\u3059\u3002\u3082\u3057\u30e1\u30f3\u30d0\u30fc\u5909\u6570\u304cpublic\u3067\u3042\u308c\u3070\u3001\u5fc5\u305a\u3057\u3082\u30d5\u30ec\u30f3\u30c9\u95a2\u6570\u3067\u3042\u308b\u5fc5\u8981\u6027\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u30e1\u30f3\u30d0\u30fc\u5909\u6570\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u975e\u30e1\u30f3\u30d0\u30fc\u306e\u95a2\u6570\u3067\u3042\u308a\u3055\u3048\u3059\u308c\u3070\u4ea4\u63db\u6cd5\u5247\u3092\u5b9f\u73fe\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:c++ decode:true \" >#include &lt;iostream&gt;\r\nusing namespace std;\r\n\r\nclass coord {\r\n\tint x, y;\r\npublic:\r\n\tcoord() : x(0), y(0) {}\r\n\tcoord(int i, int j) : x(i), y(j) {}\r\n\tvoid show() { cout &lt;&lt; \"x = \" &lt;&lt; x &lt;&lt; \", y = \" &lt;&lt; y &lt;&lt; endl; }\r\n\tfriend coord operator+(const coord&amp; pos, const int&amp; dist);\r\n\tfriend coord operator+(const int&amp; dist, const coord&amp; pos);\r\n};\r\n\r\ncoord operator+(const coord&amp; pos, const int&amp; dist)\r\n{\r\n\tcoord temp;\r\n\r\n\ttemp.x = pos.x + dist;\r\n\ttemp.y = pos.y + dist;\r\n\r\n\treturn temp;\r\n}\r\n\r\ncoord operator+(const int&amp; dist, const coord&amp; pos)\r\n{\r\n\tcoord temp;\r\n\r\n\ttemp.x = pos.x + dist;\r\n\ttemp.y = pos.y + dist;\r\n\r\n\treturn temp;\r\n}\r\n\r\nint main()\r\n{\r\n\tcoord position(5, 3);\r\n\r\n\tcoord position_m = position + 5;\r\n\tposition_m.show();\t\t\/\/ =&gt; x = 10, y = 8\r\n\r\n\tcoord position_n = 5 + position ;\r\n\tposition_n.show();\t\t\/\/ =&gt; x = 10, y = 8\r\n\r\n\treturn 0;\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e C++ \u306b\u304a\u3051\u308b\u6f14\u7b97\u5b50\u30aa\u30fc\u30d0\u30fc\u30ed\u30fc\u30c9\u306e\u7cd6\u8863\u69cb\u6587\u7684\u306a\u89e3\u91c8\u3068\u3001\u30d5\u30ec\u30f3\u30c9\u95a2\u6570\u306b\u3088\u308b\u89e3\u6c7a 1. \u6f14\u7b97\u5b50\u30aa\u30fc\u30d0\u30fc\u30ed\u30fc\u30c9\u306e\u7cd6\u8863\u69cb\u6587\u7684\u306a\u89e3\u91c8 #include &lt;iostream&gt; using name [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[84],"class_list":["post-2901","post","type-post","status-publish","format-standard","hentry","category-memo","tag-cpp"],"_links":{"self":[{"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/posts\/2901","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/comments?post=2901"}],"version-history":[{"count":19,"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/posts\/2901\/revisions"}],"predecessor-version":[{"id":2922,"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/posts\/2901\/revisions\/2922"}],"wp:attachment":[{"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/media?parent=2901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/categories?post=2901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chihayafuru.jp\/tech\/index.php\/wp-json\/wp\/v2\/tags?post=2901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}