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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # C++ 程序:使用運算符重載減去復數 > 原文: [https://www.programiz.com/cpp-programming/operator-overloading/binary-operator-overloading](https://www.programiz.com/cpp-programming/operator-overloading/binary-operator-overloading) #### 在此示例中,您將學習使用-運算符重載來減去復數。 要理解此示例,您應該了解以下 [C++ 編程](/cpp-programming "C++ tutorial")主題: * [C++ 類和對象](/cpp-programming/object-class) * [C++ 構造器](/cpp-programming/constructors) * [C++ 運算符重載](/cpp-programming/operator-overloading) * * * 由于-是二元運算符(對兩個操作數進行運算的運算符),因此應將其中一個操作數作為參數傳遞給運算符函數,其余過程類似于一元運算符的[重載](/cpp-programming/increment-decrement-operator-overloading "Unary Operator Overloading")。 * * * ## 示例:用于減去復數的二元運算符重載 ```cpp #include <iostream> using namespace std; class Complex { private: float real; float imag; public: Complex(): real(0), imag(0){ } void input() { cout << "Enter real and imaginary parts respectively: "; cin >> real; cin >> imag; } // Operator overloading Complex operator - (Complex c2) { Complex temp; temp.real = real - c2.real; temp.imag = imag - c2.imag; return temp; } void output() { if(imag < 0) cout << "Output Complex number: "<< real << imag << "i"; else cout << "Output Complex number: " << real << "+" << imag << "i"; } }; int main() { Complex c1, c2, result; cout<<"Enter first complex number:\n"; c1.input(); cout<<"Enter second complex number:\n"; c2.input(); // In case of operator overloading of binary operators in C++ programming, // the object on right hand side of operator is always assumed as argument by compiler. result = c1 - c2; result.output(); return 0; } ``` 在該程序中,創建了三個`Complex`類型的對象,并要求用戶輸入存儲在對象`c1`和`c2`中的兩個復數的實部和虛部。 然后執行語句`result = c1 -c 2`。 該語句調用運算符函數`Complex operator - (Complex c2)`。 執行`result = c1 - c2`時,會將`c2`作為參數傳遞給運算符。 **在 C++ 編程中二元運算符的運算符重載的情況下,編譯器始終將運算符右側的對象作為參數。** 然后,此函數將所得的復數(對象)返回到顯示在屏幕上的`main()`函數。 雖然,本教程包含`-`運算符的重載,但 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>

                              哎呀哎呀视频在线观看