The implementation of the default member function for the plural class. Subtraction, self-increment, self-reduction realization.
#include <iostream>using namespace std;class complex{public://display Void display () {cout <<_real<< "+" <<_image<< "I" <<ENDL;} Constructor Complex (double x=0.0,double y=0.0) {_real=x;_image=y;} destructor ~complex () {;//cout<< "destructor" <<ENDL;} Copy construction Complex (CONST&NBSP;COMPLEX&&NBSP;C1) {if (THIS!=&C1) {_real=c1._real;_image=c1._image;} cout<< "Copy construction" <<ENDL;} Addition Complex operator+ (COMPLEX&NBSP;&C1) {complex c2;c2._real=_real+c1._real;c2._image=_image+c1._ IMAGE;RETURN&NBSP;C2;} Subtraction complex operator-(COMPLEX&NBSP;&C1) {complex c2;c2._real=_real-c1._real;c2._image=_image-c1._ IMAGE;RETURN&NBSP;C2;} Multiplication complex operator * (complex &c1) {complex c2;c2._real=_real*c1._real;c2._image=_ IMAGE*C1._IMAGE;RETURN&NBSP;C2;} Division complex operator/(COMPLEX&NBSP;&C1) {complex c2;c2._real=_real/c1._real;c2._image=_image/c1._ IMAGE;RETURN&NBSP;C2;} Plus et complex& operator+= (const COMPLEX&NBSP;&C1) {_real+=c1._real;_image+=c1._image;return *this;} Minus complex& operator-= (CONST&NBSP;COMPLEX&NBSP;&C1) {_real-=c1._real;_image-=c1._image;return *this;} Front ++complex& operator ++ () {++_real;++_image;return *this;} Post ++complex operator ++ (int) {complex tmp (*this); This->_real++;this->_image++;return tmp;} Front--complex& operator--() {_real--;_image--;return *this;} Post--complex operator--(int) {complex tmp (*this); this->_real--;this->_image--;return tmp;} Judge Size c1>c2complex () {;} private:double _real;double _image;}; Int main () {complex c1 (1.0,2.0), C2 (2.0,2.0),c3;cout<< "c1="; C1. Display ();cout<< "c2="; C2. Display ();/*c3=c1+c2;cout<< "c1+c2="; C3. Display ();c3=c1-c2;cout<< "c1-c2="; C3. Display ();c3=c1*c2;cout<< "c1*c2="; C3. Display ();c3=c1/c2;cout<< "c1/c2="; C3. Display ();c1+=c2;cout<< "c1="; C1. Display (); c1-=c2;cout<< "c1="; C1. Display (); */c3=c2+ (--C1);cout<< "c3="; C3. Display ();cout<< "c1="; C1. Display (); System ("pause"); return 0;}
To write a plural class, understand the three features of C + + and the implementation of the default member function.
This article is from the "sunshine225" blog, make sure to keep this source http://10707460.blog.51cto.com/10697460/1754048
Implementation of the plural class