#pragmaOnce#ifndef Complex0_h_#defineComplex0_h_#include<iostream>classcomplex{Private: DoubleReal; DoubleIMGN; Public: Complex (); Complex (DoubleADoubleb); ~Complex (); Complexoperator+(ConstComplex & A)Const; Complexoperator-(ConstComplex & A)Const; Complexoperator~(); Complexoperator*(Doublec); Complexoperator*(ConstComplex & A)Const; //friendFriend Complexoperator*(Const DoubleCConstComplex &a); Friend Std::ostream&operator<< (Std::ostream & OS,ConstComplex &a);//don't make any space behind the operator .Friend Std::istream &operator>> (Std::istream & OS, Complex &a);//Cin was in the IStream.};#endif // ! Complex0_h_//in summary, if a double n =0 is written in the second constructor, double b = 0; you don't have to write complex (), two are equivalent, there are ambiguities .//so try not to write in the constructor parameter = 0, because there will be ambiguity, honestly use complex () and the second constructor is good//anything that doesn't change is set to Const.
---------------------------------
#include"complex0.h"Complex::complex () {Real= IMGN =0;} Complex::complex (DoubleN1,Doublen2) {Real=N1; IMGN=N2;} Complex Complex::operator+(ConstComplex & A)Const{Complex temp; Temp.real= Real +A.real; TEMP.IMGN= IMGN +A.IMGN; returntemp; /*return Complex (real + a.real, IMGN + A.IMGN);*///calling constructors directly//be accustomed to using this simplified statement//In general, the last statement, equivalent to the previous three, call the constructor will produce a temporary object, assigned to the left}complex Complex::operator-(ConstComplex & A)Const{Complex temp; Temp.real= Real-A.real; TEMP.IMGN= IMGN-A.IMGN; returntemp;} Complex Complex::operator*(ConstComplex & A)Const{Complex temp; Temp.real= Real * a.real + IMGN *A.IMGN; TEMP.IMGN= Real * A.IMGN + IMGN *A.real; returntemp;} Complex Complex::operator~() {IMGN=0-IMGN; return* This;} Complex Complex::operator*(Doublec) { returnComplex (c * Real, C *IMGN);}//friendComplexoperator*(Const DoubleCConstComplex & A)//do not call: Because friend is not inside the class, although it is declared inside the class{ returnComplex (c * a.real, C *A.IMGN);} Std::ostream&operator<< (Std::ostream & OS,ConstComplex &a) {OS<<" is"<<"( "<< A.real <<", "<< A.IMGN <<"i)"; returnOS;} Std::istream&operator>> (Std::istream & OS, Complex & A)//cin in the IStream.{std::cout<<"Real:"; OS>>A.real; Std::cout<<"Imaginary:"; OS>>A.IMGN; returnOS;} Complex::~Complex () {}
C++primer Plus 11th chapter 7th of Programming question