Complex.h
1 #pragmaOnce2#include <iostream>3 4 using namespacestd;5 //represents a complex number6 classComplex7 {8 Private:9 Doublex, y;Ten Public: OneComplex ();//default constructor AComplex (DoubleXDoubley);//Constructor Overloading -Complexoperator+ (Complex & CMX)Const;//' + ' operator overloading -Complexoperator-(Complex & CMX)Const;//'-' operator overloading theComplexoperator* (Complex & CMX)Const;//' * ' operator overloading -Complexoperator/(Complex & CMX)Const;//'/' operator overloading -Complexoperator~()Const;//' ~ ' operator overloading -Complexoperator*(Double& N)Const;//' * ' operator overloading +Friend Complexoperator*(Double& N, Complex & CMX);//friend function, which can be used to represent [4.0*complex type] -Friend Ostream &operator<< (ostream & OS, Complex & CMX);//friend function, which can be used to denote [cout << complex Type] +~Complex (); A};
Complex.cpp
1#include"Complex.h"2 3 4 5 Complex::complex ()6 {7 }8 9Complex::complex (DoubleXDoubley)Ten { One This->x =x; A This->y =y; - } - theComplex Complex::operator+ (Complex & CMX)Const - { - returnComplex (x + cmx.x, y +cmx.y); - } + -Complex Complex::operator-(Complex & CMX)Const + { A returnComplex (x-cmx.x, Y-cmx.y); at } - -Complex Complex::operator* (Complex & CMX)Const - { - returnComplex (x*cmx.x, y*cmx.y); - } in -Complex Complex::operator/(Complex & CMX)Const to { + returnComplex (x/cmx.x, Y/cmx.y); - } the *Complex Complex::operator~()Const $ {Panax Notoginseng returnComplex (x,-y); - } the +Complex Complex::operator*(Double& N)Const A { the returnComplex (X*n, y*n); + } - $Complexoperator*(Double& N, Complex &CMX) $ { - returncmx*N; - } the -Ostream &operator<< (ostream & OS, Complex &CMX)Wuyi { theOS << cmx.x <<","<< cmx.y <<Endl; - returnos; Wu } - Aboutcomplex::~Complex () $ { -}
C + + runner overloading, friend functions