<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # C++ 封裝 > 原文: [https://beginnersbook.com/2017/09/cpp-encapsulation/](https://beginnersbook.com/2017/09/cpp-encapsulation/) 封裝是將數據成員和函數組合在一個稱為類的單個單元中的過程。這是為了防止直接訪問數據,通過類的函數提供對它們的訪問。它是[面向對象編程(OOP)](https://beginnersbook.com/2017/08/cpp-oops-concepts/)的流行特性之一,它有助于**數據隱藏**。 ## 如何在類上實現封裝 為此: 1)將所有數據成員設為私有。 2)為每個數據成員創建公共設置器和獲取器函數,使設置器設置數據成員的值,獲取器獲取數據成員的值。 讓我們在一個示例程序中看到這個: ## C++ 中的封裝示例 這里我們有兩個數據成員`num`和`ch`,我們已將它們聲明為私有,因此它們在類外無法訪問,這樣我們就隱藏了數據。獲取和設置這些數據成員的值的唯一方法是通過公共設置器和獲取器函數。 ```cpp #include<iostream> using namespace std; class ExampleEncap{ private: /* Since we have marked these data members private, * any entity outside this class cannot access these * data members directly, they have to use getter and * setter functions. */ int num; char ch; public: /* Getter functions to get the value of data members. * Since these functions are public, they can be accessed * outside the class, thus provide the access to data members * through them */ int getNum() const { return num; } char getCh() const { return ch; } /* Setter functions, they are called for assigning the values * to the private data members. */ void setNum(int num) { this->num = num; } void setCh(char ch) { this->ch = ch; } }; int main(){ ExampleEncap obj; obj.setNum(100); obj.setCh('A'); cout<<obj.getNum()<<endl; cout<<obj.getCh()<<endl; return 0; } ``` **輸出:** ```cpp 100 A ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看