首页 > 后端开发 > C++ > 正文

C++运算符重载规则 算术运算符重载示例

P粉602998670
发布: 2025-08-22 13:11:01
原创
277人浏览过
C++中运算符重载允许为类类型定义算术运算行为,示例中Complex类通过成员函数重载+、-、*、/实现复数计算,遵循不改变优先级、使用const引用参数等规则,并通过友元函数重载<<实现输出。

c++运算符重载规则 算术运算符重载示例

在C++中,运算符重载允许我们为自定义类型(如类或结构体)赋予标准运算符新的行为。算术运算符如 +-*/ 等是最常被重载的运算符之一,用于实现对象之间的数学运算。

运算符重载基本规则

重载算术运算符需遵循以下规则:

  • 不能改变运算符的优先级和结合性
  • 不能创建新的运算符,只能重载已有运算符
  • 部分运算符必须作为类的成员函数重载(如赋值 =、下标 []、函数调用 ()、成员访问 ->)
  • 算术运算符通常以成员函数或友元函数形式实现,推荐使用成员函数实现 +=, -= 等复合赋值运算符,而 +, - 等可使用友元或非成员函数基于复合赋值实现

算术运算符重载示例:复数类

以下是一个简单的复数类 Complex,演示如何重载 +、-、*、/ 运算符。

立即学习C++免费学习笔记(深入)”;

#include <iostream>
using namespace std;
<p>class Complex {
private:
double real;
double imag;</p><p>public:
// 构造函数
Complex(double r = 0, double i = 0) : real(r), imag(i) {}</p><pre class='brush:php;toolbar:false;'>// 显示复数
void display() const {
    cout << "(" << real << " + " << imag << "i)";
}

// 重载加法运算符(成员函数)
Complex operator+(const Complex& other) const {
    return Complex(real + other.real, imag + other.imag);
}

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

// 重载乘法运算符
Complex operator*(const Complex& other) const {
    // (a+bi)(c+di) = (ac-bd) + (ad+bc)i
    double r = real * other.real - imag * other.imag;
    double i = real * other.imag + imag * other.real;
    return Complex(r, i);
}

// 重载除法运算符
Complex operator/(const Complex& other) const {
    double denominator = other.real * other.real + other.imag * other.imag;
    if (denominator == 0) {
        cerr << "除零错误!\n";
        return Complex();
    }
    double r = (real * other.real + imag * other.imag) / denominator;
    double i = (imag * other.real - real * other.imag) / denominator;
    return Complex(r, i);
}

// 友元函数重载输出运算符
friend ostream& operator<<(ostream& os, const Complex& c) {
    os << "(" << c.real << " + " << c.imag << "i)";
    return os;
}
登录后复制

};

算家云
算家云

高效、便捷的人工智能算力服务平台

算家云37
查看详情 算家云

使用示例

测试上面定义的运算符重载功能:

int main() {
    Complex c1(3, 4);  // 3 + 4i
    Complex c2(1, 2);  // 1 + 2i
<pre class='brush:php;toolbar:false;'>Complex sum = c1 + c2;
Complex diff = c1 - c2;
Complex prod = c1 * c2;
Complex quot = c1 / c2;

cout << "c1 = "; c1.display(); cout << endl;
cout << "c2 = "; c2.display(); cout << endl;
cout << "c1 + c2 = "; sum.display(); cout << endl;
cout << "c1 - c2 = "; diff.display(); cout << endl;
cout << "c1 * c2 = "; prod.display(); cout << endl;
cout << "c1 / c2 = "; quot.display(); cout << endl;

// 使用友元输出
cout << "使用重载<<: " << c1 << endl;

return 0;
登录后复制

}

关键点说明

几点需要注意:

  • 算术运算符重载通常返回一个新的对象,而不是引用,因为结果是临时值
  • 参数使用 const 引用避免拷贝,提高效率
  • 成员函数形式的重载只有一个显式参数(右侧操作数),左侧是 *this
  • 对于对称操作(如 a + b),若希望支持隐式类型转换(如 int + Complex),建议使用非成员友元函数
  • 除法等可能出错的操作应加入必要检查

基本上就这些。掌握这些模式后,可以扩展到向量、矩阵、分数等类型的运算符重载。

以上就是C++运算符重载规则 算术运算符重载示例的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号