C++之操作符重载学习总结(二)

一、完善的复数类:

在上一篇文章里面我们已经提到了操作符重载的概念和使用,同时也举例了一个数学里面的复数操作,从一开始使用友元到使用操作符重载全局函数,再到使用操作符重载类成员函数,这样一步步演变而成我们最终实现了复数的实部加实部,虚部加虚部;而且当时我们只讲解了一个操作重载符“+”,所以为了完善学习体系,咋们今天继续把剩下的操作重载符总结完,以免知识体系零零散散。那么复数完善的操作符还有那些呢,其实很简单就能能想到,和对数学里面的实数操作一样,加减乘除肯定是少不了嘛,下面是汇总的操作符总结:

  • 运算:+、-、*、/

  • 比较:==、!=

  • 赋值:=

  • 求模:modulus

2、各操作符重载的形式:

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(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++规定赋值操作符"="只能重载为成员函数

  • 操作符重载不能改变原操作符的优先级

  • 操作符重载不能改变操作数的个数

  • 操作符重载不应改变操作符的原有语义

二、总结:

  • 复数的概念可以通过自定义类实现

  • 复数中的运算操作可以通过操作符重载实现

  • 赋值操作符只能通过成员函数实现

  • 操作符重载的本质为函数定义

好了,今天的分享就到这里,如果文章中有错误或者不理解的地方,可以交流互动,一起进步。我是txp,下期见!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值