{"id":3313,"date":"2025-03-29T15:20:45","date_gmt":"2025-03-29T07:20:45","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3313"},"modified":"2025-03-29T15:20:45","modified_gmt":"2025-03-29T07:20:45","slug":"c-%e6%8e%a5%e5%8f%a3%ef%bc%88%e6%8a%bd%e8%b1%a1%e7%b1%bb%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/29\/c-%e6%8e%a5%e5%8f%a3%ef%bc%88%e6%8a%bd%e8%b1%a1%e7%b1%bb%ef%bc%89\/","title":{"rendered":"C++ \u63a5\u53e3\uff08\u62bd\u8c61\u7c7b\uff09"},"content":{"rendered":"\n<p>\u5728 C++ \u4e2d\uff0c\u63a5\u53e3\u5e76\u4e0d\u662f\u4e00\u4e2a\u72ec\u7acb\u7684\u8bed\u6cd5\u6982\u5ff5\uff0c\u800c\u662f\u901a\u8fc7\u62bd\u8c61\u7c7b\u6765\u5b9e\u73b0\u7684\u3002\u62bd\u8c61\u7c7b\u662f\u4e00\u79cd\u4e0d\u80fd\u76f4\u63a5\u5b9e\u4f8b\u5316\u7684\u7c7b\uff0c\u5b83\u53ef\u4ee5\u5305\u542b\u7eaf\u865a\u51fd\u6570\uff08pure virtual functions\uff09\uff0c\u8fd9\u4e9b\u51fd\u6570\u6ca1\u6709\u5b9e\u73b0\uff0c\u5fc5\u987b\u5728\u6d3e\u751f\u7c7b\u4e2d\u8fdb\u884c\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>\u63a5\u53e3\u7684\u4e3b\u8981\u76ee\u7684\u662f\u5b9a\u4e49\u4e00\u7ec4\u65b9\u6cd5\u7684\u534f\u8bae\uff08\u65b9\u6cd5\u540d\u3001\u53c2\u6570\u548c\u8fd4\u56de\u503c\uff09\uff0c\u800c\u4e0d\u5173\u5fc3\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5177\u4f53\u5b9e\u73b0\u3002\u901a\u8fc7\u62bd\u8c61\u7c7b\uff0cC++ \u5141\u8bb8\u6211\u4eec\u4e3a\u6d3e\u751f\u7c7b\u63d0\u4f9b\u7edf\u4e00\u7684\u63a5\u53e3\uff0c\u5e76\u786e\u4fdd\u8fd9\u4e9b\u6d3e\u751f\u7c7b\u5b9e\u73b0\u4e86\u63a5\u53e3\u4e2d\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u62bd\u8c61\u7c7b\u7684\u5b9a\u4e49<\/strong><\/h2>\n\n\n\n<p>\u4e00\u4e2a\u62bd\u8c61\u7c7b\u662f\u542b\u6709\u81f3\u5c11\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570\u7684\u7c7b\u3002\u7eaf\u865a\u51fd\u6570\u901a\u8fc7\u5728\u51fd\u6570\u58f0\u660e\u540e\u52a0 <code>= 0<\/code> \u6765\u5b9a\u4e49\uff0c\u8868\u793a\u8be5\u51fd\u6570\u6ca1\u6709\u5b9e\u73b0\uff0c\u5fc5\u987b\u5728\u6d3e\u751f\u7c7b\u4e2d\u91cd\u5199\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.1 \u7eaf\u865a\u51fd\u6570\u7684\u58f0\u660e<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Shape {\npublic:\n    \/\/ \u7eaf\u865a\u51fd\u6570\uff0c\u58f0\u660e\u63a5\u53e3\n    virtual void draw() = 0;\n    virtual double area() = 0;\n};<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>Shape<\/code> \u662f\u4e00\u4e2a\u62bd\u8c61\u7c7b\uff0c\u5b83\u5305\u542b\u4e24\u4e2a\u7eaf\u865a\u51fd\u6570\uff1a<code>draw()<\/code> \u548c <code>area()<\/code>\u3002\u8fd9\u610f\u5473\u7740\u4efb\u4f55\u7ee7\u627f <code>Shape<\/code> \u7c7b\u7684\u6d3e\u751f\u7c7b\u90fd\u5fc5\u987b\u5b9e\u73b0\u8fd9\u4e24\u4e2a\u51fd\u6570\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u7ee7\u627f\u62bd\u8c61\u7c7b\u5e76\u5b9e\u73b0\u63a5\u53e3<\/strong><\/h2>\n\n\n\n<p>\u6d3e\u751f\u7c7b\u5fc5\u987b\u5b9e\u73b0\u62bd\u8c61\u7c7b\u4e2d\u6240\u6709\u7684\u7eaf\u865a\u51fd\u6570\uff0c\u5426\u5219\u5b83\u4e5f\u5c06\u53d8\u6210\u4e00\u4e2a\u62bd\u8c61\u7c7b\uff0c\u4e0d\u80fd\u76f4\u63a5\u5b9e\u4f8b\u5316\u3002\u6d3e\u751f\u7c7b\u63d0\u4f9b\u4e86\u5177\u4f53\u7684\u5b9e\u73b0\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 \u7ee7\u627f\u5e76\u5b9e\u73b0\u63a5\u53e3<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\n\/\/ \u62bd\u8c61\u7c7b\uff08\u63a5\u53e3\uff09\nclass Shape {\npublic:\n    virtual void draw() = 0;  \/\/ \u7eaf\u865a\u51fd\u6570\n    virtual double area() = 0; \/\/ \u7eaf\u865a\u51fd\u6570\n};\n\n\/\/ \u6d3e\u751f\u7c7b\uff1aCircle\nclass Circle : public Shape {\nprivate:\n    double radius;\npublic:\n    Circle(double r) : radius(r) {}\n\n    \/\/ \u5b9e\u73b0 draw \u51fd\u6570\n    void draw() override {\n        cout &lt;&lt; \"Drawing a circle with radius \" &lt;&lt; radius &lt;&lt; endl;\n    }\n\n    \/\/ \u5b9e\u73b0 area \u51fd\u6570\n    double area() override {\n        return 3.14159 * radius * radius;\n    }\n};\n\n\/\/ \u6d3e\u751f\u7c7b\uff1aRectangle\nclass Rectangle : public Shape {\nprivate:\n    double width, height;\npublic:\n    Rectangle(double w, double h) : width(w), height(h) {}\n\n    \/\/ \u5b9e\u73b0 draw \u51fd\u6570\n    void draw() override {\n        cout &lt;&lt; \"Drawing a rectangle with width \" &lt;&lt; width &lt;&lt; \" and height \" &lt;&lt; height &lt;&lt; endl;\n    }\n\n    \/\/ \u5b9e\u73b0 area \u51fd\u6570\n    double area() override {\n        return width * height;\n    }\n};\n\nint main() {\n    \/\/ \u521b\u5efa\u5177\u4f53\u7684\u5bf9\u8c61\n    Circle c(5);\n    Rectangle r(4, 6);\n\n    \/\/ \u4f7f\u7528\u591a\u6001\n    Shape* shape1 = &amp;c;\n    Shape* shape2 = &amp;r;\n\n    shape1-&gt;draw();\n    cout &lt;&lt; \"Area: \" &lt;&lt; shape1-&gt;area() &lt;&lt; endl;\n\n    shape2-&gt;draw();\n    cout &lt;&lt; \"Area: \" &lt;&lt; shape2-&gt;area() &lt;&lt; endl;\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Drawing a circle with radius 5\nArea: 78.5398\nDrawing a rectangle with width 4 and height 6\nArea: 24<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u5173\u952e\u6982\u5ff5<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1 \u63a5\u53e3\u7684\u5b9e\u73b0<\/strong><\/h3>\n\n\n\n<p>\u5728 C++ \u4e2d\uff0c\u63a5\u53e3\u901a\u5e38\u662f\u901a\u8fc7\u4e00\u4e2a\u5b8c\u5168\u7531\u7eaf\u865a\u51fd\u6570\u6784\u6210\u7684\u62bd\u8c61\u7c7b\u6765\u5b9e\u73b0\u7684\u3002\u6d3e\u751f\u7c7b\u5fc5\u987b\u5b9e\u73b0\u6240\u6709\u7684\u7eaf\u865a\u51fd\u6570\uff0c\u5426\u5219\u5b83\u4eec\u5c06\u4ecd\u7136\u662f\u62bd\u8c61\u7c7b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.2 \u591a\u6001\u6027\uff08Polymorphism\uff09<\/strong><\/h3>\n\n\n\n<p>\u901a\u8fc7\u63a5\u53e3\uff08\u62bd\u8c61\u7c7b\uff09\u5b9e\u73b0\u7684\u591a\u6001\u6027\u662f C++ \u4e2d\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u4e00\u4e2a\u91cd\u8981\u7279\u6027\u3002\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>Shape* shape1<\/code> \u548c <code>Shape* shape2<\/code> \u90fd\u6307\u5411\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\uff08<code>Circle<\/code> \u548c <code>Rectangle<\/code>\uff09\uff0c\u4f46\u901a\u8fc7\u5b83\u4eec\u8bbf\u95ee <code>draw()<\/code> \u548c <code>area()<\/code> \u51fd\u6570\u65f6\uff0c\u5b9e\u9645\u8c03\u7528\u7684\u662f\u6d3e\u751f\u7c7b\u7684\u5b9e\u73b0\u3002\u8fd9\u5c31\u662f C++ \u7684\u591a\u6001\u6027\uff0c\u5b83\u5141\u8bb8\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u4ee5\u76f8\u540c\u7684\u63a5\u53e3\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.3 \u4e0d\u80fd\u5b9e\u4f8b\u5316\u62bd\u8c61\u7c7b<\/strong><\/h3>\n\n\n\n<p>\u62bd\u8c61\u7c7b\u4e0d\u80fd\u76f4\u63a5\u5b9e\u4f8b\u5316\uff0c\u5b83\u7684\u5b58\u5728\u4ec5\u7528\u4e8e\u63d0\u4f9b\u7edf\u4e00\u7684\u63a5\u53e3\u5b9a\u4e49\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u62bd\u8c61\u7c7b\u7684\u6307\u9488\u6216\u5f15\u7528\u6765\u64cd\u4f5c\u6d3e\u751f\u7c7b\u5bf9\u8c61\uff0c\u4f46\u4e0d\u80fd\u521b\u5efa\u62bd\u8c61\u7c7b\u7684\u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u9519\u8bef\uff1a\u65e0\u6cd5\u5b9e\u4f8b\u5316\u62bd\u8c61\u7c7b\n\/\/ Shape shape;  \/\/ \u7f16\u8bd1\u9519\u8bef<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.4 \u63a5\u53e3\u7684\u597d\u5904<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7edf\u4e00\u63a5\u53e3<\/strong>\uff1a\u63a5\u53e3\u5b9a\u4e49\u4e86\u6240\u6709\u5b9e\u73b0\u7c7b\u90fd\u5fc5\u987b\u9075\u5faa\u7684\u89c4\u8303\uff0c\u786e\u4fdd\u6bcf\u4e2a\u5b9e\u73b0\u7c7b\u90fd\u5177\u5907\u67d0\u4e9b\u65b9\u6cd5\uff0c\u8fd9\u6709\u52a9\u4e8e\u4ee3\u7801\u7684\u89c4\u8303\u5316\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u8026<\/strong>\uff1a\u63a5\u53e3\u63d0\u4f9b\u4e86\u4e00\u79cd\u89e3\u8026\u673a\u5236\u3002\u901a\u8fc7\u4f7f\u7528\u63a5\u53e3\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4e0d\u5173\u5fc3\u5177\u4f53\u5b9e\u73b0\u7684\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u591a\u6001\u8c03\u7528\u5bf9\u8c61\u7684\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li><strong>\u7075\u6d3b\u6027\u548c\u6269\u5c55\u6027<\/strong>\uff1a\u901a\u8fc7\u63a5\u53e3\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u6269\u5c55\u65b0\u7684\u7c7b\uff0c\u800c\u4e0d\u5f71\u54cd\u73b0\u6709\u7684\u4ee3\u7801\u3002\u53ea\u9700\u6dfb\u52a0\u65b0\u7c7b\u5e76\u5b9e\u73b0\u63a5\u53e3\u5373\u53ef\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p>\u5728 C++ \u4e2d\uff0c\u63a5\u53e3\u662f\u901a\u8fc7\u62bd\u8c61\u7c7b\u548c\u7eaf\u865a\u51fd\u6570\u5b9e\u73b0\u7684\u3002\u62bd\u8c61\u7c7b\u4f5c\u4e3a\u63a5\u53e3\u63d0\u4f9b\u65b9\u6cd5\u7684\u58f0\u660e\uff0c\u800c\u5177\u4f53\u7684\u6d3e\u751f\u7c7b\u5b9e\u73b0\u8fd9\u4e9b\u65b9\u6cd5\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u591a\u6001\u548c\u89e3\u8026\uff0c\u4ece\u800c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7075\u6d3b\u3001\u53ef\u7ef4\u62a4\u548c\u53ef\u6269\u5c55\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 C++ \u4e2d\uff0c\u63a5\u53e3\u5e76\u4e0d\u662f\u4e00\u4e2a\u72ec\u7acb\u7684\u8bed\u6cd5\u6982\u5ff5\uff0c\u800c\u662f\u901a\u8fc7\u62bd\u8c61\u7c7b\u6765\u5b9e\u73b0\u7684\u3002\u62bd\u8c61\u7c7b\u662f\u4e00\u79cd\u4e0d\u80fd\u76f4\u63a5\u5b9e\u4f8b\u5316\u7684\u7c7b\uff0c\u5b83\u53ef\u4ee5\u5305 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3314,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[81],"tags":[],"class_list":["post-3313","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3313","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3313"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3313\/revisions"}],"predecessor-version":[{"id":3315,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3313\/revisions\/3315"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3314"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}