<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/constructors](https://www.programiz.com/cpp-programming/constructors) #### 在本文中,您將學習 C++ 中的構造器。 您將學習什么是構造器,如何創建它以及 C++ 中的構造器類型。 構造器是成員函數的一種特殊類型,它在創建[對象](/cpp-programming/object-class "C++ objects")時自動對其進行初始化。 編譯器通過其名稱和返回類型將給定的成員函數標識為構造器。 構造器與類具有相同的名稱,并且沒有任何返回類型。 同樣,構造器始終是公共的。 ```cpp ... .. ... class temporary { private: int x; float y; public: // Constructor temporary(): x(5), y(5.5) { // Body of constructor } ... .. ... }; int main() { Temporary t1; ... .. ... } ``` 上面的程序顯示了定義的構造器,沒有返回類型,并且名稱與類相同。 * * * ## 構造器如何工作? 在上面的偽代碼中,`temporary()`是構造器。 創建類`temporary`的對象時,將自動調用構造器,并且`x`初始化為 5,`y`初始化為 5.5。 您還可以按如下所示初始化構造器體內的數據成員。 但是,這種方法不是優選的。 ```cpp temporary() { x = 5; y = 5.5; } // This method is not preferred. ``` * * * ## 在 C++ 中使用構造器 假設您正在處理 100 個`Person`對象,并且數據成員`age`的默認值為 0。手動初始化所??有對象將是一項非常繁瑣的任務。 相反,您可以定義一個將`age`初始化為 0 的構造器。然后,您要做的就是創建`Person`對象,該構造器將自動初始化`age`。 這些情況在處理對象數組時經常出現。 另外,如果要在創建對象后立即執行一些代碼,可以將代碼放在構造器的主體內。 * * * ### 示例 1:C++ 中的構造器 **計算并顯示矩形的面積。** ```cpp #include <iostream> using namespace std; class Area { private: int length; int breadth; public: // Constructor Area(): length(5), breadth(2){ } void GetLength() { cout << "Enter length and breadth respectively: "; cin >> length >> breadth; } int AreaCalculation() { return (length * breadth); } void DisplayArea(int temp) { cout << "Area: " << temp; } }; int main() { Area A1, A2; int temp; A1.GetLength(); temp = A1.AreaCalculation(); A1.DisplayArea(temp); cout << endl << "Default Area when value is not taken from user" << endl; temp = A2.AreaCalculation(); A2.DisplayArea(temp); return 0; } ``` 在該程序中,創建了`Area`類來處理與區域相關的功能。 它具有兩個數據成員`length`和`width`。 定義了一個構造器,該構造器將`length`初始化為 5,將`width`初始化為 2。 我們還具有三個附加的成員函數`GetLength(), AreaCalculation() and DisplayArea()`,以從用戶處獲取長度,計算面積并分別顯示面積。 當創建對象`A1`和`A2`時,由于構造器的緣故,兩個對象的`length`和`width`分別初始化為 5 和 2。 然后,調用成員函數`GetLength()`,該函數從對象獲取對象`A1`的`length`和`width`的值。 這改變了對象`A1`的長度和寬度。 然后,通過調用`AreaCalculation()`函數計算對象`A1`的區域并將其存儲在變量`temp`中,最后將其顯示。 對于對象`A2`,沒有要求用戶提供任何數據。 因此,`length`和`width`分別保持為 5 和 2。 然后,計算并顯示`A2`的面積為 10。 **輸出** ```cpp Enter length and breadth respectively: 6 7 Area: 42 Default Area when value is not taken from user Area: 10 ``` * * * ## 構造器重載 構造器可以類似于[函數重載](/cpp-programming/function-overloading "C++ Function overloading")的方式重載。 重載的構造器具有相同的名稱(類的名稱),但參數數量不同。 根據傳遞的參數的數量和類型,將調用特定的構造器。 由于存在多個構造器,因此在創建對象時也應傳遞該構造器的參數。 * * * ## 示例 2:構造器重載 ```cpp // Source Code to demonstrate the working of overloaded constructors #include <iostream> using namespace std; class Area { private: int length; int breadth; public: // Constructor with no arguments Area(): length(5), breadth(2) { } // Constructor with two arguments Area(int l, int b): length(l), breadth(b){ } void GetLength() { cout << "Enter length and breadth respectively: "; cin >> length >> breadth; } int AreaCalculation() { return length * breadth; } void DisplayArea(int temp) { cout << "Area: " << temp << endl; } }; int main() { Area A1, A2(2, 1); int temp; cout << "Default Area when no argument is passed." << endl; temp = A1.AreaCalculation(); A1.DisplayArea(temp); cout << "Area when (2,1) is passed as argument." << endl; temp = A2.AreaCalculation(); A2.DisplayArea(temp); return 0; } ``` 對于對象`A1`,在創建對象時不傳遞任何參數。 因此,將調用不帶參數的構造器,該構造器會將`length`初始化為 5,將`width`初始化為 2。因此,對象`A1`的面積將為 10。 對于對象`A2`,在創建對象時將 2 和 1 作為參數傳遞。 因此,將調用具有兩個參數的構造器,該構造器將`length`初始化為`l`(在這種情況下為 2),并將`breadth`初始化為`b`(1 在這種情況下)。 因此,對象`A2`的面積將為 2。 **輸出** ```cpp Default Area when no argument is passed. Area: 10 Area when (2,1) is passed as argument. Area: 2 ``` * * * ## 默認復制構造器 一個對象可以用另一個相同類型的對象初始化。 這與將一個類的內容復制到另一個類相同。 在上述程序中,如果要初始化對象`A3`使其包含與`A2`相同的值,則可以執行以下操作: ```cpp .... int main() { Area A1, A2(2, 1); // Copies the content of A2 to A3 Area A3(A2); OR, Area A3 = A2; } ``` 您可能會認為,您需要創建一個新的構造器來執行此任務。 但是,不需要其他構造器。 這是因為默認情況下,復制構造器已內置到所有類中。
                  <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>

                              哎呀哎呀视频在线观看