运算符重载(复数类(Complex))

环境:win10,vs2013
复数运算法则
运算后结果仍然是一个复数

1.加法
两个复数的和依然是复数,它的实部是原来两个复数实部的和,它的虚部是原来两个复数虚部的和
z1=a+bi,z2=c+di
它们的和是:(a+bi)+(c+di)=(a+c)+(b+d)i

2.减法
z1=a+bi,z2=c+di
它们的差是:(a+bi)-(c+di)=(a-c)+(b-d)i

3.乘法
z1=a+bi,z2=c+di
它们的积是:(a+bi)(c+di)=(ac-bd)+(bc+ad)i

4.除法
z1=a+bi,z2=c+di
它们的商是:(a+bi)/(c+di)=((ac+bd)+(bc-ad)i)/(c^2+d^2)

Complex.h

using namespace std;
#include
#include
class Complex
{
public :
    Complex();//默认构造函数
    Complex(double real, double image);//构造函数(构造对象)
    //赋值操作符重载需要注意:1.自己给自己赋值;2.返回值是引用的形式:3.返回当前对象;4.参数,传引用
    Complex& operator=(const Complex&d);//赋值操作符重载
    bool operator==(const Complex&c);//重载==
    bool operator!=(const Complex&c);//重载!=
    Complex(const Complex&c);//拷贝构造函数
    Complex operator+(const Complex&c);//重载+
    Complex operator*(const Complex&c);//重载*
    Complex operator-(const Complex&c);//重载-
    Complex operator/(const Complex&c);//重载/
    Complex operator+=(const Complex&c);//重载+=
    Complex operator-=(const Complex&c);//重载-=
    Complex operator*=(const Complex&c);//重载*=
    Complex operator/=(const Complex&c);//重载/=
    Complex operator++(int c);//后置++
    Complex& operator++();//前置++
    Complex operator--(int c);//后置--
    Complex& operator--();//前置--
    void Print();
private:
    double _real;
    double _image;
    friend ostream&operator<<(ostream&_cout, Complex&c);//输出运算符重载

};

Complex.cpp

#include"Complex.h"
void Complex::Print()
{
    cout << _real << "+" << _image << "i" << endl;
}
ostream&operator<<(ostream&_cout, Complex&c)//输出运算符重载
{
    _cout << c._real << "+" <"i";
    return _cout;
}

Complex::Complex()//默认构造函数
{
    _real = 1.3;
    _image = 1.4;
}
Complex::Complex(double real=0.1, double image=0.2)
:_real(real)
, _image(image)
{}
Complex::Complex(const Complex&c)//拷贝构造函数
{
    _real = c._real;
    _image = c._image;
}

Complex& Complex::operator=(const Complex&c)//赋值运算符重载
{
    if (*this != c)
    {
        _real = c._real;
        _image = c._image;
    }
    return *this;
}
bool Complex::operator==(const Complex&c)//重载==
{
    if ((_real == c._real) && (_image = c._image))
        return true;
    return false;
}
bool Complex::operator!=(const Complex&c)//重载!=
{
    return!(*this == c);
}
Complex Complex::operator+(const Complex&c)//重载+
{
    return Complex(_real + c._real, _image + c._image);
}
Complex Complex::operator*(const Complex&c)//重载*
{
    return Complex(  ( (_real*c._real) - (_image*c._image)), ((_image*c._real) + (_real*c._image) )  );
}
Complex Complex::operator-(const Complex&c)//重载—
{
    return Complex(_real - c._real, _image - c._image);
}
Complex Complex::operator+=(const Complex&c)//重载+=
{
    _real = _real+c._real;
    _image = _image+c._image;
    return *this;
}
Complex Complex::operator-=(const Complex&c)//重载-=
{
    _real = _real - c._real;
    _image = _image -c._image;
    return *this;
}
Complex Complex::operator++(int c)//后置++
{
    Complex temp(*this);
    _real += 1;
    _image += 1;
    return temp;
}
Complex& Complex::operator++()//前置++
{
    _real += 1;
    _image += 1;
    return *this;
}
Complex Complex::operator--(int c)//后置——
{
    Complex temp(*this);
    _real -= 1;
    _image -= 1;
    return temp;
}
Complex& Complex::operator--()//前置--
{
    _real -= 1;
    _image -= 1;
    return *this;
}

test.cpp

#include"Complex.h"
int main()
{
    Complex c1(1.1, 2.2);
    c1.Print();
    Complex c2(3.3, 4.4);
    c2.Print();
    Complex c3;
    c3.Print();
    c3 = c2;
    cout << c3 << endl;
    cout << (c3 != c1) << endl;
    cout << (c3 == c1) << endl;
    cout << c1 + c2 << endl;
    cout << c1 - c2 << endl;
    cout << c1 * c2 << endl;
    cout << endl;
    Complex c4(5.5, 6.6);
    c4.Print();
    cout << (c1 += c4) << endl;
    cout << (c1 -= c4) << endl;
    cout << endl;
    cout << c1++ << endl;
    cout << c2-- << endl;
    cout << ++c3 << endl;
    cout << --c4 << endl;
    system("pause");
    return 0;
}

运行结果:
运算符重载(复数类(Complex))_第1张图片

你可能感兴趣的:(C++)