C++ 操作符重载(一)

这里介绍了:
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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 对于一个运算符函数来说,它或者是类的成员,或者至少是含有一个类型的对象。 对箭头运算符来说,不 有不能重载的运算符...
    HenryTien阅读 2,915评论 0 1
  • 定义 运算符重载的 实质 是 函数的重载 使用意义赋予操作符更多的意义,同一个运算符,对不同类型的操作数,所发生的...
    第八区阅读 3,157评论 0 0
  • C++运算符重载-下篇 本章内容:1. 运算符重载的概述2. 重载算术运算符3. 重载按位运算符和二元逻辑运算符4...
    Haley_2013阅读 5,310评论 0 49
  • C++运算符重载-上篇 本章内容:1. 运算符重载的概述2. 重载算术运算符3. 重载按位运算符和二元逻辑运算符4...
    Haley_2013阅读 6,789评论 0 51
  • ​【1】 目夕川是个自由职业者,也就是无业游民,独自一人研究了一套抓娃娃的功夫,每次去电玩店都能提一大包娃娃出来,...
    第二类人阅读 2,548评论 0 1