C++之双目运算符重载

文章目录

运算符重载

cpp 复制代码
#include <iostream>
using namespace std;
class Complex{
private:
    double r;
    double i;
public:
    Complex(double r, double i){
        this->r = r;
        this->i = i;
    }
    void print(void){
        cout << r << " + " << i << "i" << endl;
    }
};
int main(void){
    Complex a(1,2);
    Complex b(3,4);
    a.print();
    b.print();
    int x = 10;
    int y = 20;
    int z = x + y;
    a+b; //想像x+y 实现Complex对象加运算 +运算符新的逻辑功能 要实现+运算符的重载
    return 0;
}

双目运算符重载

  • 双目运算符:有左右两个操作数的操作符 L#R
    • 算术运算:*、/、%、+、-
    • 关系运算:>、>=、<、<=、==、!=
    • 逻辑运算:&&、||
    • 位运算:&、|、^、<< 、>>
    • 赋值与复合赋值:=、+=、-=、*=、/=、%=、&=、|=、^=、<<= 、>>=
    • ... ...
  • 注意:
    • 表达式结果是右值
    • 左右操作数既可以是左值也可以是右值
  • 实现方式:
    • 成员函数形式:L.operator#®
    • 友元函数形式:operator#(L,R)
cpp 复制代码
#include <iostream>
using namespace std;
class Complex{
private:
    double r;
    double i;
public:
    Complex(double r, double i){
        this->r = r;
        this->i = i;
    }
    void print(void){
        cout << r << " + " << i << "i" << endl;
    }
    const Complex operator+(const Complex& c){
        Complex tmp(r+c.r, i+c.i);
        return tmp;
    }
    friend const Complex operator-(const Complex& l , const Complex& r);
};
const Complex operator-(const Complex& l, const Complex& r){
    Complex tmp(l.r - r.r, l.i - r.i);
    return tmp;
}
int main(void){
    Complex a(1,2);
    Complex b(3,4);
    a.print();
    b.print();
    Complex c = a+b;// a.operator+(b);
    c.print();
    Complex d = c - a; // operator-(c, a);
    d.print();
    return 0;
}
  • 对赋值类双目运算符重载时需要注意的事项:
    • 表达式的结果是左值,就是左操作数的自身
    • 左操作数必须是左值,右操作数可以是左值也可以是右值
cpp 复制代码
#include <iostream>
using namespace std;
class Complex{
private:
    double r;
    double i;
public:
    Complex(double r, double i){
        this->r = r;
        this->i = i;
    }
    void print(void){
        cout << r << " + " << i << "i" << endl;
    }
    const Complex operator+(const Complex& c){
        Complex tmp(r+c.r, i+c.i);
        return tmp;
    }
    Complex & operator+=(const Complex &c){
        r = r + c.r;
        i = i + c.i;
        return *this;
    }
    friend const Complex operator-(const Complex& l , const Complex& r);
    friend Complex & operator-=(Complex &L, const Complex &R);
};
const Complex operator-(const Complex& l, const Complex& r){
    Complex tmp(l.r - r.r, l.i - r.i);
    return tmp;
}
Complex & operator-=(Complex &L, const Complex &R){
    L.r -= R.r;
    L.i -= R.i;
    return L;
}
int main(void){
    Complex a(1,2);
    Complex b(3,4);
    a.print();
    b.print();
    Complex c = a+b;// a.operator+(b);
    c.print();
    Complex d = c - a; // operator-(c, a);
    d.print();
    a += b; //a.operator+=(b);
    a.print();
    (a -= b).print();
    return 0;
}
相关推荐
樱木Plus17 小时前
深拷贝(Deep Copy)和浅拷贝(Shallow Copy)
c++
blasit3 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
肆忆_4 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星4 天前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛6 天前
delete又未完全delete
c++
端平入洛7 天前
auto有时不auto
c++
哇哈哈20217 天前
信号量和信号
linux·c++
多恩Stone7 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马7 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝7 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode