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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                >[info] # 函數的重載 ## **重載的需要性** 在不同類型上做類似運算而又用相同名字的情況稱為**重載 (function overloading)**,即重新定義語義. ~~~ //重載前: int abs(int); long fbs(long); double lbs(double); //重載后 int abs(int); long abs(long); double abs(double); ~~~ **重載解析**:C++用一種函數命名技術可以準確判斷應該調用哪個重載函數. ~~~ abs(10) //調用int abs(int) abs(100000000000) //調用long abs(long) abs(10.000000000) //調用double abs(double) ~~~ ## **使用說明** * 返回類型不能單獨作為重載解析的依據;參數個數,類型,順序三者種必須至少有一種不同. 如: void func(int); int func(int); //error * 不能用typedef定義的類型名來區分重載函數聲明中的參數. * 讓重載執行不同的功能,是不好的編程風格. ## **例子** ~~~ void print(double); void print(int); void func() { print(1); //匹配void print(int) printf(1.0); //匹配void print(double) print(’a’); //匹配void print(int) print(3.1415f); //匹配void print(double) } ~~~ ~~~ /* ***求3個數中最大的數(分別考慮整數,雙精度數,長整數的情況)*** */ #include <iostream> using namespace std; int main( ) { int max(int a,int b,int c); //函數聲明 double max(double a,double b,double c); //函數聲明 long max(long a,long b,long c); //函數聲明 int i1,i2,i3,i; cin>>i1>>i2>>i3; //輸入3個整數 i=max(i1,i2,i3); //求3個整數中的最大者 cout<<”i_max=”<<i<<endl; double d1,d2,d3,d; cin>>d1>>d2>>d3; //輸入3個雙精度數 d=max(d1,d2,d3); //求3個雙精度數中的最大者 cout<<”d_max=”<<d<<endl; long g1,g2,g3,g; cin>>g1>>g2>>g3; //輸入3個長整數 g=max(g1,g2,g3); //求3個長整數中的最大者 cout<<”g_max=”<<g<<endl; } int max(int a,int b,int c) //定義求3個整數中的最大者的函數 { if(b>a) a=b; if(c>a) a=c; return a; } double max(double a,double b,double c) //定義求3個雙精度數中的最大者的函數 { if(b>a) a=b; if(c>a) a=c; return a; } long max(long a,long b,long c) //定義求3個長整數中的最大者的函數 { if(b>a) a=b; if(c>a) a=c; return a; } ~~~ ~~~ /* ***求兩個整數或3個整數中的最大數*** */ #include <iostream> using namespace std; int main( ) { int max(int a,int b,int c); //函數聲明 int max(int a,int b); //函數聲明 int a=8,b=-12,c=27; cout<<”max(a,b,c)=”<<max(a,b,c)<<endl; //輸出3個整數中的最大者 cout<<”max(a,b)=”<<max(a,b)<<endl; //輸出兩個整數中的最大者 } int max(int a,int b,int c) //此max函數的作用是求3個整數中的最大者 { if(b>a) a=b; if(c>a) a=c; return a; } int max(int a,int b) //此max函數的作用是求兩個整數中的最大者 { if(a>b) return a; else return b; } ~~~
                  <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>

                              哎呀哎呀视频在线观看