C++day12 学习笔记

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(){}
     }

原文地址:https://www.cnblogs.com/tangzhengyue/p/2622618.html