<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://www.programiz.com/cpp-programming/function-overloading](https://www.programiz.com/cpp-programming/function-overloading) #### 在本教程中,我們將通過示例學習 C++ 中的函數重載。 在 C++ 中,如果傳遞的參數的數量和/或類型不同,則兩個函數可以具有相同的名稱。 這些具有相同名稱但參數不同的函數稱為重載函數。 例如: ```cpp // same number different arguments int test() { } int test(int a) { } float test(double a) { } int test(int a, double b) { } ``` 在此,所有 4 個函數均為重載函數。 請注意,所有這四個函數的返回類型都不相同。 重載的函數可以具有或可以不具有不同的返回類型,但是它們必須具有不同的參數。 例如, ```cpp // Error code int test(int a) { } double test(int b){ } ``` 在這里,兩個函數具有相同的名稱,相同的類型和相同數量的參數。 因此,編譯器將引發錯誤。 * * * ## 帶有不同類型參數的函數重載 ```cpp // Program to compute absolute value // Works for both int and float #include <iostream> using namespace std; // function with float type parameter float absolute(float var){ if (var < 0.0) var = -var; return var; } // function with int type parameter int absolute(int var) { if (var < 0) var = -var; return var; } int main() { // call function with int type parameter cout << "Absolute value of -5 = " << absolute(-5) << endl; // call function with float type parameter cout << "Absolute value of 5.5 = " << absolute(5.5f) << endl; return 0; } ``` **輸出** ```cpp Absolute value of -5 = 5 Absolute value of 5.5 = 5.5 ``` ![Working of C++ Function Overloading for absolute()](https://img.kancloud.cn/12/b1/12b166fd5c1639a35461ea533abf1df9_938x774.png "Working of C++ Function Overloading for absolute()") `absolute()`函數重載的原理 在此程序中,我們重載了`absolute()`函數。 根據函數調用期間傳遞的參數類型,將調用相應的函數。 * * * ## 帶有不同數量的參數的函數重載 ```cpp #include <iostream> using namespace std; // function with 2 parameters void display(int var1, double var2) { cout << "Integer number: " << var1; cout << " and double number: " << var2 << endl; } // function with double type single parameter void display(double var) { cout << "Double number: " << var << endl; } // function with int type single parameter void display(int var) { cout << "Integer number: " << var << endl; } int main() { int a = 5; double b = 5.5; // call function with int type parameter display(a); // call function with double type parameter display(b); // call function with 2 parameters display(a, b); return 0; } ``` **輸出** ```cpp Integer number: 5 Float number: 5.5 Integer number: 5 and double number: 5.5 ``` 在此,`display()`函數使用不同的參數調用了三次。 根據傳遞的參數的數量和類型,調用相應的`display()`函數。 ![Working of C++ Function Overloading for display()](https://img.kancloud.cn/b5/1f/b51f318e6de2d39d68a009222f8c303b_994x1092.png "Working of C++ Function Overloading for display()") `display()`函數重載的原理 所有這些函數的返回類型都相同,但函數重載不必如此。 * * * **注意**:在 C++ 中,許多標準庫函數都已重載。 例如,`sqrt()`函數可以將`double`,`float`,`int,`等作為參數。 這是可能的,因為`sqrt()`函數在 C++ 中已重載。
                  <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>

                              哎呀哎呀视频在线观看