面向对象程序设计 C山师 第六章习题答案.docx
- 文档编号:27466954
- 上传时间:2023-07-01
- 格式:DOCX
- 页数:38
- 大小:21.05KB
面向对象程序设计 C山师 第六章习题答案.docx
《面向对象程序设计 C山师 第六章习题答案.docx》由会员分享,可在线阅读,更多相关《面向对象程序设计 C山师 第六章习题答案.docx(38页珍藏版)》请在冰豆网上搜索。
面向对象程序设计C山师第六章习题答案
第六章习题答案
一、选择填空
1、A2、C3、D4、B5、D6、A7、C8、A9、D10、A11、C12、A13、B14、C15、C16、D17、B18、C19、A20、D
21、C22、B
二、判断下列描述的正确性,对者划√,错者划×。
1、√2、×3、×4、×5、√6、√7、×8、√9、×10、√
11、√12、√13、√14、√15、×16、√17、√18、√19、√20、×
21、×22、×
三、分析下列程序的输出结果。
1、运行该程序输出结果如下所示。
Defaultconstructorcalled
Constructorcalled
a=0,b=0
a=4,b=8
2、运行该程序输出结果如下所示。
a=7,b=9
3、运行该程序输出结果如下所示。
104
4、运行该程序输出结果如下所示。
1035,789.504
5、运行该程序输出结果如下所示。
1
{}
{0,1,2,3,4,5,6,7,8}
1
{11,12,13,14,15,16,17,18,19}
{19,18,17,16,15,14,13,12,11}
6、运行该程序输出结果如下所示。
Starting1:
Defaultconstructorcalled.
Defaultconstructorcalled.
Defaultconstructorcalled.Eding1:
Starting2:
Constructor:
a=5,b=6
Constructor:
a=7,b=8
Constructor:
a=9,b=10
Ending2:
Destructorcalled.a=9,b=10
Destructorcalled.a=7,b=8
Destructorcalled.a=5,b=6
Destructorcalled.a=5,b=6
Destructorcalled.a=3,b=4
Destructorcalled.a=1,b=2
7、运行该程序输出结果如下所示。
Defaultconstructorcalled.
Defaultconstructorcalled.
Defaultconstructorcalled.
Defaultconstructorcalled.
Destructorcalled.
Constructor1called.
Destructorcalled.
Constructor2called.
Destructorcalled.
x=0,y=0
x=5,y=0
x=2,y=3
Destructorcalled.
Destructorcalled.
Destructorcalled.
8、运行该程序输出结果如下所示。
Constructorcalled.0
Constructorcalled.5
Destructorcalled.5
5
Destructorcalled.5
9、运行该程序输出结果如下所示。
Constructorcalled.5
5
Destructorcalled.5
10、运行该程序输出结果如下所示。
DefaultConstructorcalled.
Constructor:
real=6.8,imag=0
Constructor:
real=5.6,imag=7.9
0+0I
6.8+0I
5.6+7.9I
Constructor:
real=1.2,imag=3.4
Constructor:
real=5,imag=0
DefaultConstructorcalled.
1.2+3.4I
5+0I
0+0I
11、答:
⑴该程序中用string.h中所包含的函数有3种,它们是strcpy()、strcat()和strlen().
⑵该程序中使用了函数重载。
它们是下述两个构造函数重载:
String()和String(constchar*str)。
⑶类中成员函数Setc()的功能是用来设置字符数组中某位置的一个指定字符。
⑷类中成员函数Getc()的功能是用来从某个字符数组中获取指定位置的字符。
⑸类中成员函数Append()的功能是在一个字符数组中追加一个指定的字符串,即将指定的字符串添加到已知串的后边。
⑹不行。
⑺该程序中有两处使用了new运算符。
⑻运行该程序输出结果如下所示。
empty.
astring.
9
astring.
i
thisastring.
四、改正以下程序中的错误。
1、该程序中point类的构造函数定义不正确,在main()中队数据成员的访问不正确,修改如下:
#include
classpoint
{
intx1,x2。
public:
point(intx,inty){point:
:
x=x。
point:
:
y=y。
}
voiddisp()
{
cout< cout< } //…… }。 voidmain() { pointdata(5,5)。 data.disp()。 } 2、在main()函数中的p.x+=5和p.y+=6两个语句是错误的,因为保护数据仅能被类的成员函数访问。 五、按下列要求编写程序。 1、程序内容如下所示。 #include classTest { public: Test(){} Test(inti,intj=20) { t1=i。 t2=j。 t+=j-i。 } staticintfun(Test&T)。 friendintadd(Test&T)。 private: intt1,t2。 staticintt。 }。 intTest: : fun(Test&T) { t+=T.t。 returnt。 } intadd(Test&T) { intn=T.t1+T.t2。 returnn。 } intTest: : t=5。 voidmain() { Testa1,a2(10),a3(15,25)。 cout< cout< : fun(a2)< } 2、程序内容如下所示。 #include #include classProduct { char*name。 intprice。 intquantity。 public: Product(char*n,intp,intq) { name=newchar[strlen(n)+1]。 strcpy(name,n)。 price=p。 quantity=q。 } ~Product() { if(name) { delete[]name。 name=0。 } } voidbuy(intmoney) { intn,r。 n=money/price。 if(n>quantity) cout<<"数量不够"< else { quantity-=n。 r=money%price。 cout<<"产品: "< "< cout< } } voidget()const { cout<<"产品: "< "< } }。 voidmain() { Productp1("电视机",2000,15)。 p1.buy(7000)。 p1.get()。 p1.buy(4500)。 p1.get()。 } 3、程序内容如下所示。 #include #include classCDate { private: intm_nDay。 intm_nMonth。 intm_nYear。 public: CDate()。 CDate(intday,intmonth,intyear)。 voidDisplay()。 voidAddDay()。 voidSetDate(intday,intmonth,intyear)。 ~CDate()。 private: boolIsLeapYear()。 //判断该年是否为闰年 }。 CDate: : CDate() { } CDate: : CDate(intday,intmonth,intyear) { m_nDay=day。 m_nMonth=month。 m_nYear=year。 } voidCDate: : Display() { charday[5]。 charmonth[5]。 charyear[5]。 _itoa(m_nDay,day,10)。 _itoa(m_nMonth,month,10)。 _itoa(m_nYear,year,10)。 cout< } voidCDate: : AddDay() { m_nDay++。 if(IsLeapYear()) { if((m_nMonth==2)&&(m_nDay==30)) { m_nMonth++。 m_nDay=1。 return。 } } else { if((m_nMonth==2)&&(m_nDay==29)) { m_nMonth++。 m_nDay=1。 return。 } } if(m_nDay>31) { if(m_nMonth==12) { m_nYear++。 m_nMonth=1。 m_nDay=1。 } else { m_nMonth++。 m_nDay=1。 } } } voidCDate: : SetDate(intday,intmonth,intyear) { m_nDay=day。 m_nMonth=month。 m_nYear=year。 } CDate: : ~CDate() { } boolCDate: : IsLeapYear() { boolbLeap。 if(m_nYear%4! =0) bLeap=false。 elseif(m_nYear%100! =0) bLeap=true。 elseif(m_nYear%400! =0) bLeap=false。 else bLeap=true。 returnbLeap。 } voidmain() { CDated。 d.SetDate(2001,2,28)。 cout<<"当前日期=>: "。 d.Display()。 d.AddDay()。 cout<<"当前日期加1=>: "。 d.Display()。 } 4、程序内容如下所示。 #include classTc { private: doubleunlead,lead,total。 intunprice,price。 public: Tc(){unprice=17。 price=16。 } voidgetdata() { cout<<"无铅汽油总量;"。 cin>>unlead。 cout<<"有铅汽油总量;"。 cin>>lead。 total=unprice*unlead+price*lead。 } voiddisp() { cout<<"总收入: "< } }。 voidmain() { TcA。 A.getdata()。 A.disp()。 } 5、程序内容如下所示。 #include classCFactorial { intvalue。 intfact。 public: CFactorial(intval)。 voidCalculateFactorial()。 voidDisplay()。 }。 CFactorial: : CFactorial(intval) { value=val。 fact=1。 } voidCFactorial: : CalculateFactorial() { inti=value。 while(i>1) fact*=i--。 } voidCFactorial: : Display() { cout< ="< } voidmain() { CFactorialA(5)。 A.CalculateFactorial()。 A.Display()。 } 6、程序内容如下所示。 #include #include classrectangle { private: floatledge,sedge。 public: rectangle(){}。 rectangle(floata,floatb) { ledge=a。 sedge=b。 }。 floatarea() { returnledge*sedge。 }。 voidaddarea(rectangler1,rectangler2) { cout<<"总面积: "< } }。 voidmain() { rectangleA(3.5,2.5),B(4.2,3.8),C。 C.addarea(A,B)。 } 7、程序内容如下所示。 #include #include classrectangle { private: floatledge,sedge。 public: rectangle(){}。 rectangle(floata,floatb) { ledge=a。 sedge=b。 }。 floatarea() { returnledge*sedge。 }。 voidshowlength() { cout<<"周长: "<<(ledge+sedge)*2< } rectangletlength(rectangler2) { rectangletemp。 temp.ledge=ledge+r2.ledge。 temp.sedge=sedge+r2.sedge。 returntemp。 } }。 voidmain() { rectangleA(3.5,2.5),B(4.2,3.8)。 cout<<"A"。 A.showlength()。 cout<<"B"。 B.showlength()。 rectangleC=A.tlength(B)。 cout<<"C"。 C.showlength()。 } 8、程序内容如下所示。 #include #include classLine { private: doublex1,x2,y1,y2。 public: Line(){}。 Line(doublea,doubleb,doublec,doubled) { x1=a。 y1=b。 x2=c。 y2=d。 cout<<"线段端点"< } friendtwoline(Linel1,Linel2) { doubler1=l2.y1*(l2.x2-l1.x1)-l1.y1*(l2.x2-l2.x1)+(l1.x1-l2.x1)*(l2.y2-l2.y1)。 doubler2=(l1.y2-l1.y1)*(l2.x2-l2.x1)-(l1.x2-l1.x1)*(l2.y2-l2.y1)。 doubler=r1/r2。 doublet=((l1.x1-l2.x1)+r*(l1.x2-l1.x1))/(l2.x2-l2.x1)。 if(r>0&&r<1&&t>0&&t<1) cout<<""< elseif(r>0&&r<1&&t>=1) cout<<""< elseif((r>=1||r<=0)&&t>0&&t<1) cout<<""< else cout<<""< } }。 voidmain() { LineA(2,2,18,18),B(1,12,19,5)。 twoline(A,B)。 } 9、本题涉及两个类student和cdegree,前者为学生类,包含学生的学号(no),姓名(name)和成绩(degree),而成绩degree是类cdegree的对象。 cdegree类有3个数据成员,分别为数学(math),英语(english)和物理(phy)分数。 程序内容如下所示。 #include classstudent { intno。 charname[10]。 classcdegree { public: intmath。 intenglish。 intphy。 }degree。 public: voidgetdata() { cout<<"学号: "。 cin>>no。 cout<<"姓名: "。 cin>>name。 cout<<"数学分数: "。 cin>>degree.math。 cout<<"英语分数: "。 cin>>degree.english。 cout<<"物理分数: "。 cin>>degree.phy。 } voiddisp() { cout<<"学号: "< cout<<"姓名: "< cout<<"数学分数: "< cout<<"英语分数: "< cout<<"物理分数: "< } }。 voidmain() { studentstud。 stud.getdata()。 stud.disp()。 } 10、程序内容如下所示。 #include classStudent { intenglish,computer,total。 public: voidgetscore()。 voiddisplay()。 voidsort(Student*)。 ~Student()。 }。 voidStudent: : getscore() { cout<<"输入英语成绩"。 cin>>english。 cout<<"输入计算机成绩"。 cin>>computer。 total=english+computer。 } voidStudent: : sort(Student*p) { inttmp,i,j。 for(j=0。 j<2。 j++) for(i=0。 i<2。 i++) if(total { tmp=total。 total=p->total。 p->total=tmp。 tmp=english。 english=p->english。 p->english=tmp。 tmp=computer。 computer=p->computer。 p->computer=tmp。 } } voidStudent: : display() { cout<<"英语="< } voidmain() { Student*A[3]。 for(intj=0。 j<3。 j++) { A[j]=newStudent。 cout<<"学生"< A[j]->getscore()。 } inti。 for(j=0。 j<2。 j++) for(i=0。 i<2。 i++) A[i]->sort(A[i+1])。 cout< "< for(i=0。 i<3。 i++) A[i]->display()。 } 11、程序内容如下所示。 #include structlist//定义栈 { intdata。 list*next。 }。 classStack//定义一个栈操作类 { list*ptr。 public: Stack(){ptr=NULL。 } voidpush(inti)。 intpop()。 }。 voidStack: : push(intx)//入栈成员函数 { list*newnode=newlist。 newnode->data=x。 newnode->next=ptr。 ptr=newnode。 } intStack: : pop()//出栈成员函数 { list*top。 intvalue。 value=ptr->data。 top=ptr。 ptr=ptr->next。 deletetop。 returnvalue。 } voidmain() { StackA。 intarr[]={5,2,8,1,4,3,9,7,6}。 cout<<"入栈顺序: "。 for(inti=0。 i<9。 i++) { cout< A.push(arr[i])。 } cout< "。 for(i=0。 i<9。 i++) cout< cout< } 12、程序内容如下所
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向对象程序设计 山师 第六章习题答案 面向 对象 程序设计 第六 习题 答案