美文网首页C语言编程语言爱好者
C++ 函数运算符重载(二)化简版

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

作者: zcwfeng | 来源:发表于2021-03-24 13:01 被阅读0次

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

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

重载实例说明

① 输入重载 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;
}

相关文章

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

    操作符重载函数,一般卸载类的内部 可以在类的外部实现,但是内部封装性考虑,最重要的是可以减少参数的传递。 重载实例...

  • 1.2.15_C++ 关系运算符重载

    C++ 重载运算符和重载函数 C++ 语言支持各种关系运算符( < 、 > 、 <= 、 >= 、 == 等等),...

  • C++运算符重载

    C++运算符重载的实质:运算符重载的实质就是函数重载或函数多态。运算符重载是一种形式的C++多态。目的在于让人能够...

  • 1.2.14_C++ 二元运算符重载

    C++ 重载运算符和重载函数 二元运算符需要两个参数,下面是二元运算符的实例。 我们平常使用的加运算符( + )、...

  • 1.2.19_C++ 函数调用运算符 () 重载

    C++ 重载运算符和重载函数 函数调用运算符 () 可以被重载用于类的对象。当重载 () 时,您不是创造了一种新的...

  • 1.2.17_C++ ++ 和 -- 运算符重载

    C++ 重载运算符和重载函数 递增运算符( ++ )和递减运算符( -- )是 C++ 语言中两个重要的一元运算符...

  • 1.2.20_C++ 下标运算符 [] 重载

    C++ 重载运算符和重载函数 下标操作符 [] 通常用于访问数组元素。重载该运算符用于增强操作 C++ 数组的功能...

  • 1.2.16_C++ 输入/输出运算符重载

    C++ 重载运算符和重载函数 C++ 能够使用流提取运算符 >> 和流插入运算符 << 来输入和输出内置的数据类型...

  • C++ 重载 [] = == !=

    一 重载 [] = == != 1.1 重载[] C++ 规定,下标运算符[ ]必须以成员函数的形式进行重载。该重...

  • C++中的运算符重载

    1.Cpp中的重载运算符和重载函数 C++允许在同一作用域中的某个函数和运算符指定多个定义,分别称为函数重载和运算...

网友评论

    本文标题:C++ 函数运算符重载(二)化简版

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