{"id":165,"date":"2019-07-07T00:09:41","date_gmt":"2019-07-06T16:09:41","guid":{"rendered":"http:\/\/www.guanhaobo.cn\/?p=165"},"modified":"2019-07-07T00:09:41","modified_gmt":"2019-07-06T16:09:41","slug":"c%e7%b1%bb%e6%97%8f%e4%b8%ad%e7%9a%84%e4%b8%80%e4%ba%9b%e8%a7%84%e5%88%99","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=165","title":{"rendered":"C++\u7c7b\u65cf\u4e2d\u7684\u4e00\u4e9b\u89c4\u5219"},"content":{"rendered":"<h3>\u76ee\u6807<\/h3>\n<p>\u672c\u6587\u9a8c\u8bc1\u4ee5\u4e0b\u51e0\u70b9\uff1a<br \/>\n1. \u521b\u5efa\u6d3e\u751f\u7c7b\u5bf9\u8c61\u65f6\uff0c\u81ea\u52a8\u8c03\u7528\u57fa\u7c7b\u6784\u9020\u51fd\u6570<br \/>\n2. \u5728C++\u4e2d\uff0c\u6709\u51fd\u6570\u8986\u76d6\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u201c\u6d3e\u751f\u7c7b\u5bf9\u8c61.\u57fa\u7c7b::\u51fd\u6570(\u53c2\u6570)\u201d\u7684\u5f62\u5f0f\u8c03\u7528\u88ab\u8986\u76d6\u7684\u57fa\u7c7b\u51fd\u6570<br \/>\n3. \u5728C++\u4e2d\uff0c\u5141\u8bb8\u4ee5\u6d3e\u751f\u7c7b\u5bf9\u8c61\u4e3a\u521d\u59cb\u503c\u521b\u5efa\u57fa\u7c7b\u5bf9\u8c61\uff0c\u53cd\u4e4b\u4e0d\u884c<br \/>\n4. \u5728C++\u4e2d\uff0c\u5141\u8bb8\u628a\u6d3e\u751f\u7c7b\u5bf9\u8c61\u8d4b\u503c\u7ed9\u57fa\u7c7b\u5bf9\u8c61\uff0c\u53cd\u4e4b\u4e0d\u884c<br \/>\n5. \u5728C++\u4e2d\uff0c\u53ef\u4ee5\u628a\u6d3e\u751f\u7c7b\u5bf9\u8c61\u7684\u5730\u5740\u8d4b\u503c\u7ed9\u57fa\u7c7b\u6307\u9488\u53d8\u91cf\uff0c\u53cd\u4e4b\u4e0d\u884c<br \/>\n6. \u5728C++\u4e2d\uff0c\u5f53\u57fa\u7c7b\u6307\u9488\u53d8\u91cf\u6307\u5411\u6d3e\u751f\u7c7b\u5bf9\u8c61\u65f6\uff0c\u901a\u8fc7\u8be5\u6307\u9488\u8c03\u7528\u975evirtual\u51fd\u6570\uff0c\u5219\u53ea\u80fd\u8c03\u7528\u57fa\u7c7b\u7684\u51fd\u6570\uff1b\u901a\u8fc7\u8be5\u6307\u9488\u8c03\u7528virtual\u51fd\u6570\uff0c\u8c03\u7528\u7684\u662f\u6d3e\u751f\u7c7b\u7684\u51fd\u6570<\/p>\n<h3>\u4ee3\u7801<\/h3>\n<pre><code class=\"language-cpp line-numbers\">#include &lt;iostream&gt;\n#include &lt;cstdlib&gt;\n#include &lt;string&gt;\nusing namespace std;\n\nclass Animal \/\/\u52a8\u7269\u7c7b\uff0c\u62bd\u8c61\u7c7b\n{\nprotected:\n    int height; \/\/\u8eab\u9ad8\n    int weight; \/\/\u4f53\u91cd\n\npublic:\n    Animal()\n    {\n        cout &lt;&lt; \"\u4e00\u4e2a\u52a8\u7269\u8bde\u751f\u4e86...\" &lt;&lt; endl;\n    }\n\n    void Move()\n    {\n        cout &lt;&lt; \"\u6211\u7684\u4f4d\u7f6e\u53d1\u751f\u4e86\u79fb\u52a8...\" &lt;&lt; endl;\n    }\n    virtual string getClass() = 0;\n};\n\nclass Monkey : public Animal \/\/\u7334\u5b50\u7c7b\n{\nprotected:\n    int height; \/\/\u8eab\u9ad8\n    int weight; \/\/\u4f53\u91cd\n\npublic:\n    Monkey()\n    {\n        height = 130;\n        weight = 40;\n        cout &lt;&lt; \"\u6211\u662f\u4e00\u53ea\u7334\u5b50\" &lt;&lt; endl;\n    }\n\n    void Move()\n    {\n        cout &lt;&lt; \"\u6211\u8df3\u5230\u4e86\u6811\u4e0a\" &lt;&lt; endl;\n    }\n\n    string getClass()\n    {\n        return \"Monkey\";\n    }\n};\n\nclass BlueMonkey : public Monkey \/\/\u84dd\u8272\u7334\u5b50\u7c7b\n{\nprotected:\n    int height; \/\/\u8eab\u9ad8\n    int weight; \/\/\u4f53\u91cd\n\npublic:\n    BlueMonkey()\n    {\n        height = 130;\n        weight = 40;\n        cout &lt;&lt; \"\u6211\u662f\u4e00\u53ea\u84dd\u8272\u7684\u7334\u5b50\" &lt;&lt; endl;\n    }\n\n    void Move()\n    {\n        cout &lt;&lt; \"\u6211\u8df3\u5230\u4e86\u6811\u4e0a\" &lt;&lt; endl;\n    }\n\n    string getClass()\n    {\n        return \"BlueMonkey\";\n    }\n};\n\nint main()\n{\n    Monkey m;         \/\/\u9a8c\u8bc1\u7b2c1\u70b9\n    m.Animal::Move(); \/\/\u9a8c\u8bc1\u7b2c2\u70b9\n    BlueMonkey bm;\n    Monkey m1 = bm; \/\/\u9a8c\u8bc1\u7b2c3\u70b9\n    m = bm; \/\/\u9a8c\u8bc1\u7b2c4\u70b9\n    Monkey *p;\n    p = &amp;bm; \/\/\u9a8c\u8bc1\u7b2c5\u70b9\n    p-&gt;Move();     \/\/\u9a8c\u8bc1\u7b2c6\u70b9\n    cout &lt;&lt; p-&gt;getClass() &lt;&lt; endl; \/\/\u9a8c\u8bc1\u7b2c6\u70b9\n    system(\"pause\");\n    return 0;\n}\n<\/code><\/pre>\n<h3>\u8fd0\u884c\u7ed3\u679c<\/h3>\n<p><a href=\"http:\/\/www.guanhaobo.cn\/wp-content\/uploads\/2019\/07\/QQ\u622a\u56fe20190707000304.jpg\" title=\"\u8fd0\u884c\u7ed3\u679c\"><img decoding=\"async\" src=\"http:\/\/www.guanhaobo.cn\/wp-content\/uploads\/2019\/07\/QQ\u622a\u56fe20190707000304.jpg\" alt=\"\u8fd0\u884c\u7ed3\u679c\" title=\"\u8fd0\u884c\u7ed3\u679c\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u6807 \u672c\u6587\u9a8c\u8bc1\u4ee5\u4e0b\u51e0\u70b9\uff1a 1. \u521b\u5efa\u6d3e\u751f\u7c7b\u5bf9\u8c61\u65f6\uff0c\u81ea\u52a8\u8c03\u7528\u57fa\u7c7b\u6784\u9020\u51fd\u6570 2. \u5728C++\u4e2d\uff0c\u6709\u51fd\u6570\u8986\u76d6\u65f6\uff0c\u53ef\u4ee5\u901a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[8],"class_list":["post-165","post","type-post","status-publish","format-standard","hentry","category-note","tag-cpp"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=165"}],"version-history":[{"count":0,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/165\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}