c++面向对象程序设计课后习题答案Word下载.docx
《c++面向对象程序设计课后习题答案Word下载.docx》由会员分享,可在线阅读,更多相关《c++面向对象程序设计课后习题答案Word下载.docx(18页珍藏版)》请在冰豆网上搜索。
if(z<
m)m=z;
return(m);
8:
#include<
b;
a+b<
}
9:
{intadd(intx,inty);
c=add(a,b);
intadd(intx,inty)
{intc;
c=x+y;
return(c);
10:
{voidsort(intx,inty,intz);
intx,y,z;
x>
y>
z;
sort(x,y,z);
voidsort(intx,inty,intz)
inttemp;
if(x>
y){temp=x;
x=y;
y=temp;
}2-4-12-4-22-5-12-5-2Boxbox1Boxbox1core;
intk=0;
for(inti=1;
i<
5;
i++)
if(arr[i].score>
max_score){max_score=arr[i].score;
k=i;
arr[k].num<
"
max_score<
classStudent
{public:
Student(intn,floats):
num(n),score(s){}
voidchange(intn,floats){num=n;
score=s;
voiddisplay(){cout<
num<
score<
private:
intnum;
floatscore;
};
{Studentstud(101,;
();
(101,;
解法一
{public:
voiddisplay(){cout<
otal();
Product:
:
display();
}
10:
classDate;
classTime
Time(int,int,int);
friendvoiddisplay(constDate&
constTime&
);
inthour;
intminute;
intsec;
Time:
Time(inth,intm,ints)
{hour=h;
minute=m;
sec=s;
classDate
Date(int,int,int);
intmonth;
intday;
intyear;
Date:
Date(intm,intd,inty)
{month=m;
day=d;
year=y;
voiddisplay(constDate&
d,constTime&
t)
{
/"
Timet1(10,13,56);
Dated1(12,25,2004);
display(d1,t1);
11:
classTime;
Date(int,int,int);
friendTime;
private:
Date(intm,intd,inty):
month(m),day(d),year(y){}
voiddisplay(constDate&
Time:
Time(inth,intm,ints):
hour(h),minute(m),sec(s){}
voidTime:
display(constDate&
d)
hour<
minute<
sec<
(d1);
12:
template<
classnumtype>
classCompare
Compare(numtypea,numtypeb);
numtypemax();
numtypemin();
numtypex,y;
template<
Compare<
numtype>
Compare(numtypea,numtypeb)
{x=a;
y=b;
numtypeCompare<
max()
{return(x>
y)x:
y;
min()
{return(x<
{Compare<
int>
cmp1(3,7);
()<
istheMaximumoftwointegernumbers."
istheMinimumoftwointegernumbers."
endl<
Compare<
float>
cmp2,;
istheMaximumoftwofloatnumbers."
cout<
istheMinimumoftwofloatnumbers."
char>
cmp3('
a'
'
A'
istheMaximumoftwocharacters."
istheMinimumoftwocharacters."
第四章
1:
classComplex
Complex(){real=0;
imag=0;
Complex(doubler,doublei){real=r;
imag=i;
doubleget_real();
doubleget_imag();
voiddisplay();
doublereal;
doubleimag;
doubleComplex:
get_real()
{returnreal;
get_imag()
{returnimag;
voidComplex:
display()
{cout<
("
real<
"
imag<
i)"
Complexoperator+(Complex&
c1,Complex&
c2)
returnComplex()+(),()+());
{Complexc1(3,4),c2(5,-10),c3;
c3=c1+c2;
c3="
2:
Complexoperator+(Complex&
c2);
Complexoperator-(Complex&
Complexoperator*(Complex&
Complexoperator/(Complex&
ComplexComplex:
operator+(Complex&
{Complexc;
=real+;
=imag+;
returnc;
operator-(Complex&
=;
operator*(Complex&
=real**;
=imag*+real*;
operator/(Complex&
=(real*+imag*/*+*;
=(imag**/*+*;
c1+c2="
c3=c1-c2;
c1-c2="
c3=c1*c2;
c1*c2="
c3=c1/c2;
c1/c2="
3:
"
president"
135BeijingRoad,Shanghai"
(021)"
;
();
10:
cstring>
classTeacherum<
staf[i].name<
staf[i].age<
staf[i].pay<
((char*)&
staf[i],sizeof(staf[i]));
pleaseinputdatayouwantinsert:
for(i=0;
2;
{cin>
(0,ios:
end);
staf1,sizeof(staf1));
beg);
7;
{((char*)&
staf[i].num<
boolfind;
enternumberyouwantsearch,enter0tostop."
num;
while(num)
{find=false;
{((char*)&
if(num==staf[i].num)
{m=();
isNo."
m/sizeof(staf1)<
find=true;
break;
if(!
find)
can'
tfind"
6:
解法一
strstream>
structstudent
{intnum;
charname[20];
doublescore;
};
{studentstud[3]={1001,"
Li"
78,1002,"
Wang"
,1004,"
Fun"
90},stud1[3];
charc[50];
inti;
ostrstreamstrout(c,50);
3;
strout<
stud[i].num<
stud[i].name<
stud[i].score<
ends;
arrayc:
istrstreamstrin(c,50);
strin>
stud1[i].num>
stud1[i].name>
stud1[i].score;
datafromarrayctoarraystud1:
stud1[i].num<
stud1[i].name<
stud1[i].score<
解法二
{inti;
studentstud[3]={1001,"
strstreamstrio(c,50,ios:
in|ios:
out);
strio<
strio>
第八章
cmath>
doubleq(double,double,double);
voidmain()
{doublea,b,c,p,x1,x2;
pleaseentera,b,c:
p=-b/(2*a);
try
{x1=p+q(a,b,c);
x2=p-q(a,b,c);
x1="
x1<
x2="
x2<
catch(doubled)
{cout<
a="
a<
b="
b<
c="
disc="
d<
error!
end"
doubleq(doublea,doubleb,doublec)
{doubledisc;
disc=b*b-4*a*c;
if(disc<
0)throwdisc;
returnsqrt(disc)/(2*a);
2:
string>
Student(intn,stringnam)
constructor-"
n<
num=n;
name=nam;
~Student(){cout<
destructor-"
voidget_data();
stringname;
voidStudent:
get_data()
{if(num==0)thrownum;
elsecout<
name<
inget_data()"
voidfun()
{Studentstud1(1101,"
tan"
{Studentstud2(0,"
catch(intn)
num="
mainbegin"
callfun()"
fun();
mainend"
//mainfile
#include"
usingnamespacestudent1;
{Studentstud1(1001,"
18,"
123BeijingRoad,Shanghua"
student2:
Studentstud2(1102,"
f'
//,文件名为
namespacestudent1
{classStudent
Student(intn,stringnam,inta,stringaddr)
{num=n;
age=a;
address=addr;
voidshow_data();
intage;
stringaddress;
voidStudent:
show_data()
num:
name:
age:
age