Implementation of the plural class

Source: Internet
Author: User

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" &LT;&LT;ENDL;} Constructor Complex (double x=0.0,double y=0.0) {_real=x;_image=y;} destructor ~complex () {;//cout<< "destructor" &LT;&LT;ENDL;} Copy construction Complex (CONST&NBSP;COMPLEX&AMP;&NBSP;C1) {if (THIS!=&AMP;C1) {_real=c1._real;_image=c1._image;} cout<< "Copy construction" &LT;&LT;ENDL;} Addition Complex operator+ (COMPLEX&NBSP;&AMP;C1) {complex c2;c2._real=_real+c1._real;c2._image=_image+c1._ IMAGE;RETURN&NBSP;C2;} Subtraction complex operator-(COMPLEX&NBSP;&AMP;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;&AMP;C1) {complex c2;c2._real=_real/c1._real;c2._image=_image/c1._ IMAGE;RETURN&NBSP;C2;} Plus et complex& operator+= (const COMPLEX&NBSP;&AMP;C1) {_real+=c1._real;_image+=c1._image;return *this;} Minus complex& operator-= (CONST&NBSP;COMPLEX&NBSP;&AMP;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

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.