C++day12 學習筆記

來源:互聯網
上載者:User

1、拷貝建構函式和運算子多載
  (1)當類的成員變數中出現指標類型的時候,需要動態申請空間,這樣就需要解決淺拷貝的問題
       在聲明對象的同時用另一個對象為其賦值,會調用拷貝建構函式。
       系統提供的預設拷貝建構函式,是淺拷貝,我們可以自己寫一個拷貝建構函式,把指標指向的變數也拷貝過去
  (2)類中的成員變數出現指標類型,當兩個對象都建立出來了以後,相互賦值的時候,就需要重載賦值運算子號
       手工為指標指向的變數賦值

2、其他的運算子號重載
   對於對象之間的加減操作,系統是不允許的,但通過自己的運算子多載,按照自己的規則,實現對象之間的運算操作。

   Integer operator+(const Integer& i){          int t = *p + *(i.p);          Integer temp(t);          return temp;   }

   (1)自增運算子
      前++是左值,返回引用
      後++是右值,返回臨時值
     
      "++"運算子的優先順序比"+"高
     
      Integer& operator++(){}
      Integer operator++(int i){}
      int i是用於區別前++和後++的,是沒有實際意義的參數,稱為啞元,必須是int類型
     
      前++和後++的操作,主要區別就是返回的值不同,內部都是把變數加1。
      前++,(++i)先加後用,返回加1之後的變數值,可以把變數直接加1,就返回,所有可以直接返回引用
      後++,(i++)先用後加,返回加1之前的變數值,就是要返回原來的舊值,
             這樣需要在重載運算子的函數內部建立一個對象儲存舊值,再進行加1運算,返回這箇舊值本身。

   (2)重載"="
       實現用一個int類型給一個Integer對象賦值

       Integer& operator=(int i){   //賦值運算,把對象內的int值改變,返回本身即可,所以傳回值是引用                *p = i;             //手工將int類型的值賦到對象內                return *this;       }

   (3)運算子多載
        不僅可以用類的成員函數實現,也可以用普通函數實現
        用成員函數實現,參數只有一個,運算子左邊的是自身,右邊的是參數  a1.operator=(a2);
        用普通函數實現,參數需要兩個,第一個參數是運算子左邊的值,第二個參數是運算子右邊的值,作為友員函數重載                                
        operator(a1,a2);
     
   (4)推薦原則
        所有一元運算子  ---  成員重載    "=","[]"只能成員重載
        二元運算子 --- 友員重載
       
        Integer物件類型與int類型相加的時候,
        實現 5+i 而且也能 i+5
        可以用友員重載2次

        friend Integer operator+(const Integer& i , int a);        friend Integer operator+(int a , const Integer& i );  //在類中友員函數的聲明

    (5)強制類型轉換運算子
          operator int(){......}  //強轉成int類型的聲明     
         
3、流操作符的重載
  (1)輸出資料流操作符只能使用友員方式重載

       friend ostream& operator<< (ostream & o,Integer & i);  //聲明友員函數       ostream& operator<< (ostream & o ,Integer & i){        //實現             o << *(i.p) ;           //輸出對象內的*p,即指標指向的變數            return o;       }

       cout << i ;   <=>   operator(cout,i);
      
   (2)輸入運算子也能重載,實現對象的讀入,只能使用友員函數重載

       friend istream& operator>> (istream & in,Integer & i);  //聲明友員函數       istream& operator>> (istream & in ,Integer & i){        //實現             in >> *(i.p) ;          //把讀入的資料放到*p指向的變數中            return in;       }            

   (3)為什麼只能用友員重載?
        因為cin cout 的位置是固定的,  cin >> i ; cout << i ;
        這樣不能利用對象本身調用重載的流操作符  i.operator(cout) ->這樣是不正確的
        只能使用友員重載,把對象和cout都當做參數傳進去,這樣就能操作了
       
練習:帳號類,直接輸出帳號

View Code

      Account a1;      cout << a1 << endl;             friend ostream& operator<< (ostream & o,Account & a);        ostream& operator<< (ostream & o ,Account & a){                   o << "Name     : " << a.name <<endl;            o << "password : " << a.password << endl;                       o << "id       : " << a.id << endl;            o << "balance  : " << a.balance  ;                        return o;       }                   friend istream& operator>> (istream & in,Account & a);        istream& operator>> (istream & in ,Account & a){                     cout << "enter your name >";            in >> a.name;                    cout << "enter your password >";            in >> a.password;            cout << "enter your id >";            in >> a.id;            cout << "enter your balance >";            in >> a.balance;            return in;       }

作業 :
     寫一個類叫“rmb”
     RMB{
        int * yuan;
        int * jiao;
        int * fen;
       
        RMB();
        RMB(int y , int j ,int f);
        RMB(const RMB & r);
        ~RMB();
        operator=
        friend ostream& operator<< (ostream & o,RMB &r);
        friend istream& operator>> (istream & in,RMB &r);
        人民幣之間的加減運算,及與int的乘法
        operator double(){} //強轉運算
        operator float(){}
     }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.