{"id":3307,"date":"2025-03-29T15:18:54","date_gmt":"2025-03-29T07:18:54","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3307"},"modified":"2025-03-29T15:18:54","modified_gmt":"2025-03-29T07:18:54","slug":"c-%e6%95%b0%e6%8d%ae%e6%8a%bd%e8%b1%a1%ef%bc%88data-abstraction%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/29\/c-%e6%95%b0%e6%8d%ae%e6%8a%bd%e8%b1%a1%ef%bc%88data-abstraction%ef%bc%89\/","title":{"rendered":"C++ \u6570\u636e\u62bd\u8c61\uff08Data Abstraction\uff09"},"content":{"rendered":"\n<p>\u6570\u636e\u62bd\u8c61\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\uff0c\u5b83\u901a\u8fc7\u5c06\u6570\u636e\u7684\u5177\u4f53\u5b9e\u73b0\u7ec6\u8282\u9690\u85cf\u8d77\u6765\uff0c\u63d0\u4f9b\u4e00\u4e2a\u7b80\u6d01\u7684\u63a5\u53e3\u6765\u64cd\u4f5c\u6570\u636e\u3002\u6570\u636e\u62bd\u8c61\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u63a7\u5236\u7a0b\u5e8f\u590d\u6742\u5ea6\uff0c\u5e76\u786e\u4fdd\u6570\u636e\u7684\u5b89\u5168\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n\n\n\n<p>\u5728 C++ \u4e2d\uff0c\u6570\u636e\u62bd\u8c61\u901a\u5e38\u901a\u8fc7<strong>\u7c7b<\/strong>\uff08class\uff09\u548c<strong>\u63a5\u53e3<\/strong>\uff08interface\uff09\u6765\u5b9e\u73b0\u3002\u5b83\u5f3a\u8c03\u5c06\u6570\u636e\u8868\u793a\uff08\u5373\u7c7b\u7684\u6210\u5458\u53d8\u91cf\uff09\u548c\u64cd\u4f5c\u8fd9\u4e9b\u6570\u636e\u7684\u51fd\u6570\uff08\u5373\u6210\u5458\u51fd\u6570\uff09\u5206\u5f00\u3002\u6570\u636e\u62bd\u8c61\u4f7f\u5f97\u7528\u6237\u53ea\u9700\u8981\u5173\u6ce8\u5bf9\u8c61\u7684\u884c\u4e3a\uff0c\u800c\u65e0\u9700\u4e86\u89e3\u5176\u5b9e\u73b0\u7ec6\u8282\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u6570\u636e\u62bd\u8c61\u7684\u6982\u5ff5<\/strong><\/h2>\n\n\n\n<p>\u6570\u636e\u62bd\u8c61\u6709\u4e24\u4e2a\u4e3b\u8981\u7279\u6027\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4fe1\u606f\u9690\u85cf\uff08Information Hiding\uff09<\/strong>\uff1a\u901a\u8fc7\u5c06\u7c7b\u7684\u5185\u90e8\u5b9e\u73b0\u7ec6\u8282\uff08\u5982\u6570\u636e\u6210\u5458\uff09\u9690\u85cf\u5728\u7c7b\u7684\u5185\u90e8\uff0c\u5916\u90e8\u4ee3\u7801\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee\u8fd9\u4e9b\u6570\u636e\u3002\u8fd9\u6709\u52a9\u4e8e\u51cf\u5c11\u7cfb\u7edf\u590d\u6742\u6027\uff0c\u9632\u6b62\u5916\u90e8\u4ee3\u7801\u5bf9\u6570\u636e\u7684\u968f\u610f\u4fee\u6539\u3002<\/li>\n\n\n\n<li><strong>\u63a5\u53e3\u4e0e\u5b9e\u73b0\u5206\u79bb\uff08Separation of Interface and Implementation\uff09<\/strong>\uff1a\u7c7b\u63d0\u4f9b\u63a5\u53e3\uff08public \u65b9\u6cd5\uff09\u4e0e\u5916\u754c\u4ea4\u4e92\uff0c\u5177\u4f53\u5b9e\u73b0\u5219\u9690\u85cf\u5728\u7c7b\u7684\u79c1\u6709\u90e8\u5206\uff08private \u90e8\u5206\uff09\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u5982\u4f55\u5b9e\u73b0\u6570\u636e\u62bd\u8c61<\/strong><\/h2>\n\n\n\n<p>\u5728 C++ \u4e2d\uff0c\u6570\u636e\u62bd\u8c61\u901a\u5e38\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 \u7c7b\uff08Class\uff09<\/strong><\/h3>\n\n\n\n<p>\u7c7b\u662f C++ \u4e2d\u5b9e\u73b0\u6570\u636e\u62bd\u8c61\u7684\u57fa\u672c\u7ed3\u6784\uff0c\u5b83\u5c01\u88c5\u4e86\u6570\u636e\u6210\u5458\u548c\u6210\u5458\u51fd\u6570\u3002\u7c7b\u7684<strong>\u6210\u5458\u53d8\u91cf<\/strong>\u901a\u5e38\u8bbe\u7f6e\u4e3a\u79c1\u6709\u7684\uff08private\uff09\uff0c\u800c<strong>\u6210\u5458\u51fd\u6570<\/strong>\u5219\u8bbe\u7f6e\u4e3a\u516c\u6709\u7684\uff08public\uff09\uff0c\u4ee5\u63d0\u4f9b\u8bbf\u95ee\u548c\u64cd\u4f5c\u6570\u636e\u7684\u65b9\u5f0f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Account {\nprivate:\n    double balance;  \/\/ \u6570\u636e\u6210\u5458\uff1a\u5b58\u50a8\u8d26\u6237\u4f59\u989d\n\npublic:\n    \/\/ \u6784\u9020\u51fd\u6570\uff1a\u521d\u59cb\u5316\u4f59\u989d\n    Account(double initialBalance) {\n        if (initialBalance &gt; 0) {\n            balance = initialBalance;\n        } else {\n            balance = 0;\n        }\n    }\n\n    \/\/ \u516c\u6709\u65b9\u6cd5\uff1a\u5b58\u6b3e\n    void deposit(double amount) {\n        if (amount &gt; 0) {\n            balance += amount;\n        }\n    }\n\n    \/\/ \u516c\u6709\u65b9\u6cd5\uff1a\u53d6\u6b3e\n    void withdraw(double amount) {\n        if (amount &gt; 0 &amp;&amp; amount &lt;= balance) {\n            balance -= amount;\n        } else {\n            cout &lt;&lt; \"Invalid withdrawal amount!\" &lt;&lt; endl;\n        }\n    }\n\n    \/\/ \u516c\u6709\u65b9\u6cd5\uff1a\u67e5\u8be2\u4f59\u989d\n    double getBalance() const {\n        return balance;\n    }\n};\n\nint main() {\n    Account acc(1000);  \/\/ \u521b\u5efa\u4e00\u4e2a\u8d26\u6237\uff0c\u521d\u59cb\u4f59\u989d\u4e3a 1000\n\n    acc.deposit(500);  \/\/ \u5b58\u6b3e 500\n    acc.withdraw(300); \/\/ \u53d6\u6b3e 300\n\n    cout &lt;&lt; \"Current balance: \" &lt;&lt; acc.getBalance() &lt;&lt; endl;  \/\/ \u67e5\u8be2\u4f59\u989d\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>Current balance: 1200<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>balance<\/code> \u662f\u4e00\u4e2a\u79c1\u6709\u7684\u6210\u5458\u53d8\u91cf\uff0c\u5916\u90e8\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee\u3002\u901a\u8fc7 <code>deposit()<\/code>\u3001<code>withdraw()<\/code> \u548c <code>getBalance()<\/code> \u65b9\u6cd5\uff0c\u5916\u90e8\u53ef\u4ee5\u64cd\u4f5c\u8d26\u6237\u4f59\u989d\uff0c\u4f46\u65e0\u6cd5\u76f4\u63a5\u4fee\u6539 <code>balance<\/code>\u3002\u8fd9\u5c31\u5b9e\u73b0\u4e86\u6570\u636e\u62bd\u8c61\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 \u5c01\u88c5\uff08Encapsulation\uff09<\/strong><\/h3>\n\n\n\n<p>\u5c01\u88c5\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u4e00\u79cd\u673a\u5236\uff0c\u5b83\u5c06\u6570\u636e\u548c\u64cd\u4f5c\u8fd9\u4e9b\u6570\u636e\u7684\u4ee3\u7801\u6346\u7ed1\u5728\u4e00\u8d77\u3002\u901a\u8fc7\u5c01\u88c5\uff0c\u7c7b\u7684\u5b9e\u73b0\u7ec6\u8282\u5bf9\u5916\u90e8\u4ee3\u7801\u662f\u9690\u85cf\u7684\uff0c\u5916\u90e8\u53ea\u80fd\u901a\u8fc7\u7c7b\u63d0\u4f9b\u7684\u63a5\u53e3\u6765\u8bbf\u95ee\u6216\u4fee\u6539\u6570\u636e\u3002<\/p>\n\n\n\n<p>C++ \u4e2d\u5c01\u88c5\u7684\u5b9e\u73b0\u901a\u5e38\u901a\u8fc7\u7c7b\u7684\u8bbf\u95ee\u63a7\u5236\u6765\u5b8c\u6210\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>public<\/strong>\uff1a\u53ef\u4ee5\u4ece\u7c7b\u5916\u90e8\u8bbf\u95ee\uff0c\u901a\u5e38\u7528\u4e8e\u63d0\u4f9b\u5bf9\u5916\u7684\u63a5\u53e3\u3002<\/li>\n\n\n\n<li><strong>private<\/strong>\uff1a\u53ea\u80fd\u5728\u7c7b\u7684\u5185\u90e8\u8bbf\u95ee\uff0c\u901a\u5e38\u7528\u4e8e\u6570\u636e\u6210\u5458\uff0c\u9632\u6b62\u76f4\u63a5\u4fee\u6539\u3002<\/li>\n\n\n\n<li><strong>protected<\/strong>\uff1a\u53ea\u80fd\u5728\u7c7b\u548c\u6d3e\u751f\u7c7b\u4e2d\u8bbf\u95ee\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>class Rectangle {\nprivate:\n    double width;\n    double height;\n\npublic:\n    \/\/ \u6784\u9020\u51fd\u6570\n    Rectangle(double w, double h) : width(w), height(h) {}\n\n    \/\/ \u516c\u6709\u65b9\u6cd5\uff1a\u8ba1\u7b97\u9762\u79ef\n    double area() {\n        return width * height;\n    }\n\n    \/\/ \u516c\u6709\u65b9\u6cd5\uff1a\u8bbe\u7f6e\u5bbd\u5ea6\n    void setWidth(double w) {\n        if (w &gt; 0) {\n            width = w;\n        }\n    }\n\n    \/\/ \u516c\u6709\u65b9\u6cd5\uff1a\u8bbe\u7f6e\u9ad8\u5ea6\n    void setHeight(double h) {\n        if (h &gt; 0) {\n            height = h;\n        }\n    }\n};<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>width<\/code> \u548c <code>height<\/code> \u88ab\u5c01\u88c5\u4e3a\u79c1\u6709\u6210\u5458\uff0c\u5916\u90e8\u65e0\u6cd5\u76f4\u63a5\u4fee\u6539\u8fd9\u4e9b\u6570\u636e\uff0c\u800c\u53ea\u80fd\u901a\u8fc7\u7c7b\u63d0\u4f9b\u7684 <code>setWidth()<\/code> \u548c <code>setHeight()<\/code> \u65b9\u6cd5\u6765\u4fee\u6539\u5b83\u4eec\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.3 \u62bd\u8c61\u7c7b\u548c\u63a5\u53e3\uff08Abstract Class and Interface\uff09<\/strong><\/h3>\n\n\n\n<p>C++ \u4e2d\u7684\u62bd\u8c61\u7c7b\u53ef\u4ee5\u6709\u7eaf\u865a\u51fd\u6570\uff08pure virtual functions\uff09\uff0c\u8fd9\u79cd\u7c7b\u4e0d\u80fd\u5b9e\u4f8b\u5316\uff0c\u53ea\u80fd\u4f5c\u4e3a\u5176\u4ed6\u7c7b\u7684\u57fa\u7c7b\u3002\u62bd\u8c61\u7c7b\u7528\u4e8e\u5b9a\u4e49\u63a5\u53e3\uff0c\u5b83\u5c06\u5177\u4f53\u7684\u5b9e\u73b0\u7559\u7ed9\u6d3e\u751f\u7c7b\u3002\u901a\u8fc7\u62bd\u8c61\u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u4e0d\u540c\u7c7b\u5171\u4eab\u7684\u63a5\u53e3\uff0c\u800c\u4e0d\u5173\u5fc3\u5b83\u4eec\u5177\u4f53\u7684\u5b9e\u73b0\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Shape {\npublic:\n    \/\/ \u7eaf\u865a\u51fd\u6570\uff1a\u6240\u6709\u6d3e\u751f\u7c7b\u5fc5\u987b\u5b9e\u73b0\u8be5\u51fd\u6570\n    virtual double area() = 0;\n    virtual double perimeter() = 0;\n};\n\nclass Circle : public Shape {\nprivate:\n    double radius;\n\npublic:\n    Circle(double r) : radius(r) {}\n\n    double area() override {\n        return 3.14 * radius * radius;\n    }\n\n    double perimeter() override {\n        return 2 * 3.14 * radius;\n    }\n};\n\nclass Rectangle : public Shape {\nprivate:\n    double width, height;\n\npublic:\n    Rectangle(double w, double h) : width(w), height(h) {}\n\n    double area() override {\n        return width * height;\n    }\n\n    double perimeter() override {\n        return 2 * (width + height);\n    }\n};<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\uff0c<code>Shape<\/code> \u662f\u4e00\u4e2a\u62bd\u8c61\u7c7b\uff0c\u5b83\u5b9a\u4e49\u4e86 <code>area()<\/code> \u548c <code>perimeter()<\/code> \u4e24\u4e2a\u7eaf\u865a\u51fd\u6570\uff0c\u800c\u5177\u4f53\u7684\u5b9e\u73b0\u5219\u7559\u7ed9\u6d3e\u751f\u7c7b <code>Circle<\/code> \u548c <code>Rectangle<\/code> \u5b8c\u6210\u3002\u6211\u4eec\u4e0d\u80fd\u5b9e\u4f8b\u5316 <code>Shape<\/code> \u7c7b\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u591a\u6001\u6765\u4f7f\u7528\u5176\u63a5\u53e3\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u6570\u636e\u62bd\u8c61\u7684\u4f18\u52bf<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7b80\u5316\u590d\u6742\u6027<\/strong>\uff1a\u6570\u636e\u62bd\u8c61\u9690\u85cf\u4e86\u5bf9\u8c61\u5185\u90e8\u7684\u5b9e\u73b0\u7ec6\u8282\uff0c\u4f7f\u5f97\u7a0b\u5e8f\u7684\u4f7f\u7528\u8005\u53ea\u9700\u8981\u5173\u5fc3\u5982\u4f55\u4f7f\u7528\u5bf9\u8c61\uff0c\u800c\u4e0d\u9700\u8981\u4e86\u89e3\u5176\u5185\u90e8\u662f\u5982\u4f55\u5b9e\u73b0\u7684\u3002<\/li>\n\n\n\n<li><strong>\u589e\u5f3a\u4ee3\u7801\u53ef\u7ef4\u62a4\u6027<\/strong>\uff1a\u901a\u8fc7\u5c06\u6570\u636e\u548c\u64cd\u4f5c\u5c01\u88c5\u5728\u4e00\u4e2a\u7c7b\u4e2d\uff0c\u5982\u679c\u9700\u8981\u4fee\u6539\u5185\u90e8\u5b9e\u73b0\uff0c\u5916\u90e8\u63a5\u53e3\u4e0d\u9700\u8981\u6539\u53d8\uff0c\u4ece\u800c\u964d\u4f4e\u4e86\u4fee\u6539\u5f15\u53d1\u9519\u8bef\u7684\u98ce\u9669\u3002<\/li>\n\n\n\n<li><strong>\u63d0\u9ad8\u4ee3\u7801\u91cd\u7528\u6027<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f\u548c\u591a\u6001\uff0c\u91cd\u7528\u57fa\u7c7b\u7684\u63a5\u53e3\u548c\u529f\u80fd\uff0c\u907f\u514d\u91cd\u590d\u4ee3\u7801\u7684\u7f16\u5199\u3002<\/li>\n\n\n\n<li><strong>\u589e\u5f3a\u6570\u636e\u5b89\u5168\u6027<\/strong>\uff1a\u901a\u8fc7\u4fe1\u606f\u9690\u85cf\uff0c\u5916\u90e8\u4ee3\u7801\u65e0\u6cd5\u76f4\u63a5\u4fee\u6539\u6570\u636e\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u6570\u636e\u7684\u5b89\u5168\u6027\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u5c0f\u7ed3<\/strong><\/h2>\n\n\n\n<p>\u6570\u636e\u62bd\u8c61\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u91cd\u8981\u7279\u6027\uff0cC++ \u4e2d\u901a\u8fc7\u7c7b\u3001\u5c01\u88c5\u3001\u62bd\u8c61\u7c7b\u7b49\u673a\u5236\u6765\u5b9e\u73b0\u6570\u636e\u62bd\u8c61\u3002\u901a\u8fc7\u6570\u636e\u62bd\u8c61\uff0c\u6211\u4eec\u53ef\u4ee5\u9690\u85cf\u5bf9\u8c61\u7684\u5185\u90e8\u5b9e\u73b0\u7ec6\u8282\uff0c\u63d0\u4f9b\u7b80\u6d01\u4e14\u6613\u4e8e\u4f7f\u7528\u7684\u63a5\u53e3\uff0c\u4ece\u800c\u7b80\u5316\u4ee3\u7801\u7684\u590d\u6742\u6027\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u5b89\u5168\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6570\u636e\u62bd\u8c61\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\uff0c\u5b83\u901a\u8fc7\u5c06\u6570\u636e\u7684\u5177\u4f53\u5b9e\u73b0\u7ec6\u8282\u9690\u85cf\u8d77\u6765\uff0c\u63d0\u4f9b\u4e00\u4e2a\u7b80\u6d01\u7684\u63a5\u53e3\u6765\u64cd\u4f5c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3308,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[81],"tags":[],"class_list":["post-3307","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\/3307","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=3307"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3307\/revisions"}],"predecessor-version":[{"id":3309,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3307\/revisions\/3309"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3308"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}