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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # C++ 中的構造函數 > 原文: [https://beginnersbook.com/2017/08/cpp-constructors/](https://beginnersbook.com/2017/08/cpp-constructors/) 構造函數是初始化類對象的類的特殊成員函數。構造函數名稱與類名稱相同,并且沒有返回類型。讓我們舉一個簡單的例子來理解構造函數的工作原理。 ## 簡單示例:如何在 C++ 中使用構造函數 閱讀以下程序中的注釋,以了解該程序的每個部分。 ```cpp #include <iostream> using namespace std; class constructorDemo{ public: int num; char ch; /* This is a default constructor of the * class, do note that it's name is same as * class name and it doesn't have return type. */ constructorDemo() { num = 100; ch = 'A'; } }; int main(){ /* This is how we create the object of class, * I have given the object name as obj, you can * give any name, just remember the syntax: * class_name object_name; */ constructorDemo obj; /* This is how we access data members using object * we are just checking that the value we have * initialized in constructor are reflecting or not. */ cout<<"num: "<<obj.num<<endl; cout<<"ch: "<<obj.ch; return 0; } ``` **輸出:** ```cpp num: 100 ch: A ``` ## 構造函數與成員函數 現在我們知道什么是構造函數,讓我們討論構造函數與類的成員函數的不同之處。 1)構造函數沒有返回類型。成員函數具有返回類型。 2)當我們創建類的對象時,會自動調用構造函數。需要使用類的對象顯式調用成員函數。 3)當我們不在我們的類中創建任何構造函數時,C++ 編譯器生成一個默認構造函數并將其插入到我們的代碼中。這同樣適用于成員函數。 這是編譯器生成的默認構造函數的外觀: ```cpp class XYZ { .... XYZ() { //Empty no code } }; ``` ## C++ 中構造函數的類型 C++ 中有兩種類型的構造函數。 1)默認構造函數 2)參數化構造函數 ### 1)默認構造函數 默認構造函數沒有任何參數(或參數)。 ```cpp #include <iostream> using namespace std; class Website{ public: //Default constructor Website() { cout<<"Welcome to BeginnersBook"<<endl; } }; int main(void){ /*creating two objects of class Website. * This means that the default constructor * should have been invoked twice. */ Website obj1; Website obj2; return 0; } ``` **輸出:** ```cpp Welcome to BeginnersBook Welcome to BeginnersBook ``` > 如果未在類中指定任何構造函數,則編譯器將在代碼中插入沒有代碼(空體)的默認構造函數。 ### 2)參數化構造函數 帶參數的構造函數稱為參數化構造函數。這些類型的構造函數允許我們在創建對象時傳遞參數。讓我們看看他們的樣子: 讓我們假設類名是`XYZ`。 **默認構造函數:** ```cpp XYZ() { } .... XYZ obj; .... ``` **參數化構造函數:** ```cpp XYZ(int a, int b) { } ... XYZ obj(10, 20); ``` #### 例: ```cpp #include <iostream> using namespace std; class Add{ public: //Parameterized constructor Add(int num1, int num2) { cout<<(num1+num2)<<endl; } }; int main(void){ /* One way of creating object. Also * known as implicit call to the * constructor */ Add obj1(10, 20); /* Another way of creating object. This * is known as explicit calling the * constructor. */ Add obj2 = Add(50, 60); return 0; } ``` **輸出:** ```cpp 30 110 ```
                  <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>

                              哎呀哎呀视频在线观看