在這裡實現復數類運算符的重載,網上比較少的是對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);
}
如有不足希望指正,有疑惑希望留言一起探討