0
点赞
收藏
分享

微信扫一扫

C++ 函数运算符重载(二)化简版

操作符重载函数,一般卸载类的内部

可以在类的外部实现,但是内部封装性考虑,最重要的是可以减少参数的传递。

重载实例说明

① 输入重载 istream >>
② 输出重载 ostream <<
③ 前置++ 和后++ 重载 ---注意后置参数设置
④ 运算符+,- 重载,
const 对象 & 别名

系统是这样写的 常量引用:不允许修改,只读模式
& 性能的提高,如果没有& 运行+ 构建新的副本,会浪费性能
如果增加了& 引用是给这块内存空间取一个别名而已

#include <iostream>

using namespace std;

class David {
public:
    David(int x, int y) : x(x), y(y) {
    }

    David() : x(0), y(0) {

    }

    // 系统是这样写的  常量引用:不允许修改,只读模式
    // const 关键字的解释
    // & 性能的提高,如果没有&  运行+ 构建新的副本,会浪费性能
    // 如果增加了& 引用是给这块内存空间取一个别名而已
    David operator + (const David& david) {
        // this指针 指向当前对象,所以只需要一个
        int x = this->x + david.x; // 我在类的里面,是可以拿私有成员的
        int y = this->y + david.y; // 我在类的里面,是可以拿私有成员的
        return David(x, y);
    }

    // 运算符- 重载
    David operator - (const David & david) {
        int x = this->x - david.x;
        int y = this->y - david.y;
        return David(x, y);
    }

    //++ 对象
    void operator++() {
        this->x = this->x + 1;
        this->y = this->y + 1;
    }

    //对象++
    void operator++(int) {
        this->x = this->x + 1;
        this->y = this->y + 1;
    }

    friend istream &operator>>(istream &_START, David &david) {
        _START >> david.x >> david.y;
        return _START;
    }
    // 单个输出
//    friend void operator << (ostream & _START,David & david){
//        _START << "单个输出了 "<< david.getX() << "!!!" << david.getY() << endl;
//    }

    // 多个输出
    friend ostream &operator<<(ostream &_START, const David &david) {
        _START << "多个输出 " << david.getX() << "!!!" << david.getY() << endl;
        return _START;
    }

    int getX() const {
        return x;
    }

    void setX(int x) {
        this->x = x;
    }

    int getY() const {
        return y;
    }

    void setY(int y) {
        this->y = y;
    }

private:
    int x = 0;
    int y = 0;
};

int main() {
    David res;
    //① 输入重载测试
//    cin >> res;
//    cout << res.getX() << "," << res.getY() << endl;

//② 输出重载测试
    David david(1000, 2000);
    David david2(2000, 4000);
//    cout << david;
    cout << david << david2;
    //③ 前置++ 和后++ 重载
    david++;
    ++david2;
    cout << david << david2;
//    ④运算符+,- 重载,const 对象 & 别名
cout << (david + david2) << david2 - david ;
    return 0;


}

3.括号运算符。 数组 系统源码把此括号[i]给重载, 系统重载后的样子 *(arr+i)

建议:
能在栈区的,尽量在栈区
1.代码量少
2.避免麻烦
3.怕有问题
4.栈区的回收,不是你负责的,责任推卸

#include <iostream>
using namespace std;

// 写一个小容器,模拟容器
class ArrayClass {

private:
    // C++ 默认都是系统值  size 系统值 -13275
    int size = 0; // 大小  开发过程中,给size赋默认值,不然会出现,后患无穷的问题
    int * arrayValue; // 数组存放 int 类型的很多值

public:
    void set(int index, int value) {
        arrayValue[index] = value; // []目前不是我的
        size+=1;
    }
    int getSize() { // size成员的目标:是为了循环可以遍历
        return this->size;
    }
    // 运算符重载 [index]
    int operator[](int index) {
        return this->arrayValue[index]; // 系统的
    }
};

// 输出容器的内容
void printfArryClass(ArrayClass arrayClass) {
    cout << arrayClass.getSize() << endl;
    for (int i = 0; i < arrayClass.getSize(); ++i) {
        cout << arrayClass[i] << endl; // []是我们自己的 重载符号
    }
}

int main() {
    

    ArrayClass arrayClass;  // 栈区    实例出来的对象,是在堆区了

    arrayClass.set(0, 1000);
    arrayClass.set(1, 2000);
    arrayClass.set(2, 3000);
    arrayClass.set(3, 4000);
    arrayClass.set(4, 5000);

    printfArryClass(arrayClass);

    return 0;
}
举报

相关推荐

0 条评论