面向对象习题DOCWord文档下载推荐.docx
《面向对象习题DOCWord文档下载推荐.docx》由会员分享,可在线阅读,更多相关《面向对象习题DOCWord文档下载推荐.docx(12页珍藏版)》请在冰豆网上搜索。
![面向对象习题DOCWord文档下载推荐.docx](https://file1.bdocx.com/fileroot1/2023-2/1/5d4e603f-3c70-407c-a452-edc05c08a22b/5d4e603f-3c70-407c-a452-edc05c08a22b1.gif)
};
complexcomplex:
:
operator+(complex&
c2)
returncomplex(real+c2.real,imag+c2.imag);
}
operator-(complex&
returncomplex(real-c2.real,imag-c2.imag);
operator*(complex&
returncomplex(real*c2.real,imag*c2.imag);
operator/(complex&
returncomplex(real/c2.real,imag/c2.imag);
intmain()
complexc1(12,9),c2(2,3),c3;
c1=("
;
c1.display();
cout<
)"
endl;
c2=("
c2.display();
c3=c1+c2;
C1+C2=("
c3.display();
c3=c1-c2;
c1-c2=("
c3=c1*c2;
c1*c2=("
c3=c1/c2;
c1/c2=("
return0;
输出:
c1=(12,9)
c2=(2,3)
C1+C2=(14,12)
c1-c2=(10,6)
c1*c2=(24,27)
c1/c2=(6,3)
Pressanykeytocontinue...
三、定义一个复数类,重载运算符+,用于复数的加法,参加运算的可是类对象,也可以是只有一个整数,
顺序任意,如c1+c2,c1+i,i+c1,设i为整数,编程求两个复数之和,整数和复数之和。
complex(){real=0;
imag=0;
complex(intr,inti):
complex(intr){real=r;
friendcomplexoperator+(constcomplex&
constcomplex&
friendostream&
operator<
(ostream&
complex&
complexoperator+(constcomplex&
c1,constcomplex&
returncomplex(c1.real+c2.real,c1.imag+c2.imag);
ostream&
output,complex&
c)
output<
("
c.real<
c.imag<
returnoutput;
c1="
c1<
c2="
c2<
c1+c2="
c3<
c3=c2+c1;
c2+c1="
c3=c1+8;
c1+8="
c3=8+c1;
8+c1="
c1+c2=(14,12)
c2+c1=(14,12)
c1+8=(20,9)
8+c1=(20,9)
4.8为复数类(Complex)增加重载的运算符-、-=、*=和/=。
设++为实部和虚部各自增一,亦请重载前置与后置++运算符。
分别使用成员函数和友元函数各做一遍。
并测试。
解:
注意后++,返回的是原值,但实部和虚部已各自增一,所以要先保留原值,再++。
这里是使用成员函数。
classcomplex{
doublereal;
//实部
doubleimag;
//虚部
complex(doubler=0.0,doublei=0.0);
//构造函数
voidprint();
//显示复数
complexoperator+(complexc);
//重载复数"
+"
complexoperator-(complexc);
-"
complexoperator*(complexc);
*"
complexoperator/(complexc);
/"
complexoperator+=(complexc);
+="
complexoperator-=(complexc);
-="
complexoperator*=(complexc);
*="
complexoperator/=(complexc);
/="
complexoperator++();
//重载复数前缀"
++"
complexoperator++(int);
//重载复数后缀"
complex:
complex(doubler,doublei){
real=r;
imag=i;
operator+(complexc){//重载复数"
complextemp;
temp.real=real+c.real;
temp.imag=imag+c.imag;
returntemp;
//返回局部变量,函数声明不可为引用,这时返回了局部变量的地址
operator-(complexc){//重载复数"
temp.real=real-c.real;
temp.imag=imag-c.imag;
operator*(complexc){//重载复数"
temp.real=real*c.real-imag*c.imag;
temp.imag=real*c.imag+imag*c.real;
operator/(complexc){//重载复数"
doubled;
d=c.real*c.real+c.imag*c.imag;
temp.real=(real*c.real+imag*c.imag)/d;
temp.imag=(c.real*imag-real*c.imag)/d;
operator+=(complexc){//重载复数"
real=temp.real;
imag=temp.imag;
operator-=(complexc){//重载复数"
operator*=(complexc){//重载复数"
operator/=(complexc){//重载复数"
operator++(){//重载复数前缀"
temp.real=++real;
temp.imag=++imag;
operator++(int){//重载复数后缀"
complextemp(real,imag);
real++;
imag++;
voidcomplex:
print(){//显示复数
cout<
real;
if(imag>
=0)cout<
'
+'
imag<
i'
intmain(){
complexA(30,40),B(15,30),C,D;
//定义4个复数对象
A为:
A.print();
C=A.operator++
(1);
//即C=A++完整的表达式
C=A++后,C为:
C.print();
A为:
C=A.operator++();
//即++A
C=++A后,C为:
C=A*B;
C=A*B后,C为:
A*=B;
A*=B后,A为:
C=A/B;
C=A/B后,C为:
A/=B;
A/=B后,A为:
"
B为:
B.print();
D=A+B+C;
D=A+B+C后,D为:
D.print();
A+=B+=C;
A+=B+=C后,A为:
D=A-B-C;
D=A-B-C后,D为:
A-=B+=C;
A-=B+=C后,A为:
A=B;
A=B后,A为:
D=A*B*C;
D=A*B*C后,D为:
D=D/B/C;
D=D/B/C后,D为:
A*=B*=C;
A*=B*=C后,A为:
A/=B/=C/=C;
A/=B/=C/=C后,A为:
解2:
iostream.h>
friendcomplexoperator+(constcomplex&
constcomplex&
friendcomplexoperator-(constcomplex&
friendcomplexoperator*(constcomplex&
friendcomplexoperator/(constcomplex&
friendcomplex&
operator+=(complex&
operator-=(complex&
operator*=(complex&
operator/=(complex&
friendcomplexoperator++(complex&
int);
real=r;
imag=i;
complexoperator+(constcomplex&
c1,constcomplex&
c2){//重载复数"
temp.real=c1.real+c2.real;
temp.imag=c1.imag+c2.imag;
complexoperator-(constcomplex&
temp.real=c1.real-c2.real;
temp.imag=c1.imag-c2.imag;
complexoperator*(constcomplex&
temp.real=c1.real*c2.real-c1.imag*c2.imag;
temp.imag=c1.real*c2.imag+c1.imag*c2.real;
complexoperator/(constcomplex&
d=(c2.real*c2.real+c2.imag*c2.imag);
temp.real=(c1.real*c2.real+c1.imag*c2.imag)/d;
temp.imag=(c2.real*c1.imag-c1.real*c2.imag)/d;
complex&
operator+=(complex&
c1.real=c1.real+c2.real;
c1.imag=c1.imag+c2.imag;
returnc1;
//返回由引用参数传递过来的变量,函数声明可为引用
operator-=(complex&
c1.real=c1.real-c2.real;
c1.imag=c1.imag-c2.imag;
operator*=(complex&
c1=temp;
operator/=(complex&
complexoperator++(complex&
c){//重载复数前缀"
++c.real;
++c.imag;
returnc;
c,int){//重载复数后缀"
complextemp(c.real,c.imag);
c.real++;
c.imag++;
real;