<pre name= "code" class= "CPP" >/**************************************************************************** Complex.hpp:Copyright (c) Bit Software, Inc., All rights reserved. Purpose: Declaring and implementing a complex number class difficulty: **author:xxxcreated time:2015-4-26******************************************************** /#ifndef complex_hpp_included#define Complex_hpp_included#include <iostream >using namespace Std;class complex{public://with default values for constructors complex (double real = 0, double image = 0): _real (Real), _image (IM Age) {cout<< "complex (double real = 0, double image = 0)" <<ENDL; destructor ~complex () {cout<< "~complex ()" <<ENDL;} Copy constructor complex (const complex& D): _image (D._image), _real (d._real) {cout<< "complex (const complex& D ) "<<ENDL;} Assignment operator overloading//Thinking why does the operator= assignment function require a date& return value?? complex& operator= (const complex& D) {cout<< "operator= (const complex& D)" <<endl;if (This! = & Amp;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:" <<_IMAGE<<ENDL<<ENDL;} 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; 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<span style= "font-family:arial, Helvetica, Sans-serif;" > (_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)) </span><span style= "font-family:arial, Helvetica, Sans-serif;" >; </span>}private:double _real;double _image;}; #endif//complex_hpp_included
Main function:
#include "complex.hpp" int main () { //test_complex1 (); cout << "**********************" << Endl; Complex A (1.1,2.2); Complex B (3.3,4.4); Complex C; c = a + B; C.display (); c = A-B; C.display (); c = A * b; C.display (); c = A/b; C.display (); return 0;}
C + + declares and implements a plural class