zoukankan      html  css  js  c++  java
  • 重载操作符

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

    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;
    }

  • 相关阅读:
    aop 切面编程
    动态代理模式
    idea 从接口方法 跳转到 实现类 对应的方法
    2019年的某一天
    javaweb . 页面登出 操作
    Could not find acceptable representation报错
    window下mysql character_set_server修改不生效问题
    git 命令推送
    spring boot 2.x + elasticsearch+mybatis-plus
    java8的Consumer函数式接口
  • 原文地址:https://www.cnblogs.com/judes/p/8206908.html
Copyright © 2011-2022 走看看