美文网首页NDK开发
NDK----C++篇(四)C++运算符重载

NDK----C++篇(四)C++运算符重载

作者: 初夏的雪 | 来源:发表于2021-06-18 08:59 被阅读0次

上一章我们学习了类的核心知识,那这些类的对象可以像基本数据类型一样,可以做一些运算法操作不?答案是肯定的。接下来就是我们要说的运算符重载的一些知识。

说明:

1、C++语言中的运算符基本上都在写在类内部的,这样可以直接访问私有成员

2、内部运算符重载时,将操作符的参数设置为const+ 引用,一是防止修改,二是引用不会引起浪费性能的问题(只是别名)

3、前置++ 和后置++运算符重载时,为了区分需要在函数名()中加入int,

          **例如:operator ++(int) 代表后置++ ;operator ++()  代表前置++**

4、输入输出预算法重载(友元函数),需要支持连续调用,那就需要类似于RxJava的思路返回this

我们先来定义一个类:

class Paper {
private:
    int length;
    int width;

public:

    //输出运算符重载
    friend ostream &operator<<(ostream &_START, Paper paper) {
        _START << "width = " << paper.width << ",length=" << paper.length << endl;
        return _START;
    }

    //输入运算符重载
    friend istream &operator>>(istream &_START, Paper& paper) {
        _START >> paper.width;
        _START >> paper.length;
        return _START;
    }

    /**
    * 内部运算符重载,可以直接访问私有成员
    */
    Paper operator-(const Paper &paper) {
        int tempWidth = this->width - paper.width;
        int tempLength = this->length - paper.length;
        return Paper(tempWidth, tempLength);
    }

    /**
     * 前置++
     */
    void operator++() {
        this->width++;
        this->length++;
    }

    /**
     * 后置++
     */
    void operator++(int) {
        this->width++;
        this->length++;
    }

public:


    Paper(int width, int length) : width(width), length(length) {
    }

    int getLength() {
        return this->length;
    }

    int getWidth() {
        return this->width;
    }

    void setLength(int length) {
        this->length = length;
    }

    void setWidth(int width) {
        this->width = width;
    }

};

定义一个外部运算符重载

/**
 * 外部运算符重载
 * @param p1
 * @param p2
 * @return
 */
Paper operator+(Paper p1, Paper p2) {
    int width = p1.getWidth() + p2.getWidth();
    int length = p1.getLength() + p2.getLength();
    Paper totalPaper(width, length);
    return totalPaper;

}

测试代码:

int main() {

    Paper whitePaper(100, 200);
    Paper redPaper(333, 444);
    Paper totalPaper = whitePaper + redPaper;
    cout << "-----------------外部运算符重载-----------------" << endl;
    cout << "外部运算符+重载后的结果是:width=" << totalPaper.getWidth() << ",length=" << totalPaper.getLength() << endl;

    cout << "-----------------内部运算符重载-----------------" << endl;
    Paper innerPaper = totalPaper - whitePaper;
    cout << "内部运算符-重载后的结果是:width=" << innerPaper.getWidth() << ",length=" << innerPaper.getLength() << endl;


    cout << "-----------------前置后置++运算符重载-----------------" << endl;
    innerPaper++;
    cout << "内部运算符后置++重载后的结果是:width=" << innerPaper.getWidth() << ",length=" << innerPaper.getLength() << endl;
    ++innerPaper;
    cout << "内部运算符前置++重载后的结果是:width=" << innerPaper.getWidth() << ",length=" << innerPaper.getLength() << endl;


    cout << "-----------------输入输出运算符重载-----------------" << endl;
    cin >> innerPaper;
    cout <<"输出用户输入:"<< innerPaper;

    return 0;
}


/**
-----------------外部运算符重载-----------------
外部运算符+重载后的结果是:width=433,length=644
-----------------内部运算符重载-----------------
内部运算符-重载后的结果是:width=333,length=444
-----------------前置后置++运算符重载-----------------
内部运算符后置++重载后的结果是:width=334,length=445
内部运算符前置++重载后的结果是:width=335,length=446
-----------------输入输出运算符重载-----------------
*/

下节预告:容器与谓词

相关文章

  • NDK----C++篇(四)C++运算符重载

    上一章我们学习了类的核心知识,那这些类的对象可以像基本数据类型一样,可以做一些运算法操作不?答案是肯定的。接下来就...

  • 第十一章 使用类

    运算符重载 运算符重载是一种形式的C++多态。运算符重载将重载的概念扩展到运算符上,允许赋予C++运算符多种含义。...

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

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

  • C++ 运算符重载

    运算符重载将重载的概念扩展到运算符上,允许赋予C++运算符多种含义。实际上,很多C++运算符已经重载。将*运算符用...

  • C++运算符重载

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

  • C++运算符重载-下篇 (Boolan)

    C++运算符重载-下篇 (Boolan) 本章内容:1. 运算符重载的概述2. 重载算术运算符3. 重载按位运算符...

  • C++运算符重载-上篇 (Boolan)

    C++运算符重载-上篇 (Boolan) 本章内容:1. 运算符重载的概述2. 重载算术运算符3. 重载按位运算符...

  • C++ 重载运算符

    C++重载运算符

  • C++重载

    重载 C++语言规定: 重载的运算符要保持原运算符的意义。只能对已有的运算符重载,不能增加新的运算符。重载的运算符...

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

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

网友评论

    本文标题:NDK----C++篇(四)C++运算符重载

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