{"id":3310,"date":"2025-03-29T15:19:49","date_gmt":"2025-03-29T07:19:49","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3310"},"modified":"2025-03-29T15:19:49","modified_gmt":"2025-03-29T07:19:49","slug":"c-%e6%95%b0%e6%8d%ae%e5%b0%81%e8%a3%85%ef%bc%88data-encapsulation%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/29\/c-%e6%95%b0%e6%8d%ae%e5%b0%81%e8%a3%85%ef%bc%88data-encapsulation%ef%bc%89\/","title":{"rendered":"C++ \u6570\u636e\u5c01\u88c5\uff08Data Encapsulation\uff09"},"content":{"rendered":"\n<p>\u6570\u636e\u5c01\u88c5\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\u4e2d\u7684\u4e00\u4e2a\u57fa\u672c\u6982\u5ff5\uff0c\u5b83\u901a\u8fc7\u5c06\u6570\u636e\u548c\u64cd\u4f5c\u6570\u636e\u7684\u51fd\u6570\uff08\u65b9\u6cd5\uff09\u6346\u7ed1\u5728\u4e00\u8d77\uff0c\u540c\u65f6\u9690\u85cf\u6570\u636e\u7684\u5b9e\u73b0\u7ec6\u8282\uff0c\u53ea\u66b4\u9732\u51fa\u5fc5\u8981\u7684\u63a5\u53e3\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u51cf\u5c11\u590d\u6742\u6027\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u5b89\u5168\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u91cd\u7528\u6027\u3002<\/p>\n\n\n\n<p>\u5728 C++ \u4e2d\uff0c\u6570\u636e\u5c01\u88c5\u901a\u8fc7\u7c7b\uff08class\uff09\u6765\u5b9e\u73b0\u3002\u7c7b\u5c06\u6570\u636e\u6210\u5458\uff08\u6210\u5458\u53d8\u91cf\uff09\u548c\u64cd\u4f5c\u8fd9\u4e9b\u6570\u636e\u7684\u6210\u5458\u51fd\u6570\uff08\u65b9\u6cd5\uff09\u5c01\u88c5\u5728\u4e00\u8d77\u3002\u901a\u8fc7\u5c01\u88c5\uff0c\u53ef\u4ee5\u63a7\u5236\u5bf9\u6570\u636e\u7684\u8bbf\u95ee\u6743\u9650\uff0c\u786e\u4fdd\u6570\u636e\u4e0d\u88ab\u5916\u90e8\u7a0b\u5e8f\u9519\u8bef\u4fee\u6539\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u6570\u636e\u5c01\u88c5\u7684\u6982\u5ff5<\/strong><\/h2>\n\n\n\n<p>\u5c01\u88c5\u7684\u76ee\u6807\u662f\u5c06\u6570\u636e\uff08\u6210\u5458\u53d8\u91cf\uff09\u548c\u64cd\u4f5c\u6570\u636e\u7684\u884c\u4e3a\uff08\u6210\u5458\u51fd\u6570\uff09\u6346\u7ed1\u5728\u4e00\u8d77\uff0c\u5e76\u901a\u8fc7\u63a7\u5236\u8bbf\u95ee\u6743\u9650\u6765\u4fdd\u62a4\u6570\u636e\u3002C++ \u901a\u8fc7\u8bbf\u95ee\u63a7\u5236\u7b26\u6765\u5b9e\u73b0\u5c01\u88c5\uff0c\u5b83\u6709\u4e09\u4e2a\u4e3b\u8981\u7684\u8bbf\u95ee\u63a7\u5236\u7b26\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>public<\/strong>\uff1a\u53ef\u4ee5\u88ab\u7c7b\u5916\u90e8\u7684\u4ee3\u7801\u8bbf\u95ee\u3002<\/li>\n\n\n\n<li><strong>private<\/strong>\uff1a\u53ea\u80fd\u5728\u7c7b\u7684\u5185\u90e8\u8bbf\u95ee\uff0c\u5916\u90e8\u65e0\u6cd5\u8bbf\u95ee\u3002<\/li>\n\n\n\n<li><strong>protected<\/strong>\uff1a\u53ef\u4ee5\u5728\u7c7b\u7684\u5185\u90e8\u53ca\u5176\u6d3e\u751f\u7c7b\u4e2d\u8bbf\u95ee\uff0c\u4f46\u5916\u90e8\u4ee3\u7801\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee\u3002<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u5982\u4f55\u5b9e\u73b0\u6570\u636e\u5c01\u88c5<\/strong><\/h2>\n\n\n\n<p>\u5728 C++ \u4e2d\uff0c\u6570\u636e\u5c01\u88c5\u901a\u5e38\u901a\u8fc7\u5b9a\u4e49\u7c7b\u6765\u5b9e\u73b0\u3002\u7c7b\u4e2d\u7684\u6210\u5458\u53d8\u91cf\u901a\u5e38\u88ab\u8bbe\u7f6e\u4e3a <code>private<\/code>\uff0c\u800c\u64cd\u4f5c\u8fd9\u4e9b\u6210\u5458\u53d8\u91cf\u7684\u65b9\u6cd5\uff08\u5982 getter \u548c setter \u65b9\u6cd5\uff09\u88ab\u8bbe\u7f6e\u4e3a <code>public<\/code>\uff0c\u4ee5\u63a7\u5236\u5bf9\u6570\u636e\u7684\u8bbf\u95ee\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 \u7c7b\u4e0e\u6210\u5458\u7684\u8bbf\u95ee\u63a7\u5236<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Person {\nprivate:\n    string name;  \/\/ \u79c1\u6709\u6570\u636e\u6210\u5458\n    int age;      \/\/ \u79c1\u6709\u6570\u636e\u6210\u5458\n\npublic:\n    \/\/ \u6784\u9020\u51fd\u6570\uff1a\u521d\u59cb\u5316\u59d3\u540d\u548c\u5e74\u9f84\n    Person(string n, int a) : name(n), age(a) {}\n\n    \/\/ \u516c\u6709\u65b9\u6cd5\uff1a\u8bbe\u7f6e\u59d3\u540d\n    void setName(string n) {\n        name = n;\n    }\n\n    \/\/ \u516c\u6709\u65b9\u6cd5\uff1a\u8bbe\u7f6e\u5e74\u9f84\n    void setAge(int a) {\n        if (a &gt;= 0) {\n            age = a;\n        }\n    }\n\n    \/\/ \u516c\u6709\u65b9\u6cd5\uff1a\u83b7\u53d6\u59d3\u540d\n    string getName() {\n        return name;\n    }\n\n    \/\/ \u516c\u6709\u65b9\u6cd5\uff1a\u83b7\u53d6\u5e74\u9f84\n    int getAge() {\n        return age;\n    }\n};\n\nint main() {\n    \/\/ \u521b\u5efa\u5bf9\u8c61\u5e76\u521d\u59cb\u5316\n    Person p(\"Alice\", 30);\n\n    \/\/ \u83b7\u53d6\u5bf9\u8c61\u7684\u5c5e\u6027\u503c\n    cout &lt;&lt; \"Name: \" &lt;&lt; p.getName() &lt;&lt; endl;\n    cout &lt;&lt; \"Age: \" &lt;&lt; p.getAge() &lt;&lt; endl;\n\n    \/\/ \u4fee\u6539\u5bf9\u8c61\u7684\u5c5e\u6027\u503c\n    p.setName(\"Bob\");\n    p.setAge(35);\n\n    \/\/ \u83b7\u53d6\u4fee\u6539\u540e\u7684\u5c5e\u6027\u503c\n    cout &lt;&lt; \"Updated Name: \" &lt;&lt; p.getName() &lt;&lt; endl;\n    cout &lt;&lt; \"Updated Age: \" &lt;&lt; p.getAge() &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>Name: Alice\nAge: 30\nUpdated Name: Bob\nUpdated Age: 35<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>name<\/code> \u548c <code>age<\/code> \u662f\u79c1\u6709\u7684\u6210\u5458\u53d8\u91cf\uff0c\u5916\u90e8\u4ee3\u7801\u65e0\u6cd5\u76f4\u63a5\u8bbf\u95ee\u3002\u901a\u8fc7 <code>setName()<\/code>\u3001<code>setAge()<\/code> \u548c <code>getName()<\/code>\u3001<code>getAge()<\/code> \u65b9\u6cd5\uff0c\u5916\u90e8\u53ef\u4ee5\u4fee\u6539\u548c\u8bbf\u95ee\u8fd9\u4e9b\u79c1\u6709\u6570\u636e\u6210\u5458\u3002\u8fd9\u5c31\u662f\u901a\u8fc7\u5c01\u88c5\u5b9e\u73b0\u7684\u63a7\u5236\u8bbf\u95ee\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 \u901a\u8fc7 Getter \u548c Setter \u65b9\u6cd5\u5c01\u88c5\u6570\u636e<\/strong><\/h3>\n\n\n\n<p>\u5c01\u88c5\u4e0d\u4ec5\u4ec5\u662f\u9690\u85cf\u6570\u636e\uff0c\u8fd8\u901a\u8fc7\u4e13\u95e8\u7684\u65b9\u6cd5\uff08getter \u548c setter\uff09\u63a7\u5236\u6570\u636e\u7684\u8bbf\u95ee\u3002<code>getter<\/code> \u7528\u4e8e\u83b7\u53d6\u79c1\u6709\u6210\u5458\u7684\u503c\uff0c<code>setter<\/code> \u7528\u4e8e\u8bbe\u7f6e\u79c1\u6709\u6210\u5458\u7684\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class BankAccount {\nprivate:\n    double balance;\n\npublic:\n    \/\/ \u6784\u9020\u51fd\u6570\uff1a\u521d\u59cb\u5316\u8d26\u6237\u4f59\u989d\n    BankAccount(double initialBalance) : balance(initialBalance) {}\n\n    \/\/ \u83b7\u53d6\u8d26\u6237\u4f59\u989d\uff08getter\uff09\n    double getBalance() {\n        return balance;\n    }\n\n    \/\/ \u5b58\u6b3e\uff08setter\uff09\n    void deposit(double amount) {\n        if (amount &gt; 0) {\n            balance += amount;\n        }\n    }\n\n    \/\/ \u53d6\u6b3e\uff08setter\uff09\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};<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.3 \u5bf9\u8c61\u5c01\u88c5\u5b9e\u4f8b<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>int main() {\n    BankAccount account(500.0);  \/\/ \u521d\u59cb\u5316\u8d26\u6237\u4f59\u989d\u4e3a 500\n\n    \/\/ \u83b7\u53d6\u4f59\u989d\n    cout &lt;&lt; \"Initial Balance: \" &lt;&lt; account.getBalance() &lt;&lt; endl;\n\n    \/\/ \u5b58\u6b3e\n    account.deposit(200.0);\n    cout &lt;&lt; \"Balance after deposit: \" &lt;&lt; account.getBalance() &lt;&lt; endl;\n\n    \/\/ \u53d6\u6b3e\n    account.withdraw(100.0);\n    cout &lt;&lt; \"Balance after withdrawal: \" &lt;&lt; account.getBalance() &lt;&lt; endl;\n\n    \/\/ \u65e0\u6548\u53d6\u6b3e\n    account.withdraw(700.0);  \/\/ \u63d0\u793a\u65e0\u6548\u53d6\u6b3e\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>Initial Balance: 500\nBalance after deposit: 700\nBalance after withdrawal: 600\nInvalid withdrawal amount!<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.4 \u6570\u636e\u5c01\u88c5\u7684\u597d\u5904<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u4fdd\u62a4\u6570\u636e<\/strong>\uff1a\u901a\u8fc7\u5c01\u88c5\uff0c\u6570\u636e\u53ea\u80fd\u901a\u8fc7\u516c\u5171\u65b9\u6cd5\u8fdb\u884c\u8bbf\u95ee\u548c\u4fee\u6539\uff0c\u4ece\u800c\u9632\u6b62\u5916\u90e8\u4ee3\u7801\u76f4\u63a5\u4fee\u6539\u6570\u636e\uff0c\u4fdd\u62a4\u4e86\u6570\u636e\u7684\u5b8c\u6574\u6027\u3002<\/li>\n\n\n\n<li><strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u5c01\u88c5\u5141\u8bb8\u5728\u7c7b\u5185\u90e8\u6539\u53d8\u6570\u636e\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u800c\u4e0d\u5f71\u54cd\u5916\u90e8\u4ee3\u7801\uff0c\u53ea\u8981\u63a5\u53e3\u4e0d\u53d8\uff0c\u5916\u90e8\u4ee3\u7801\u4f9d\u7136\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u3002<\/li>\n\n\n\n<li><strong>\u589e\u5f3a\u4ee3\u7801\u53ef\u7ef4\u62a4\u6027<\/strong>\uff1a\u5c01\u88c5\u964d\u4f4e\u4e86\u5916\u90e8\u4ee3\u7801\u4e0e\u7c7b\u5185\u90e8\u5b9e\u73b0\u4e4b\u95f4\u7684\u8026\u5408\u5ea6\u3002\u5373\u4f7f\u7c7b\u7684\u5185\u90e8\u5b9e\u73b0\u53d1\u751f\u53d8\u5316\uff0c\u5916\u90e8\u4ee3\u7801\u901a\u5e38\u65e0\u9700\u4fee\u6539\u3002<\/li>\n\n\n\n<li><strong>\u63d0\u9ad8\u4ee3\u7801\u5b89\u5168\u6027<\/strong>\uff1a\u901a\u8fc7\u63a7\u5236\u6570\u636e\u8bbf\u95ee\u6743\u9650\uff0c\u9632\u6b62\u975e\u6cd5\u7684\u64cd\u4f5c\uff0c\u907f\u514d\u6570\u636e\u88ab\u65e0\u610f\u95f4\u7834\u574f\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.5 C++ \u4e2d\u5c01\u88c5\u7684\u5b9e\u9645\u5e94\u7528<\/strong><\/h3>\n\n\n\n<p>\u5c01\u88c5\u662f\u6784\u5efa\u826f\u597d\u8f6f\u4ef6\u8bbe\u8ba1\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002\u901a\u8fc7\u5c01\u88c5\uff0cC++ \u7c7b\u53ef\u4ee5\u5b9e\u73b0\u4ee5\u4e0b\u529f\u80fd\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9690\u79c1\u4fdd\u62a4<\/strong>\uff1a\u901a\u8fc7\u79c1\u6709\u5316\u6570\u636e\u6210\u5458\uff0c\u53ef\u4ee5\u9632\u6b62\u5916\u90e8\u4e0d\u5f53\u4fee\u6539\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>\u63a5\u53e3\u7b80\u5316<\/strong>\uff1a\u5916\u90e8\u901a\u8fc7\u516c\u5171\u63a5\u53e3\u4e0e\u7c7b\u4ea4\u4e92\uff0c\u800c\u4e0d\u9700\u8981\u5173\u5fc3\u5176\u5185\u90e8\u5b9e\u73b0\u3002<\/li>\n\n\n\n<li><strong>\u7ef4\u62a4\u548c\u6269\u5c55\u6027<\/strong>\uff1a\u5c01\u88c5\u4f7f\u5f97\u7c7b\u7684\u5185\u90e8\u5b9e\u73b0\u53ef\u4ee5\u968f\u65f6\u4fee\u6539\u800c\u4e0d\u5f71\u54cd\u5916\u90e8\u4ee3\u7801\uff0c\u4fdd\u8bc1\u4e86\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p>\u6570\u636e\u5c01\u88c5\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\u7684\u6838\u5fc3\u6982\u5ff5\u4e4b\u4e00\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u5c06\u6570\u636e\u548c\u64cd\u4f5c\u6570\u636e\u7684\u884c\u4e3a\u6346\u7ed1\u5728\u4e00\u8d77\uff0c\u4ece\u800c\u4f7f\u5f97\u7c7b\u7684\u5b9e\u73b0\u7ec6\u8282\u5bf9\u5916\u90e8\u4ee3\u7801\u4e0d\u53ef\u89c1\u3002\u901a\u8fc7\u5c01\u88c5\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u63a7\u5236\u5bf9\u6570\u636e\u7684\u8bbf\u95ee\uff0c\u9632\u6b62\u5916\u90e8\u4e0d\u5f53\u4fee\u6539\u6570\u636e\uff0c\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u5b89\u5168\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u91cd\u7528\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6570\u636e\u5c01\u88c5\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\u4e2d\u7684\u4e00\u4e2a\u57fa\u672c\u6982\u5ff5\uff0c\u5b83\u901a\u8fc7\u5c06\u6570\u636e\u548c\u64cd\u4f5c\u6570\u636e\u7684\u51fd\u6570\uff08\u65b9\u6cd5\uff09\u6346\u7ed1\u5728\u4e00\u8d77\uff0c\u540c\u65f6\u9690\u85cf\u6570\u636e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3311,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[81],"tags":[],"class_list":["post-3310","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\/3310","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=3310"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3310\/revisions"}],"predecessor-version":[{"id":3312,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3310\/revisions\/3312"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3311"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}