C + + operator overloading

Source: Internet
Author: User

<span style= "font-family:arial, Helvetica, Sans-serif;" > #include <iostream></span>
Using namespace Std;class complex{public://constructor with default value complex (double real = 0, double image = 0): _real (Real), _image (image) {cout<< "complex (double real = 0, double image = 0)" &LT;&LT;ENDL; destructor ~complex () {cout<< "~complex ()" &LT;&LT;ENDL;} Copy constructor complex (const complex& D): _image (D._image), _real (d._real) {cout<< "complex (const complex& D ) "&LT;&LT;ENDL;} The assignment operator Overloads complex& operator= (const complex& D) {cout<< "operator= (const complex& D)" <<endl;if ( This! = &d) {this->_real = D._real;this->_image = D._image;} return *this;} Fetch address operator overload complex* operator& () {cout<< "operator& ()" <<endl;return this; The const-Modified fetch address operator Overloads Const complex* operator& () const{cout<< "operator& () const" <<endl;return this;} void display () {cout<< "real:" <<_real<< "--image:" &LT;&LT;_IMAGE&LT;&LT;ENDL&LT;&LT;ENDL;} Please implement the following operator overloads! complex& operator++ ()//front + +//Returns the current object itself {cout<< "operator++ ()" &Lt;<endl;        _real++;                  _image++;return *this;        Can immediately reflect the value of the front + +}complex operator++ (int)//Post ++{cout<< "operator++ (int)" <<endl;        Complex C = *this;        this->_real++;        this->_image++; return c;}        complex& operator--() {cout<< "operator--()" <<endl;        _real--;        _image--; return *this;}        Complex operator--(int)//--{cout<< "operator--()" <<endl;        Complex C = *this;        this->_real--;        this->_image--; return c;}        Complex operator+ (const complex& c) {cout<< "operator+ (const complex& c)" <<endl; Return complex (_real+c._real,_image+c._image);}        Complex operator-(const complex& c) {cout<< "operator-(const complex& c)" <<endl; Return complex (_real-c._real,_image-c._image);}  complex& operator-= (const complex& c) {cout<< "operator-= (const complex& c)" <<endl;      _real-= C._real;        _image-= C._image; return *this;}        complex& operator+= (const complex& c) {cout<< "operator+= (const complex& c)" <<endl;        _real + = C._real;        _image + = C._image; return *this;}        Complex operator* (const complex& c) {cout<< "operator* (const complex& c)" <<endl; Return complex (_real*c._real-_image*c._image,c._image*_real+_image*c._real);}        Complex operator/(const complex& c) {cout<< "operator/(const complex& c)" <<endl; Return complex (_real/c._real+_image*c._image)/(C,_real*c._real+c._image*c._image), (_image*c._real-_real*c._ Image)/(C,_real*c._real+c._image*c._image));} Private:double _real;double _image;};

C + + operator overloading

Related Article

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.