C++primer Plus 11th chapter 7th of Programming question

Source: Internet
Author: User

#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

Related Article

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.