• C++运算符重载复习


    本人理解运算符重载实质 就类似函数重载   运算符重载都可以写成一个函数 里面传入参数 来调用 运算符重载不是必须的 但是重载后会方便很多。

    小例子 一个类实现 ++  和+某个数重载 大于号重载  有一点注释

    // ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    class Number
    {
    private:
        int lowValue;
        int highValue;
    public:
        Number(int lowValue, int highValue);
        void Print();
        Number operator++(); //这个类为返回值类型
        Number operator+(Number& n);
        bool operator<(Number& n);
    };
    bool Number::operator<(Number& n)
    {
        if (this->highValue<n.highValue) //说真的这个this->highValue 左边类的highValue 不是很理解 先记住把
        {
            return true;
        }
        else
        {
            return false;
        }
    
    }
    Number::Number(int lowValue, int highValue)
    {
        this->highValue = highValue;
        this->lowValue = lowValue;
    }
    Number Number::operator++()
    {
        lowValue++;
        highValue++;
        return *this;  //应该难理解的是这一句 当时学的时候老师没说自己也没想 本人拙见 this为这个函数的地址 例如 00401000 | 0001 0002   this为00401000  *this返回这个函数头 0001 0002
    }
    Number Number::operator+(Number& n)
    {
        lowValue = lowValue + n.lowValue;
        highValue = highValue + n.highValue;
        return *this;
    }
    void Number::Print()
    {
        printf("%d
    ", lowValue);
        printf("%d
    ", highValue);
    }
    
    void Test()
    {
        Number p(1, 1), p2(3, 4);
        
        if (p<p2)
        {
            cout << "True" << endl;
        }
    
        (p + p2).Print();
    
    }
    int main(int argc, char* argv[])
    {
        Test();
    
    
        return 0;
    }
  • 相关阅读:
    python学习笔记day08 文件功能详解
    python学习笔记day08 文件操作
    python学习笔记day07 小结
    python学习笔记day07 集合
    python学习笔记day06 购物车实现
    python学习笔记 day06 小知识点总结
    python学习笔记day01_03python 发展历史
    python学习笔记 day06
    python学习笔记 day05 习题讲解
    PAT L2-002 链表去重
  • 原文地址:https://www.cnblogs.com/xuexidememeda/p/12377052.html
Copyright © 2020-2023  润新知