美文网首页
C++ 操作符重载(一)

C++ 操作符重载(一)

作者: 司马捷 | 来源:发表于2016-07-30 15:40 被阅读30次

    这里介绍了:
    1.++obj和obj++ 如何重载.(添天占位参数)
    2.基本的操作符重载过程. 返回类型 operator 操作符 参数
    3.友元函数 没有自带的 this 指针,在重载操作符的时候,所有的参数都必须在参数中显示声明.
    4.重载 [] 提取操作符 << >> 流操作符.

    #include <iostream>
    using namespace std;
    // 运算符重载 本质是一个函数
    
    
    
    class Complex{
    public:
        int a;
        int b;
        
      //  friend Complex operator+(Complex &c1,Complex &c2);
        
        
        Complex(int a = 0,int b =0){
            this->a = a;
            this->b = b;
        }
        
    //    Complex operator+(Complex &c1){
    //        
    //        Complex temp = Complex(this->a+c1.a,this->b+c1.b);
    //        
    //        return temp;
    //    }
        
        // 前置
        Complex operator--(){
            this->a--;
            this->b--;
            
            return *this;
        }
        
        //占位参数来 区别 是 obj-- 还是 --obj   这里使用一个伪参数 表示后置的 obj--;
        Complex operator--(int){
            this->a--;
            this->b--;
            
            return *this;
        }
        Complex operator+(int z){
            
            this->a = this->a + z;
            return *this;
        }
        
        
    public:
        void printCom(){
            cout<<a<<"+"<<b<<endl;
        }
        
        
    };
    
    Complex operator+(Complex &c1,Complex &c2){
        
        Complex tmp(c1.a+c2.a,c1.b+c2.b);
        
        
        
        return tmp;
    }
    
    class vector{
        
    public:
        vector(int size = 1);
        
        int & operator[](int i);
        friend ostream& operator<<(ostream& output,vector&);
        friend istream& operator>>(istream& input, vector&);
        
        ~vector();
    private:
        int *v;
        int len;
    };
    
    vector::vector(int size){
        
        if (size<= 0 || size>100) {
            cout<<"The size of "<<size<<"is null!\n";abort();
        }
        v = new int[size];
        len = size;
    }
    
    vector::~vector(){
        delete [] v;
        len = 0;
        
    }
    
    //重载提取操作符 判断数组越界
    int &vector::operator[](int i){
        if (i>=0&&i<len) {
            return v[i];
        }
        cout<<"The subscript"<<"is outside!\n"; abort();
        
    }
    
    ostream & operator <<(ostream& output,vector& ary){
        
        for (int i = 0; i<ary.len; i++) {
            output<<ary[i]<<"  ";
        }
        output<<endl;
        
        return output;
    }
    
    istream & operator >> (istream & input,vector &ary){
        
        for (int i = 0; i<ary.len; i++) {
            input>>ary[i];
        }
        return input;
    }
    
    
    int main(int argc, const char * argv[]) {
        // insert code here...
        std::cout << "Hello, World!\n";
        
        
        Complex c1(1,2),c2(3,4);
        
        Complex c3 = c1+c2;
        c3.printCom();
        
        c3--;
        --c3;
        
        
        /**
         *  在第一个参数需要隐式转换的情形下,使用友元函数重载运算符是正确的选择
         友元函数没有this 指针,所需操作数 都必须在参数表显式生命,很容易实现类型的隐式转换.
         */
        c3 = c3+4;
        c3.printCom();
        
        int k;
        cout<<"Input the length of vetor A:\n";
        cin>>k;
        
        vector A(k);
        
        cout<<"Input the elements of vetor A:\n";
        cin>>A;
        cout<<"Output the elements of vector A:\n";
        cout<<A;
        
        
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:C++ 操作符重载(一)

          本文链接:https://www.haomeiwen.com/subject/nbktsttx.html