运算符重载

简易cout、cin的实现

类内重载

//自己写一个命名空间
namespace myspace {
class istream {
public:
    //返回对象的引用:为了一直使用这个cin对象,避免再拷贝一份
    istream &operator>>(int &n) {
        //参数传引用,避免再拷贝一份n
        std::cin >> n; //可以用scanf来实现
        return *this;
    }
private:

};

class ostream {
public:
    ostream &operator<<(int &n) {
        std::cout << n; //可用printf("%g")来实现
        return *this;
    }
    ostream &operator<<(const char *msg) {
        std::cout << msg;
        return *this;
    }
private:

};
istream cin;
ostream cout;

};
int main() {
    int n, m;
    myspace::cin >> n >> m; //系统默认为执行 cin.operator>>(n)
    myspace::cout << n << " " <<  m << "\n";
    return 0;
}

类外重载

//自己写一个命名空间
namespace myspace {
class istream {
public:
    //返回对象的引用:为了一直使用这个cin对象,避免再拷贝一份
    istream &operator>>(int &n) {
        //参数传引用,避免再拷贝一份n
        std::cin >> n;
        return *this;
    }
private:

};

class ostream {
public:
    ostream &operator<<(int &n) {
        std::cout << n;
        return *this;
    }
    ostream &operator<<(const char *msg) {
        std::cout << msg;
        return *this;
    }
private:

};

istream cin;
ostream cout;

};
//类外重载运算符
myspace::ostream &operator<<(myspace::ostream &out, double &z) {
    std::cout << z;
    return out;
}

ostream &operator+(ostream &out, const int &a) {
    //这时的a不仅可以是传过来的常量(子面量)也可以是变量
    cout << a;
    return out;
}

int main() {
    int n, m;
    //cin >> n: 系统会将其转化为 cin.oprator>>(n)
    myspace::cin >> n >> m;
    myspace::cout << n << " " <<  m << "\n";
    double k = 5.5;
    myspace::cout << k << "\n";
    (((((cout + 8) << " ") + 9 )<< " ") + 10) << "\n"; //要注意 + 和 << 的优先级
    return 0;
}


重载实战

//重载运算符的方法的参数个数一定要和原来运算符的目数相同
//优先匹配类内重载

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <algorithm>
#include <set>
using namespace std;

class Point {
public :
    Point() : __x(0), __y(0) {}
    Point(int x, int y) : __x(x), __y(y) {}
    int x() const { return __x; }
    int y() const { return __y; }
    
    Point operator+(const Point &a) {
        return Point(x() + a.x(), y() + a.y());
    }
    
    Point &operator+=(const Point &a) {
        __x += a.x();
        __y += a.y();
        return *this;
    }

    Point &operator++() {
        //前++
        __x += 1;
        __y += 1;
        return *this;
    }

    Point operator++(int) {
        //后++
        //返回不能是引用,因为方法一结束,temp就没了,所以要返回一个temp的拷贝
        Point temp(*this);
        __x += 1;
        __y += 1;
        return temp;
    }


private:
    int __x, __y;
};


ostream &operator<<(ostream &out, const Point &a) {
    out << "Point (" << a.x() << ", " << a.y() << ")";
    return out;
}



int main() {
    Point a(4, 5), b(3, 4), c(1, 1);
    cout << a << endl;
    cout << b << endl;
    cout << c << endl;
    cout << a + b << endl;

    cout << "pre incr: " << ++(c += b) << endl;
    //++(c += b);
    cout << c << endl;
    cout << "after incr: " << c++ << endl;

    int n = 6, m = 7;
    (n += m)++;
    cout << n << endl;
    return 0;
}

特殊运算符的重载

class A {
public :
    A() {
        arr = new int[10];
    }
    A(const A &a) : A() {
        //深拷贝,系统默认拷贝构造是浅拷贝()
        for (int i = 0; i < 10; i++) {
            this->arr[i] = a.arr[i];
        }
        this->x = a.x;
        this->y = a.y;
    }

    int x, y;
    int *arr;
};

class B {
public:
    B() : obj(nullptr){
        arr = new int[10];
        arr[3] = 9972;
    }
    B(A *obj) : B() {
        this->obj = obj;
    }


    int operator()(int a, int b){
        return a + b;
    }
    int &operator[](int ind) {
        //返回引用,返回的是ind处的变量
        return arr[ind];
    }
    
    void operator[](const char *msg) {
        cout << msg << endl;
        return ;
    }

    A *operator->() {
        return obj;
    }
    A &operator*() {
        return *obj;
    }

    ~B() {
        delete arr;
    }

private:
    int *arr;
    A *obj;
};


ostream &operator<<(ostream &out, const A &a) {
    out << "A(" << a.x << ", " << a.y << ")";
    return out;
}

//主函数中是外部表现,类中是内部实现
int main() {
    B add;
    cout << add(3, 5) << endl; //函数对象(像函数的对象),重载()
    add[3] = 787; //数组对象, 重载[]
    cout << add[3] << endl;
    add["hello world"];

    A a, b(a); //浅拷贝
    a.x = 87, a.y = 852;
    B p = &a; //指针对象
    cout << p->x << " " << p->y << endl;
    cout << *p << endl;

    a.arr[3] = 111;
    b.arr[3] = 222;
    cout << a.arr[3] << endl;
    cout << b.arr[3] << endl;
    cout << sizeof(a) << endl;
    return 0;
}


class IntArray {
 public:
    IntArray(int n) : n(n) {
        this->arr = new int[n];
    }

    IntArray(IntArray &obj) : n(obj.n){
        this->arr = new int[n];
        for (int i = 0; i < n; i++) {
            this->arr[i] = obj[i];
        }
    }

    int &operator[](int ind) {
        if (ind >= 0) { 
            return this->arr[ind];
        } else {
            return this->arr[this->n + ind];
        }
    }

    void operator+=(int num) {
        for (int i = 0; i < n; i++) {
            this->arr[i] += num;
        }
        return ;
    }

    IntArray operator++(int x) {
        //后++, 无论后++还是前++都是要加的。所以要提前拷贝一份返回加之前的样子.
        IntArray temp(*this);
        for (int i = 0; i < this->n; i++) {
            this->arr[i] += 1;
        }
        return temp;
    }

    IntArray &operator++() {
        for (int i = 0; i < this->n; i++) {
            this->arr[i] += 1;
        }
        return *this;
    }

    friend ostream &operator<<(ostream &, const IntArray &);
    ~IntArray() {
        delete[] this->arr;
    }
 private:
    int *arr, n;
};

ostream &operator<<(ostream &out, const IntArray &a) {
    out << "[ ";
    for (int i = 0; i < a.n; i++) {
        out << a.arr[i] << " ";
    }
    out << "]";
    return out;
}

int main() {
    srand(time(0));
    IntArray a(10);
    for (int i = 0; i < 10; i++) {
        a[i] = rand() % 100; //!!这里要注意一下如何重载[], a[i]返回值应该是arr[i]这个变量的引用
    }
    cout << a << endl; //输出数组中所有的元素
    cout << a[4] << endl;
    cout << a[-2] << endl; //输出倒数第2位的值
    a += 5; //给数组的所有元素都加5
    cout << a << endl; //输出数组中所有的元素
    cout << (a++) << endl; //给数组中所有元素都加1
    cout << (++a) << endl; //给数组中所有元素都加1
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值