<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++ 重載運算符和重載函數 C++ 允許在同一作用域中的某個**函數**和**運算符**指定多個定義,分別稱為**函數重載**和**運算符重載**。 重載聲明是指一個與之前已經在該作用域內聲明過的函數或方法具有相同名稱的聲明,但是它們的參數列表和定義(實現)不相同。 當您調用一個**重載函數**或**重載運算符**時,編譯器通過把您所使用的參數類型與定義中的參數類型進行比較,決定選用最合適的定義。選擇最合適的重載函數或重載運算符的過程,稱為**重載決策**。 ## C++ 中的函數重載 在同一個作用域內,可以聲明幾個功能類似的同名函數,但是這些同名函數的形式參數(指參數的個數、類型或者順序)必須不同。您不能僅通過返回類型的不同來重載函數。 下面的實例中,同名函數 **print()** 被用于輸出不同的數據類型: ``` #include <iostream> using namespace std; class printData { public: void print(int i) { cout << "Printing int: " << i << endl; } void print(double f) { cout << "Printing float: " << f << endl; } void print(char* c) { cout << "Printing character: " << c << endl; } }; int main(void) { printData pd; // Call print to print integer pd.print(5); // Call print to print float pd.print(500.263); // Call print to print character pd.print("Hello C++"); return 0; } ``` 當上面的代碼被編譯和執行時,它會產生下列結果: ``` Printing int: 5 Printing float: 500.263 Printing character: Hello C++ ``` ## C++ 中的運算符重載 您可以重定義或重載大部分 C++ 內置的運算符。這樣,您就能使用自定義類型的運算符。 重載的運算符是帶有特殊名稱的函數,函數名是由關鍵字 operator 和其后要重載的運算符符號構成的。與其他函數一樣,重載運算符有一個返回類型和一個參數列表。 ``` Box operator+(const Box&); ``` 聲明加法運算符用于把兩個 Box 對象相加,返回最終的 Box 對象。大多數的重載運算符可被定義為普通的非成員函數或這被定義為類成員函數。如果我們定義上面的函數為類的非成員函數,那么我們需要為每次操作傳遞兩個參數,如下所示: ``` Box operator+(const Box&, const Box&); ``` 下面的實例使用成員函數演示了運算符重載的概念。在這里,對象作為參數進行傳遞,對象的屬性使用 **this** 運算符進行訪問,如下所示: ``` #include <iostream> using namespace std; class Box { public: double getVolume(void) { return length * breadth * height; } void setLength( double len ) { length = len; } void setBreadth( double bre ) { breadth = bre; } void setHeight( double hei ) { height = hei; } // 重載 + 運算符,用于把兩個 Box 對象相加 Box operator+(const Box& b) { Box box; box.length = this->length + b.length; box.breadth = this->breadth + b.breadth; box.height = this->height + b.height; return box; } private: double length; // 長度 double breadth; // 寬度 double height; // 高度 }; // 程序的主函數 int main( ) { Box Box1; // 聲明 Box1,類型為 Box Box Box2; // 聲明 Box2,類型為 Box Box Box3; // 聲明 Box3,類型為 Box double volume = 0.0; // 把體積存儲在該變量中 // Box1 詳述 Box1.setLength(6.0); Box1.setBreadth(7.0); Box1.setHeight(5.0); // Box2 詳述 Box2.setLength(12.0); Box2.setBreadth(13.0); Box2.setHeight(10.0); // Box1 的體積 volume = Box1.getVolume(); cout << "Volume of Box1 : " << volume <<endl; // Box2 的體積 volume = Box2.getVolume(); cout << "Volume of Box2 : " << volume <<endl; // 把兩個對象相加,得到 Box3 Box3 = Box1 + Box2; // Box3 的體積 volume = Box3.getVolume(); cout << "Volume of Box3 : " << volume <<endl; return 0; } ``` 當上面的代碼被編譯和執行時,它會產生下列結果: ``` Volume of Box1 : 210 Volume of Box2 : 1560 Volume of Box3 : 5400 ``` ## 可重載運算符/不可重載運算符 下面是可重載的運算符列表: | + | - | * | / | % | ^ | |:--- |:--- |:--- |:--- | | & | &#124; | ~ | ! | , | = | | &lt; | &gt; | &lt;= | &gt;= | ++ | -- | | &lt;&lt; | &gt;&gt; | == | != | && | &#124;&#124; | | += | -= | /= | %= | ^= | &= | | &#124;= | *= | &lt;&lt;= | &gt;&gt;= | [] | () | | -&gt; | -&gt;* | new | new [] | delete | delete [] | 下面是不可重載的運算符列表: | :: | .* | . | ?: | |:--- |:--- |:--- |:--- | ## 運算符重載實例 下面提供了各種運算符重載的實例,幫助您更好地理解重載的概念。 | 序號 | 運算符和實例 | | --- | --- | | 1 | [一元運算符重載](/cplusplus/unary-operators-overloading.html) | | 2 | [二元運算符重載](/cplusplus/binary-operators-overloading.html) | | 3 | [關系運算符重載](/cplusplus/relational-operators-overloading.html) | | 4 | [輸入/輸出運算符重載](/cplusplus/input-output-operators-overloading.html) | | 5 | [++ 和 -- 運算符重載](/cplusplus/increment-decrement-operators-overloading.html) | | 6 | [賦值運算符重載](/cplusplus/assignment-operators-overloading.html) | | 7 | [函數調用運算符 () 重載](/cplusplus/function-call-operator-overloading.html) | | 8 | [下標運算符 [] 重載](/cplusplus/subscripting-operator-overloading.html) | | 9 | [類成員訪問運算符 -&gt; 重載](/cplusplus/class-member-access-operator-overloading.html) |
                  <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>

                              哎呀哎呀视频在线观看