31_完善的复数类

1. 完善的复数类

  • 复数类应该具有的操作:

运算:+、-、*、/
比较:==、!=
赋值:=
求模:modulus

编程说明:完善的负数类实现
Complex.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

Complex.cpp

#include 
#include 
#include "Complex.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)
{
    Complex ret;
    
    ret.a = this->a + c.a;
    ret.b = this->b + c.b;
    
    return ret;
}

Complex Complex::operator - (const Complex& c)
{
    Complex ret;
    
    ret.a = this->a - c.a;
    ret.b = this->b - c.b;
    
    return ret;
}

Complex Complex::operator * (const Complex& c)
{
    Complex ret;
    
    ret.a = this->a * c.a - this->b * c.b;
    ret.b = this->b * c.a + this->a * c.b;
    
    return ret;
}

Complex Complex::operator / (const Complex& c)
{
    Complex ret;
    double cm = c.a * c.a + c.b * c.b;
    
    ret.a = (this->a * c.a + this->b * c.b) / cm;
    ret.b = (this->b * c.a - this->a * c.b) / cm;
    
    return ret;
}

bool Complex::operator == (const Complex& c)
{
    return (this->a == c.a) && (this->b == c.b);
}


bool Complex::operator != (const Complex& c)
{
    return !(*this == c);
}
    
Complex& Complex::operator = (const Complex& c)
{
    if( this != &c)     
    {
        this->a = c.a;
        this->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;
}

输出结果:

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

2. 注意事项

  • C++规定赋值操作符(=)只能重载为成员函数
  • 操作符重载不能改变原操作符的优先级
  • 操作符重载不能改变操作数的个数
  • 操作数重载不应改变操作符的原有语义

3. 小结

  • 复数的概念可以通过自定义类实现
  • 复数中的运算操作可以通过操作符重载实现
  • 赋值操作符只能通过成员函数实现
  • 操作符重载的本质为函数定义,即定义新函数来扩展已有系统功能

你可能感兴趣的:(31_完善的复数类)