阅读以下说明和C++代码(代码13-1),将应填入(n)处的字句写在对应栏内。 【说明】软件设计师东方飞龙

5 查阅

阅读以下说明和C++代码(代码13-1),将应填入(n)处的字句写在对应栏内。

【说明】

软件设计师东方飞龙利用UML设计了一个迷你小型复数类,其类图如图13-11所示。

【代码13-l】

/*___________________________________*/

/********* 文件 MiniComplex. h*********/

/*___________________________________*/

include<iostream>

using namespace std;

class MiniComplex

{(1):

//重载流插入和提取运算符

(2) ostream & operator <<(ostream & osObject, const MiniComplex & complex)

{ osObject <<"("<<complex. realPart<<"+"<<complex. imagPart <<"I"<<")";

return osObject;

}

friend (3) operator >>(istream & isObject, MiniComplex & complex)

{ char ch;

isObject >>complex. realPart >>ch>>complex. imagPart >>ch;

return isObject;

}

MiniComplex(double real=0, double imag=0); //构造函数

MiniComplex operator+(const MiniComplex & otherComplex)const! //重载运算符+

MiniComplex operator--(const MiniComplex & otherComplex)const! //重载运算符-

MiniComplex operator*(const MiniComplex& othmComplex)const; //重载运算符*

MiniComplex operator/(const MiniComplex & otherComplex)const; //重载运算符/

bool perator==(const MiniComplex &otherComplex)const; //重载运算符==

private:

double realPart; //存储实部变量

double imagPart; //存储虚部变量

};

/*_______________________________________________________*/

/* * * * * * * * *文件 MiniComplex. cpp* * * * * * * * * */

/*_______________________________________________________*/

include "MiniComplex.h"

bool MiniComplex:: perator==(const MiniComplex & otherComplex)const

{ (1);}

MiniComplex:: MiniComplex(double real, double imag){realPart=real;imagPart=imag!}

MiniComplex MiniComplex:: operator+(const MiniComplex & otherComplex)const

{ MiniComplex temp;

temp. realPart=realPart+ otherComplex. realPart;

temp. imagPart=imagPart+ otherComplex. imagPart;

return temp;

}

MiniComplex MiniComplex::operator--(const MiniComplex & otherComplex)const

{ MiniComplex temp;

temp.realPart=realPart-otherComplex.realPart;

temp. imagPart=imagPart-otherCompler.imagPart;

return temp;

}

MiniComplex MiniComplex:: operator*(const MiniComplex& otherComplex)const

{ MiniComplex temp;

temp.realPart=(realPart* otherComplex.realPart)-(imag-Part* otherComplex.imag-Part);

temp imagPart=(realPart* otherComplex. imagPart)+(imag-Part *otherComplex.realPart);

return temp,

}

MiniComplex MiniComplex:: operator/(const MiniComplex& otherComplex)eonst

{ MiniComplex temp;

float tt;

tt=1/(otherComplex. realPart *otherComplex. realPart+otherComplex. imagPart* other Complex.imagPart);

temp. realPart=((realPart* otherComplex.realPart)+(imagPart* otherComplex.imagPart))*tt;

temp. imagPart=((imagPart * otherComplex.realPart)-(realPart* otherComplex.imagPart))*tt;<

参考答案:

(1)public(2)friend(3)istream&.(4)return(realPart==otherComplex. realPart && imagPart==otherComplex. imagPart)(5)MiniComplex.h(1)public(2)friend(3)istream&.(4)return(realPart==otherComplex. realPart && imagPart==otherComplex. imagPart)(5)MiniComple

软考中级