<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國際加速解決方案。 廣告
                #### 25. 數據封裝 所有的 C++ 程序都有以下兩個基本要素: * \*\*程序語句(代碼):\*\*這是程序中執行動作的部分,它們被稱為函數。 * \*\*程序數據:\*\*數據是程序的信息,會受到程序函數的影響。 封裝是面向對象編程中的把數據和操作數據的函數綁定在一起的一個概念,這樣能避免受到外界的干擾和誤用,從而確保了安全。數據封裝引申出了另一個重要的 OOP 概念,即**數據隱藏**。 **數據封裝**是一種把數據和操作數據的函數捆綁在一起的機制,**數據抽象**是一種僅向用戶暴露接口而把具體的實現細節隱藏起來的機制。 C++ 通過創建**類**來支持封裝和數據隱藏(public、protected、private)。我們已經知道,類包含私有成員(private)、保護成員(protected)和公有成員(public)成員。默認情況下,在類中定義的所有項目都是私有的。例如: ~~~ class Box { public: double getVolume(void) { return length * breadth * height; } private: double length; // 長度 double breadth; // 寬度 double height; // 高度 }; 復制代碼 ~~~ 變量 length、breadth 和 height 都是私有的(private)。這意味著它們只能被 Box 類中的其他成員訪問,而不能被程序中其他部分訪問。這是實現封裝的一種方式。 為了使類中的成員變成公有的(即,程序中的其他部分也能訪問),必須在這些成員前使用 **public** 關鍵字進行聲明。所有定義在 public 標識符后邊的變量或函數可以被程序中所有其他的函數訪問。 把一個類定義為另一個類的友元類,會暴露實現細節,從而降低了封裝性。理想的做法是盡可能地對外隱藏每個類的實現細節。 ~~~ //1. .h class Box { public: double getVolume(void) { return length * breadth * height; } private: double length; // 長度 double breadth; // 寬度 double height; // 高度 }; //2. .cpp double Box::getVolume() { return this->length * this->height * this->width; } void Box::initValue() { //默認 int a = 10, b = 20, c = 30; length = a, width = b, height = c; } //3. cpp 測試 void test23() { Box box; box.initValue(); cout << "數據私有封住:" << box.getVolume() << endl; } 復制代碼 ~~~ > **輸出:** > > 數據私有封住:6000
                  <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>

                              哎呀哎呀视频在线观看