<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/pass-return-object-function](https://www.programiz.com/cpp-programming/pass-return-object-function) #### 在本文中,您將學習在 C++ 編程中將對象傳遞給函數并從函數返回對象。 在 C++ 編程中,[對象可以與結構](/cpp-programming/structure-function)相似的方式傳遞給函數。 * * * ## 如何將對象傳遞給函數? ![Pass Object to a function in C++](https://img.kancloud.cn/eb/4b/eb4be77a11ccd185c15be162ad9c6b97_464x276.png) * * * ## 示例 1:將對象傳遞給函數 **C++ 程序,通過將對象傳遞給函數來加兩個復數。** ```cpp #include <iostream> using namespace std; class Complex { private: int real; int imag; public: Complex(): real(0), imag(0) { } void readData() { cout << "Enter real and imaginary number respectively:"<<endl; cin >> real >> imag; } void addComplexNumbers(Complex comp1, Complex comp2) { // real represents the real data of object c3 because this function is called using code c3.add(c1,c2); real=comp1.real+comp2.real; // imag represents the imag data of object c3 because this function is called using code c3.add(c1,c2); imag=comp1.imag+comp2.imag; } void displaySum() { cout << "Sum = " << real<< "+" << imag << "i"; } }; int main() { Complex c1,c2,c3; c1.readData(); c2.readData(); c3.addComplexNumbers(c1, c2); c3.displaySum(); return 0; } ``` **輸出** ```cpp Enter real and imaginary number respectively: 2 4 Enter real and imaginary number respectively: -3 4 Sum = -1+8i ``` * * * ## 如何從函數返回對象? 在 C++ 編程中,可以通過與結構類似的方式從函數中返回[對象](/cpp-programming/structure-function)。 ![Return object from a function in C++](https://img.kancloud.cn/4c/22/4c224aa5bb91c6e50c8b95a310d5f811_407x296.png) * * * ## 示例 2:從函數傳遞和返回對象 在該程序中,復數(對象)的總和返回到`main()`函數并顯示。 ```cpp #include <iostream> using namespace std; class Complex { private: int real; int imag; public: Complex(): real(0), imag(0) { } void readData() { cout << "Enter real and imaginary number respectively:"<<endl; cin >> real >> imag; } Complex addComplexNumbers(Complex comp2) { Complex temp; // real represents the real data of object c3 because this function is called using code c3.add(c1,c2); temp.real = real+comp2.real; // imag represents the imag data of object c3 because this function is called using code c3.add(c1,c2); temp.imag = imag+comp2.imag; return temp; } void displayData() { cout << "Sum = " << real << "+" << imag << "i"; } }; int main() { Complex c1, c2, c3; c1.readData(); c2.readData(); c3 = c1.addComplexNumbers(c2); c3.displayData(); return 0; } ```
                  <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>

                              哎呀哎呀视频在线观看