【C++】複数種類の操作
複数の概念は高校で接触したことがありますが、実部と虚部が含まれています.
For example:2i + 3J;実部と虚部の値は整数型でも浮動小数点型でもよいが,ここでは簡単な複数種類の操作を実現する.
コードは次のとおりです.
For example:2i + 3J;実部と虚部の値は整数型でも浮動小数点型でもよいが,ここでは簡単な複数種類の操作を実現する.
コードは次のとおりです.
class Complex
{
public:
Complex(double real,double imag)
{
_real = real;
_imag = imag;
}
Complex(const Complex& c)
{
_real = c._real;
_imag = c._imag;
}
~Complex()
{
}
Complex& operator=(const Complex& c)
{
if(&c != this)
{
this->_real = c._real;
this->_imag = c._imag;
}
return *this;
}
Complex operator+(const Complex& c)
{
Complex ret(*this);// +
ret._real += c._real;
ret._imag += c._imag;
return ret;
}
Complex& operator++() // ++ , , &
{
this->_real += 1;
this->_imag += 1;
return *this;
}
Complex operator++(int) // ++ , , &
{
Complex tmp(*this);// ++ ,
this->_real += 1;
this->_imag += 1;
return tmp;
}
Complex& operator+= (const Complex& c)
{
this->_real = this->_real + c._real;
this->_imag = this->_imag + c._imag;
return *this;
}
inline bool operator==(const Complex& c)
{
return (_real == c._real
&& _imag == c._imag);
}
inline bool operator>(const Complex& c)
{
return (_real > c._real
&& _imag > c._imag);
}
inline bool operator>=(const Complex& c)
{
//return (_real >= c._real
// && _imag >= c._imag);
return (*this > c) || (*this == c);// > ==
}
inline bool operator<=(const Complex& c)
{
//return (_real <= c._real
// && _imag <= c._imag);
return !(*this > c);// >
}
bool operator c) || (*this == c);// >=
}
inline void Display()
{
cout<<_real private:="">
:( main , )
int main()
{
Complex c(1,2);
Complex c1(c);
//c1.Display();
//Complex ret = ++c1;
//Complex ret = c1++;
//ret.Display();
Complex ret(c1);
ret.Display();
return 0;
}