C++中怎么实现操作符重载
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,本篇文章给大家分享的是有关C++中怎么实现操作符重载,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。各操作符重载的形式:Complex o
千家信息网最后更新 2025年12月02日C++中怎么实现操作符重载
本篇文章给大家分享的是有关C++中怎么实现操作符重载,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
各操作符重载的形式:
Complex operator + (const Complex& c);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);
代码实现:
test.h头文件:
#ifndef _COMPLEX_H_
#define _COMPLEX_H_
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);
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);
};
#endif
test.cpp文件:
#include "Complex.h"
#include "math.h"
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;
}
main.cpp文件:
#include
#include "Complex.h"
int main()
{
Complex c1(1, 2);
Complex c2(3, 6);
Complex c3 = c2 - c1;
Complex c4 = c1 * c3;
Complex c5 = c2 / c1;
printf("c3.a = %f, c3.b = %f\n", c3.getA(), c3.getB());
printf("c4.a = %f, c4.b = %f\n", c4.getA(), c4.getB());
printf("c5.a = %f, c5.b = %f\n", c5.getA(), c5.getB());
Complex c6(2, 4);
printf("c3 == c6 : %d\n", c3 == c6);
printf("c3 != c4 : %d\n", c3 != c4);
(c3 = c2) = c1;
printf("c1.a = %f, c1.b = %f\n", c1.getA(), c1.getB());
printf("c2.a = %f, c2.b = %f\n", c2.getA(), c2.getB());
printf("c3.a = %f, c3.b = %f\n", c3.getA(), c3.getB());
return 0;
}
运行结果:
root@txp-virtual-machine:/home/txp/tt# ./a.out
c3.a = 2.000000, c3.b = 4.000000
c4.a = -6.000000, c4.b = 8.000000
c5.a = 3.000000, c5.b = 0.000000
c3 == c6 : 1
c3 != c4 : 1
c1.a = 1.000000, c1.b = 2.000000
c2.a = 3.000000, c2.b = 6.000000
c3.a = 1.000000, c3.b = 2.000000
上面设计到一些数学知识,比如复数的乘法和除法运算:
乘法:(a+bi)(c+di)=(ac-bd)+(bc+ad)i
除法:(a+bi)/(c+di) =(ac+bd)/c*c+d*d +(bc-ad)/c*c +d*d
3、注意事项:
C++规定赋值操作符"="只能重载为成员函数
操作符重载不能改变原操作符的优先级
操作符重载不能改变操作数的个数
操作符重载不应改变操作符的原有语义
二、总结:
复数的概念可以通过自定义类实现
复数中的运算操作可以通过操作符重载实现
赋值操作符只能通过成员函数实现
操作符重载的本质为函数定义
以上就是C++中怎么实现操作符重载,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。
操作符
c.a
c.b
C++
函数
复数
文件
知识
乘法
可以通过
成员
更多
篇文章
除法
运算
实用
个数
事项
代码
优先级
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
诛仙2开哪个服务器好
衢州学软件开发流程八个步骤
怎么用旧手机搭建内网服务器
win7如何打开服务器配置
数据库技术方向选什么笔记本
软件开发研发方案评审
软件开发程序图
国家网络安全等级三级
主营计算机软件开发
抚州无线互联网科技有限公司
打印服务器 推荐
家庭私有云服务器原理是什么
江苏超频服务器欢迎咨询
工作室的服务器配置方案
discuz纯真ip数据库路径
软件开发模型优缺点对比
wmwave虚拟服务器安全吗
金蝶云星空用什么数据库
电竞服务器报价
云网络安全
网络安全专题讲课的简报
dbo数据库
服务器多核刷怪笼最大利用效率
湖南省医疗机构网络安全等级保护
深圳网络安全技术培训学校
空格存入数据库
forest怎么转到全球服务器
我来做服装定制软件开发
软件开发培训
网络安全与防范技术