Implementation of the "C + +" plural class

Source: Internet
Author: User

#include <iostream>using namespace Std;class complex{private:double _real; double _image;public:complex (double  Real = 2.2,double image=1.1)//constructor {cout<< "constructor called" <<endl;  _real = Real; _image = image;  } Complex (const complex& D)//copy constructor {cout<< "copy constructor called" <<endl;  This->_real = D._real; This->_image = D._image;  } ~complex () {cout<< "destructor called" <<endl;} void Display () {cout<< "Real:" <<_real; cout<< "Image:" <<_image<<endl;}   public:complex& operator= (const complex& D) {if (This! = &d) {cout<< "assignment operator is overloaded" <<endl;   This->_real = D._real;  This->_image = D._image; } return *this;  } complex& operator++ () {cout<< "front + + is overloaded" <<endl;  this->_real++; return *this;  } Complex operator++ (int) {cout<< "Post + + is overloaded" <<endl;  Complex *tmp = this;  this->_real++; return *tmp; } complex& operator--() {cout<< "front-overloaded" <<endl;  This->_real--; return *this;  } Complex operator--(int) {cout<< "post-overloaded" <<endl;  Complex *tmp = this;  This->_real--; return *tmp;  } Complex operator+ (const complex& D) {cout<< "+ overloaded" <<endl;  Complex tmp;  Tmp._real = This->_real + d._real;  Tmp._image = This->_image + d._image; return TMP;  } Complex operator-(const complex& D) {cout<< "-overloaded" <<endl;  Complex tmp;  Tmp._real = This->_real-d._real;  Tmp._image = this->_image-d._image; return TMP;  } complex& operator-= (const complex& D) {cout<< "-= is overloaded" <<endl;  This->_real-= D._real;  This->_image-= D._image; return *this;  } complex& operator+= (const complex& D) {cout<< "+ = is overloaded" <<endl;  This->_real + = D._real;  This->_image + = D._image; return *this;  } Complex operator* (const complex& D) {Complex tmp;  Tmp._real = this->_real * D._real-this->_image * d._image; Tmp._image = This->_Image * D._real + this->_real * d._image; return TMP;  } Complex operator/(const complex& D) {Complex tmp;  Tmp._real = (This->_real * d._real + this->_image * d._image)/(D._real * d._real +d._image * d._image);  Tmp._image = (This->_image * d._real-this->_real * d._image)/(D._real * d._real +d._image * d._image); return TMP; }}; int main () {Complex D1; D1. Display (); Complex D2 (D1); Complex d2 = D1; D2. Display (); Complex D3 (4.4,5.5); D3. Display (); D3 = D2; D3.    Display (); ++D3; D3. Display (); d3++; D3. Display (); --D3; D3. Display (); d3--; D3. Display (); System (pause); D3 = D3+D1; D3. Display (); D3 = D3-D1; D3. Display ();    D3-=D1; D3. Display (); GetChar (); return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Implementation of the "C + +" plural class

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.