"C + +" complex class operations

Source: Internet
Author: User

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&AMP;&NBSP;C)     {        _real =  c._real;        _imag = c._imag;     }    ~complex ()     {             }        complex& operator= ( CONST&NBSP;COMPLEX&AMP;&NBSP;C)     {        if ( &c != this)         {         this->_real = c._real;        this->_imag = c._imag;         }        return *this;     }        complex operator+ (const  COMPLEX&AMP;&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&AMP;&NBSP;C)     {        return   (_real == c._real             &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&AMP;&AMP;&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&AMP;&NBSP;C)     {                 //return  (_real >= c._real        //   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&AMP;&AMP;&NBSP;_IMAG&NBSP;&GT;=&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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.