在这里实现复数类运算符的重载,网上比较少的是对cout cin的重载,以及数学运算*,/,一下是我实现的声明:
Complex.h:
#ifndef __COMPLEX__
#define __COMPLEX__
#include <iostream>
using namespace std;
class Complex
{
public:
friend ostream & operator << (ostream& out, const Complex& num)//重载cout,cin,课一直接调
{ //用>>、<<运算符进行输入输
if (num._imgic<0.0) //出,我把定义友元的重载写
{ //到了头文件里
out << num._real << " - " << -(num._imgic) << "i";
}
else
{
out << num._real << " + " << num._imgic << "i";
}
return out;
}
friend istream & operator>> (istream& in, Complex& num)
{
in >> num._real;
in >> num._imgic;
return in;
}
Complex(double real = 0, double imgic = 0)
:_real(real)
, _imgic(imgic)
{
}
~Complex()
{
}
bool operator >(const Complex & num);
bool operator <(const Complex & num);
bool operator ==(const Complex & num);
bool operator >=(const Complex & num);
bool operator <=(const Complex & num);
bool operator !=(const Complex & num);
Complex operator + (const Complex & num);//算数运算符的重载
Complex operator - (const Complex & num);
Complex operator ++ ();//前置++、--,后置++、--在这里我全部实现为+、-实部
Complex operator ++ (int);//对虚部不做运算
Complex operator -- ();
Complex operator -- (int);
Complex operator += (const Complex & num);
Complex operator -= (const Complex & num);
Complex operator * (const Complex & num);//乘、除运算的实现
Complex operator *= (const Complex & num);//若实现 / 运算功能,我们必须先实现~(共轭)
Complex operator / (Complex & num);
Complex operator /= (Complex & num);
Complex operator ~ ();//共轭运算符的实现
private:
double _real;
double _imgic;
};
#endif
实现代码如下,Complex.cpp:
#include "Complex.h"
bool Complex:: operator >(const Complex & num)
{
if ((_real*_real + _imgic*_imgic) >
(num._real*num._real + num._imgic*num._imgic))
return true;
return false;
}
bool Complex:: operator <(const Complex & num)
{
return !(*this >= num);
}
bool Complex:: operator ==(const Complex & num)
{
if ((_real == num._real) &&
(_imgic == num._imgic))
return true;
return false;
}
bool Complex:: operator != (const Complex & num)
{
return !(*this == num);
}
bool Complex:: operator >=(const Complex & num)
{
return (*this > num) || (*this == num);
}
bool Complex:: operator <=(const Complex & num)
{
return !(*this > num);
}
Complex Complex:: operator + (const Complex & num)
{
return Complex(_real + num._real, _imgic + num._imgic);
}
Complex Complex:: operator - (const Complex & num)
{
return Complex(_real - num._real, _imgic - num._imgic);
}
Complex Complex:: operator ++ ()
{
_real++;
return *this;
}
Complex Complex:: operator ++ (int)
{
Complex ret = *this;
_real++;
return *this;
}
Complex Complex:: operator -- ()
{
_real--;
return *this;
}
Complex Complex:: operator -- (int)
{
Complex ret = *this;
_real--;
return *this;
}
Complex Complex:: operator += (const Complex & num)
{
_real += num._real;
_imgic += num._imgic;
return *this;
}
Complex Complex:: operator -= (const Complex & num)
{
_real -= num._real;
_imgic -= num._imgic;
return *this;
}
Complex Complex:: operator * (const Complex & num)
{
return Complex((_real*num._real - _imgic*num._imgic),
(_real*num._imgic + (num._imgic*_real)));
}
Complex Complex:: operator *= (const Complex & num)
{
return*this = *this*num;
}
Complex Complex:: operator / (Complex & num)
{
return Complex(((*this*(~num))._real) / ((num*(~num))._real),
((*this*(~num))._imgic) / ((num*(~num))._real));
}
Complex Complex:: operator /= (Complex & num)
{
Complex ret = *this / num;
return ret;
}
Complex Complex:: operator ~ ()
{
return Complex(_real, -_imgic);
}
如有不足希望指正,有疑惑希望留言一起探讨