The concept of plural we have been exposed in high school, including the real and imaginary parts,
For Example:2i + 3J, the real and imaginary values can be integral types, or floating-point types, where a simple plural class operation is implemented
The code is as follows:
Class complex{public: complex (Double real,double imag) { _real = real; _imag = imag; } complex (const COMPLEX&&NBSP;C) { _real = c._real; _imag = c._imag; } ~complex () { } complex& operator= ( CONST&NBSP;COMPLEX&&NBSP;C) { if ( &c != this) { this->_real = c._real; this->_imag = c._imag; } return *this; } complex operator+ (const COMPLEX&&NBSP;C) { complex ret ( *this);// + operation returns the value after Operation ret._real += c._real; ret._imag += c._imag; return ret; } complex& operator++ () // front ++ , When you need this return value outside, you need to add & { this->_real += 1; this->_imag += 1; return *this; } complex operator++ (int) // rear ++ There is no need to add & { complex tmp when this return value is not needed outside ( *this);//Post + + returns the original value, so define a temporary variable to save 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&NBSP;COMPLEX&&NBSP;C) { return (_real == c._real &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&&&NBSP;_IMAG&NBSP;==&NBSP;C._IMAG); } inline bool operator> (const complex& c) { return (_real > c._real && _imag > &NBSP;C._IMAG); } inline bool operator>= (const &NBSP;COMPLEX&&NBSP;C) { //return (_real >= c._real // &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&&&NBSP;_IMAG&NBSP;>=&NBSP;C._IMAG); return (*this > c) | | (*this == c);//directly using the above implementation of the overloaded characters > and == } inline bool operator<= (const complex& c) { //return (_real <= c._real // && _imag <= c._ IMAG); return ! (*this > c);//directly using the above implementation of the overloaded character > } bool operator< (const complex& c) { return ! (*this > c) | | (*this == c);//directly using the above implementation of the overloaded characters >= } Inline void display () { cout <<_real<< "-" <<_imag<<endl; }private: double _real; double _imag;};
Test unit: (lazy is tested in the main function, you can individually encapsulate a function to test)
int main () {Complex C (up); Complex C1 (c); C1. Display (); Complex ret = ++C1; Complex ret = c1++; Ret. Display (); COMPLEX ret (C1); Ret. Display (); return 0;}
This article from "vs LV" blog, declined reprint!
"C + +" complex class operations