C++运算符重载详解及常用的运算符重载代码示例

C++ 运算符重载是指对已有运算符重新定义,以使它们能够处理用户自定义的数据类型。重载运算符可以使自定义类型的操作更加自然和直观。以下部分是详细的讲解,包括所有类型的重载、注意点、常用的C++运算符重载代码示例以及运行结果解释。

1. 运算符重载类型

C++中可以重载的运算符主要分为以下几类:

  1. 算术运算符+, -, *, /, %
  2. 关系运算符==, !=, <, >, <=, >=
  3. 逻辑运算符&&, ||, !
  4. 位运算符&, |, ^, ~, <<, >>
  5. 赋值运算符=, +=, -=, *=, /=, %=
  6. 增量和减量运算符++, --
  7. 下标运算符[]
  8. 函数调用运算符()
  9. 指针运算符*, ->
  10. 输入输出运算符<<, >>

2. 注意点

  • 不能重载的运算符:::, . , .*, ?:
  • 重载运算符必须是类的成员函数或友元函数。
  • 一元运算符通常作为成员函数重载,二元运算符可以作为成员或友元函数重载。
  • 对称性运算符(如 ==, +)有时需要友元函数以保持操作数交换的对称性。
  • 重载的运算符的优先级和结合性无法改变。

3. 常用的C++运算符重载代码示例

(1) 算术运算符重载

#include <iostream>

class Complex 
{
private:
    double real, imag;
public:
    Complex(double r = 0, double i = 0) : real(r), imag(i) {}

    // 重载 + 运算符
    Complex operator + (const Complex& other) const 
    {
        return Complex(real + other.real, imag + other.imag);
    }

    // 重载 << 运算符用于输出
    friend std::ostream& operator << (std::ostream& out, const Complex& c) 
    {
        out << c.real << " + " << c.imag << "i";
        return out;
    }
};

int main() 
{
    Complex c1(3.0, 4.0), c2(1.0, 2.0);
    Complex c3 = c1 + c2;
    std::cout << "c1 + c2 = " << c3 << std::endl;
    return 0;
}

解释

  • Complex 类中重载了 + 运算符,实现了复数相加。
  • << 运算符被重载以便于输出复数格式。

输出结果

c1 + c2 = 4 + 6i

(2) 关系运算符重载

#include <iostream>

class Complex 
{
private:
    double real, imag;
public:
    Complex(double r = 0, double i = 0) : real(r), imag(i) {}

    // 重载 == 运算符
    bool operator == (const Complex& other) const 
    {
        return (real == other.real) && (imag == other.imag);
    }
};

int main() 
{
    Complex c1(3.0, 4.0), c2(3.0, 4.0), c3(1.0, 2.0);
    if (c1 == c2) 
    {
        std::cout << "c1 and c2 are equal." << std::endl;
    } 
    else 
    {
        std::cout << "c1 and c2 are not equal." << std::endl;
    }
    if (c1 == c3) 
    {
        std::cout << "c1 and c3 are equal." << std::endl;
    } 
    else 
    {
        std::cout << "c1 and c3 are not equal." << std::endl;
    }
    return 0;
}

解释

  • Complex 类中重载了 == 运算符,用于比较两个复数是否相等。

输出结果

c1 and c2 are equal.
c1 and c3 are not equal.

(3) 下标运算符重载

#include <iostream>

class Array 
{
private:
    int* arr;
    int size;
public:
    Array(int s) : size(s) 
    {
        arr = new int[size];
    }
    ~Array() 
    {
        delete[] arr;
    }

    // 重载 [] 运算符
    int& operator[](int index) 
    {
        if (index >= 0 && index < size) 
        {
            return arr[index];
        } 
        else 
        {
            std::cerr << "Index out of bounds" << std::endl;
            exit(1);
        }
    }
};

int main() 
{
    Array a(10);
    a[0] = 10;
    a[1] = 20;
    std::cout << "a[0] = " << a[0] << std::endl;
    std::cout << "a[1] = " << a[1] << std::endl;
    return 0;
}

解释

  • Array 类中重载了 [] 运算符,用于数组元素的访问。

输出结果

a[0] = 10
a[1] = 20

(4) 赋值运算符重载

#include <iostream>

class Complex 
{
private:
    double real, imag;
public:
    Complex(double r = 0, double i = 0) : real(r), imag(i) {}

    // 重载 = 运算符
    Complex& operator = (const Complex& other) 
    {
        if (this == &other) return *this;
        real = other.real;
        imag = other.imag;
        return *this;
    }

    // 重载 << 运算符用于输出
    friend std::ostream& operator << (std::ostream& out, const Complex& c) 
    {
        out << c.real << " + " << c.imag << "i";
        return out;
    }
};

int main() 
{
    Complex c1(3.0, 4.0), c2;
    c2 = c1;
    std::cout << "c2 = " << c2 << std::endl;
    return 0;
}

解释

  • Complex 类中重载了 = 运算符,实现对象赋值。

输出结果

c2 = 3 + 4i

这些示例详细介绍了不同类型的运算符重载以及在C++中重载运算符时需要注意的要点。这些重载使得自定义类型能够自然地使用运算符,从而增强代码的可读性和可维护性。

  • 15
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Warren++

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值