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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 靜態數據成員 ## 特點 * 靜態數據成員是被該類的所有成員共享的,不屬于某個對象 * 為對象創建空間時,靜態數據成員分配空間在數據區,其他成員分配在棧區 * 分配時機提前,類定義好了,該靜態數據成員就分配好了 * 靜態數據成員在類體外初始化,函數體之外初始化 ## 聲明 static 類型 名稱 ## 例子 ~~~ #include <iostream> using namespace std; ? class Box { public: ? Box(int,int); ? int volume( ); ? static int height; ? ? ? ? ? ? ? //把height定義為公用的靜態的數據成員 ? int width; ? int length; }; ? Box∷Box(int w,int len) ? ? ? ? //通過構造函數對width和length賦初值 { width=w; ? length=len; } ? int Box∷volume( ) { return(height*width*length); } ? int Box∷height=10; //對靜態數據成員height初始化 ? int main( ) { Box a(15,20),b(20,30); ? cout<<a.height<<endl; ? ? ? ? ? //通過對象名a引用靜態數據成員 ? cout<<b.height<<endl; ? ? ? ? ? //通過對象名b引用靜態數據成員 ? cout<<Box∷height<<endl; ? ? ? ? //通過類名引用靜態數據成員 ? cout<<a.volume( )<<endl; ? ? ? ? //調用volume函數,計算體積,輸出結果 } ~~~ # 靜態成員函數 ## 聲明 static 類型 函數名(); 為了更方便的訪問靜態數據成員 ## 例子 ~~~ include <iostream> using namespace std; ? class Student ? ? ? ? ? ? ? ? ? //定義Student類 { public: ? Student(int n,int a,float s):num(n),age(a),score(s){ }//定義構造函數 ? ? ? void total( ); ? ? ? static float average( ); ? ? //聲明靜態成員函數 ? private: ? int num; ? int age; ? float score; ? static float sum; ? ? ? ? ? //靜態數據成員 ? static int count; ? ? ? ? ? //靜態數據成員 }; ? void Student∷total( ) //定義非靜態成員函數 { sum+=score; ? ? ? ? ? ? ? ? ? ? ? ? ? //累加總分 ? count++; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //累計已統計的人數 } ? float Student∷average( ) //定義靜態成員函數 { return(sum/count); } ? float Student∷sum=0; ? ? ? ? //對靜態數據成員初始化 int Student∷count=0; ? ? ? //對靜態數據成員初始化 ? int main( ) { Student stud[3]={ ? ? ? //定義對象數組并初始化 ? Student(1001,18,70), ? Student(1002,19,78), ? Student(1005,20,98) }; int n; ? cout<<″please input the number of students:″; ? cin>>n; ? ? //輸入需要求前面多少名學生的平均成績 ? for(int i=0;i<n;i++) //調用3次total函數 ? stud[i].total( ); ? cout<<″the average score of ″<<n ? <<″ students is ″ ? ? ? <<Student∷average( ) //調用靜態成員函數 ? ? ? <<endl; ? 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>

                              哎呀哎呀视频在线观看