<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # C++ 編程中的公共,受保護和私有繼承 > 原文: [https://www.programiz.com/cpp-programming/public-protected-private-inheritance](https://www.programiz.com/cpp-programming/public-protected-private-inheritance) #### 在本文中,您將學習在 C++ 中使用公共,保護和私有繼承。 您將通過示例學習在何處以及如何使用它。 您可以從具有不同訪問控制的基類中聲明派生的[類](/cpp-programming/object-class "C++ objects"),即公共[繼承](/cpp-programming/inheritance "C++ inheritance"),受保護的繼承或私有繼承。 ```cpp #include <iostream> using namespace std; class base { .... ... .... }; class derived : access_specifier base { .... ... .... }; ``` **注意**:可以使用`public`,`protected`或`private`關鍵字代替上述中使用的`access_specifier`術語。 ## C++ 中的公共繼承,保護繼承和私有繼承的示例 ```cpp class base { public: int x; protected: int y; private: int z; }; class publicDerived: public base { // x is public // y is protected // z is not accessible from publicDerived }; class protectedDerived: protected base { // x is protected // y is protected // z is not accessible from protectedDerived }; class privateDerived: private base { // x is private // y is private // z is not accessible from privateDerived } ``` 在上面的示例中,我們觀察到以下內容: * `base`具有三個成員變量:`x`,`y`和`z`,它們分別是`public`,`protected`和`private`成員。 * `publicDerived`繼承了變量`x`和`y`為公共變量和受保護變量。`z`不是繼承的,因為它是`base`的私有成員變量。 * `protectedDerived`繼承變量`x`和`y`。 這兩個變量都被保護。`z`不繼承 如果我們從`protectedDerived`派生一個類`derivedFromProtectedDerived`,則變量`x`和`y`也將繼承到派生類。 * `privateDerived`繼承變量`x`和`y`。 這兩個變量都變為私有。`z`不繼承 如果我們從`privateDerived`派生類`derivedFromPrivateDerived`,則變量`x`和`y`不被繼承,因為它們是`privateDerived`的私有變量。 * * * ### 公共繼承中的可見性 | 訪問性 | 私有變量 | 受保護變量 | 公共變量 | | --- | --- | --- | --- | | 可從自己的類訪問? | 是 | 是 | 是 | | 可從派生類訪問? | 否 | 是 | 是 | | 可從二階派生類訪問? | 否 | 是 | 是 | * * * ### 受保護繼承中的可見性 | 訪問性 | 私有變量 | 受保護變量 | 公共變量 | | --- | --- | --- | --- | | 可從自己的類訪問? | 是 | 是 | 是 | | 可從派生類訪問? | 否 | 是 | 是(繼承為受保護的變量) | | 可從二階派生類訪問? | 否 | 是 | 是 | * * * ### 私有繼承中的可見性 | 訪問性 | 私有變量 | 受保護變量 | 公共變量 | | --- | --- | --- | --- | | 可從自己的類訪問? | 是 | 是 | 是 | | 可從派生類訪問? | 否 | 是(繼承為私有變量) | 是(繼承為私有變量) | | 可從二階派生類訪問? | 否 | 否 | 否 |
                  <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>

                              哎呀哎呀视频在线观看