温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++中怎么实现操作符重载

发布时间:2021-07-06 17:18:32 阅读:177 作者:Leah 栏目:大数据

本篇文章给大家分享的是有关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 <stdio.h>#include "Complex.h"int main(){    Complex c1(12);    Complex c2(36);    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(24);        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.outc3.a = 2.000000, c3.b = 4.000000c4.a = -6.000000, c4.b = 8.000000c5.a = 3.000000, c5.b = 0.000000c3 == c6 : 1c3 != c4 : 1c1.a = 1.000000, c1.b = 2.000000c2.a = 3.000000, c2.b = 6.000000c3.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++中怎么实现操作符重载,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++
AI