• 重载操作符


    一、类外重载操作符(使用友元函数)

    class Complex
    {
        int a;
        int b;
    public:
        Complex(int a = 0, int b = 0)
        {
            this->a = a;
            this->b = b;
        }

      friend Complex operator + (const Complex& p1, const Complex& p2);//友元函数,使类之外的函数能够访问私有属性

                  //两个参数,分别对应+的左右参数

    };

    Complex operator + (const Complex& p1, const Complex& p2)
    {
      Complex ret;
      ret.a = p1.a + p2.a;
      ret.b = p1.b + p2.b;
      return ret;
    }

    int main()
    {

      Complex c1(1, 2);

      Complex c2(3, 4);
      Complex c3 = c1 + c2; // operator + (c1, c2)
      printf("c3.a = %d, c3.b = %d ", c3.getA(), c3.getB());
      return 0;
    }

    二、类函数重载操作符

    class Complex
    {
        double a;
        double b;
    public:
        Complex(double a = 0, double b = 0);
        double getA();
        double getB();
        double getModulus();
       
        Complex operator + (const Complex& c);//只有一个参数,对应+的右操作数,左操作数默认为this
        Complex operator - (const Complex& c);
        Complex operator * (const Complex& c);
        Complex operator / (const Complex& c);
       
        bool operator == (const Complex& c);
        bool operator != (const Complex& c);
       
        Complex& operator = (const Complex& c);
    };

    Complex::Complex(double a, double b)
    {
        this->a = a;
        this->b = b;
    }

    double Complex::getA()
    {
        return a;
    }

    double Complex::getB()
    {
        return b;
    }

    double Complex::getModulus()
    {
        return sqrt(a * a + b * b);
    }

    Complex Complex::operator + (const Complex& c)
    {
        double na = a + c.a;
        double nb = b + c.b;
        Complex ret(na, nb);
       
        return ret;
    }

    Complex Complex::operator - (const Complex& c)
    {
        double na = a - c.a;
        double nb = b - c.b;
        Complex ret(na, nb);
       
        return ret;
    }

    Complex Complex::operator * (const Complex& c)
    {
        double na = a * c.a - b * c.b;
        double nb = a * c.b + b * c.a;
        Complex ret(na, nb);
       
        return ret;
    }

    Complex Complex::operator / (const Complex& c)
    {
        double cm = c.a * c.a + c.b * c.b;
        double na = (a * c.a + b * c.b) / cm;
        double nb = (b * c.a - a * c.b) / cm;
        Complex ret(na, nb);
       
        return ret;
    }
       
    bool Complex::operator == (const Complex& c)
    {
        return (a == c.a) && (b == c.b);
    }

    bool Complex::operator != (const Complex& c)
    {
        return !(*this == c);
    }
       
    Complex& Complex::operator = (const Complex& c)
    {
        if( this != &c )
        {
            a = c.a;
            b = c.b;
        }
       
        return *this;
    }

  • 相关阅读:
    spring boot的@RequestParam和@RequestBody的区别
    SpringBoot 中常用注解@PathVaribale/@RequestParam/@GetMapping介绍
    Required String parameter is not present
    Spring Boot 日志记录 SLF4J
    400错误,Required String parameter 'paramter' is not present
    初学 Spring boot 报错 Whitelabel Error Page 404
    Powershell获取WMI设备清单
    Powershell快速入门
    perl的Sys::Syslog模块(openlog,syslog,closelog函数,setlogsock)-自定义日志
    制作Windows10政府版的小白教程
  • 原文地址:https://www.cnblogs.com/judes/p/8206908.html
Copyright © 2020-2023  润新知