<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++ 中的函數傳遞和返回對象 > 原文: [https://beginnersbook.com/2017/09/cpp-pass-and-return-object-from-a-function/](https://beginnersbook.com/2017/09/cpp-pass-and-return-object-from-a-function/) 在本教程中,我們將了解如何將對象作為參數傳遞給函數以及如何從函數返回對象。 ## 將對象傳遞給函數 可以將對象傳遞給函數,就像我們[將結構傳遞給函數](https://beginnersbook.com/2017/09/cpp-structure-and-function/)一樣。在`A`類中,我們有一個函數`disp()`,我們在其中傳遞類`A`的對象。類似地,我們可以將一個類的對象傳遞給不同類的函數。 ```cpp #include <iostream> using namespace std; class A { public: int n=100; char ch='A'; void disp(A a){ cout<<a.n<<endl; cout<<a.ch<<endl; } }; int main() { A obj; obj.disp(obj); return 0; } ``` **輸出:** ```cpp 100 A ``` ## 從函數返回對象 在這個例子中,我們有兩個函數,函數`input()`返回`Student`對象,`disp()`將`Student`對象作為參數。 ```cpp #include <iostream> using namespace std; class Student { public: int stuId; int stuAge; string stuName; /* In this function we are returning the * Student object. */ Student input(int n, int a, string s){ Student obj; obj.stuId = n; obj.stuAge = a; obj.stuName = s; return obj; } /* In this function we are passing object * as an argument. */ void disp(Student obj){ cout<<"Name: "<<obj.stuName<<endl; cout<<"Id: "<<obj.stuId<<endl; cout<<"Age: "<<obj.stuAge<<endl; } }; int main() { Student s; s = s.input(1001, 29, "Negan"); s.disp(s); return 0; } ``` **輸出:** ```cpp Name: Negan Id: 1001 Age: 29 ```
                  <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>

                              哎呀哎呀视频在线观看