<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 功能強大 支持多語言、二開方便! 廣告
                # 運算符重載的規則 * 只能對C++已有的運算符進行重載,不能定義新的運算符 * 重載的運算符的作用的操作數至少有一個對象 * 不能改變運算符的運算對象的個數 * 不能改變運算符的優先級 * 不能改變運算符的結合性 * 重載運算符的函數不能有默認參數 * 當函數的參數列表中有兩個參數時,一般重載為友元形式 * 賦值運算符,下表運算符,函數調用運算符必須定義為函數的成員函數 * 流插入運算符,流提取運算符,類型轉換運算符不能定義為類的成員函數 # 定義方式 ~~~ T operator 操作符(形參列表) ? ?//T代表返回值的類型 { ? ?··· ?//函數體 } ~~~ # 允許重載的運算符 ![image-20200607000238121](file://C:/Users/28165/AppData/Roaming/Typora/typora-user-images/image-20200607000238121.png?lastModify=1591534259) # 不允許重載的運算符 | . | 成員訪問運算符 | | --- | --- | | .\* | 成員指針訪問運算符 | | :: | 域運算符 | | sizeof | 長度運算符 | | ?: | 條件運算符 | # 例子 ~~~ #include <iostream> using namespace std; class String { ? ?public: String( ){p=NULL;} ? ? ? ? ? ? ? ? ? ? ? ? ? ? //默認構造函數 ? String(char *str); ? ? ? ? ? ? ? ? ? ? ? ? ? ?//構造函數 ? friend bool operator> (String &string1, String &string2); ? friend bool operator< (String &string1, String &string2); ? friend bool operator==(String &string1, String & string2); ? void display( ); ? ?private: ? char *p; ? ? ? ? ? ? ? ? ? ? ? ? ? //字符型指針,用于指向字符串 }; ? String∷String(char *str) ? ? ? ? ? ? //定義構造函數 { ? ?p=str; } ? ? ? ? ? ? ? ? ? ? ? ? ?//使p指向實參字符串 ? void String∷display( ) ? //輸出p所指向的字符串 { ? ?cout<<p; } bool operator>(String &string1,String &string2) ? ? ? ? ?//對運算符“>”重載 { ? ?if(strcmp(string1.p,string2.p)>0) ? ? ? ?return true; ? ?else ? ? ? ?return false; } bool operator<(String &string1,String &string2) ? ? ? ? ?//對運算符“<”重載 { ? ?if(strcmp(string1.p,string2.p)<0) ? ? ? ?return true; ? ?else ? ? ? ?return false; } bool operator==(String &string1,String&string2) //對運算符“==”重載 { ? ?if(strcmp(string1.p,string2.p)==0) ? ? ? ?return true; ? ?else ? ? ? ?return false; } void compare(String &string1,String &string2) { ? ?if(operator>(string1,string2)==1) ? { ? ? ? ?string1.display( ); ? ? ? ?cout<<″>″; ? ? ? ?string2.display( ); ? } ? ?else ? ? ? ?if(operator<(string1,string2)==1) ? ? ? { ? ? ? ? ? ?string1.display( );cout<<″<″;string2.display( ); ? ? ? } ? ?else if(operator==(string1,string2)==1) ? { ? ? ? ?string1.display( );cout<<″=″;string2.display( ); ? } ? ?cout<<endl; } int main( ) { ? ?String string1(″Hello″),string2(″Book″),string3(″Computer″),string4(″Hello″); ? ?compare(string1,string2); ? ?compare(string2,string3); ? ?compare(string1,string4); ? ?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>

                              哎呀哎呀视频在线观看