2022年面向对象程序设计C山师第六章习题答案 .pdf
《2022年面向对象程序设计C山师第六章习题答案 .pdf》由会员分享,可在线阅读,更多相关《2022年面向对象程序设计C山师第六章习题答案 .pdf(22页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、1 / 22 第六章习题答案一、选择填空 1、A 2、C 3、D 4、 B 5、D 6 、A 7、C 8、A 9、D 10、A 11 、C 12、A 13、B 14、 C 15、C 16、D 17、 B 18、C 19、A 20、D 21、C 22、B 二、判断下列描述的正确性,对者划,错者划。 1、 2、 3、 4、 5、 6、 7、 8、 9、 10、 11、 12、 13、 14、 15、 16、 17、 18、 19、 20、 21、 22、三、分析下列程序的输出结果。1、运行该程序输出结果如下所示。Default constructor called Constructor call
2、ed 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: Default constructor called. Default constructor called. Default constructor called.E
3、ding1: Starting2: Constructor: a=5,b=6 Constructor: a=7,b=8 Constructor: a=9,b=10 Ending2: Destructor called.a=9,b=10 Destructor called.a=7,b=8 Destructor called.a=5,b=6 Destructor called.a=5,b=6 Destructor called.a=3,b=4 精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 1 页,共 22 页2 / 22 Destructor cal
4、led.a=1,b=2 7、运行该程序输出结果如下所示。Default constructor called. Defaultconstructor called. Default constructor called. Default constructor called. Destructor called. Constructor1 called. Destructor called. Constructor2 called. Destructor called. x=0,y=0 x=5,y=0 x=2,y=3 Destructor called. Destructor called.
5、Destructor called. 8、运行该程序输出结果如下所示。Constructor called.0 Constructor called.5 Destructor called.5 5 Destructor called.5 9、运行该程序输出结果如下所示。Constructor called.5 5 Destructor called.5 10、运行该程序输出结果如下所示。Default Constructor called. Constructor:real=6.8,imag=0 Constructor:real=5.6,imag=7.9 0+0I 6.8+0I 5.6+7.9
6、I Constructor:real=1.2,imag=3.4 Constructor:real=5,imag=0 Default Constructor called. 1.2+3.4I 5+0I 0+0I 11、答:该程序中用string.h 中所包含的函数有3 种,它们是strcpy( )、strcat( )和 strlen( ). 该程序中使用了函数重载。它们是下述两个构造函数重载:String( )和 String(const char * str)。精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 2 页,共 22 页3 / 22 类中成
7、员函数Setc( )的功能是用来设置字符数组中某位置的一个指定字符。类中成员函数Getc( )的功能是用来从某个字符数组中获取指定位置的字符。类中成员函数Append( )的功能是在一个字符数组中追加一个指定的字符串,即将指定的字符串添加到已知串的后边。不行。该程序中有两处使用了new 运算符。运行该程序输出结果如下所示。empty. a string. 9 a string. i this a string. 四、改正以下程序中的错误。1、该程序中point 类的构造函数定义不正确,在main()中队数据成员的访问不正确,修改如下:#include class point int x1,x2
8、 。public: point(int x,int y)point:x=x。point:y=y 。 void disp() coutx1endl 。 coutx2endl 。 /。void main() point data(5,5) 。data.disp()。 2、在 main()函数中的p.x+=5 和 p.y+=6 两个语句是错误的,因为保护数据仅能被类的成员函数访问。五、按下列要求编写程序。1、程序内容如下所示。#include class Test public: Test() Test(int i,int j=20) 精选学习资料 - - - - - - - - - 名师归纳总结
9、- - - - - - -第 3 页,共 22 页4 / 22 t1=i。t2=j。t+=j-i 。 static int fun(Test&T) 。friend int add(Test&T) 。private: int t1,t2。static int t。int Test:fun(Test&T) t+=T.t。return t。 int add(Test&T) int n=T.t1+T.t2 。return n。 int Test:t=5 。void main() Test a1,a2(10),a3(15,25)。coutadd(a2)endl 。coutTest:fun(a2)endl
10、 。 2、程序内容如下所示。#include #include class Product char *name。 int price。 int quantity 。public: Product(char *n,int p,int q) name =new charstrlen(n)+1 。 strcpy(name,n)。price=p。quantity=q 。 精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 4 页,共 22 页5 / 22 Product() if(name) delete name。 name=0。 void buy(int
11、 money) int n,r 。 n=money/price。 if(nquantity) cout 数量不够 endl 。 else quantity-=n 。 r=money%price 。 cout 产品: name 单价: price 元 顾客 。 coutmoney 元,买了 n 台,剩余 r 元endl。 void get() const cout 产品: name 单价: price 元 剩余 quantity 台endl 。 。void main() Product p1(电视机 ,2000,15)。 p1.buy(7000)。 p1.get()。 p1.buy(4500)。
12、 p1.get()。 3、程序内容如下所示。#include #include class CDate private: int m_nDay 。 int m_nMonth 。 int m_nYear。精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 5 页,共 22 页6 / 22 public: CDate()。 CDate(int day,int month,int year) 。 void Display() 。 void AddDay() 。 void SetDate(int day,int month,int year) 。 CDate(
13、)。 private: bool IsLeapYear()。 /判断该年是否为闰年。CDate:CDate() CDate:CDate(int day,int month,int year) m_nDay=day。 m_nMonth=month 。 m_nYear=year。 void CDate:Display() char day5。 char month5 。 char year5。 _itoa(m_nDay,day,10)。 _itoa(m_nMonth,month,10) 。 _itoa(m_nYear,year,10)。 coutday/month/year31) if(m_nMo
14、nth=12) m_nYear+。 m_nMonth=1 。 m_nDay=1。 else m_nMonth+ 。 m_nDay=1。 void CDate:SetDate(int day,int month,int year) m_nDay=day。 m_nMonth=month 。 m_nYear=year。 CDate:CDate() bool CDate:IsLeapYear() bool bLeap。 if (m_nYear%4!=0) bLeap=false。 else if(m_nYear%100!=0) bLeap=true。else if(m_nYear%400!=0) bL
15、eap=false。 else bLeap=true。 return bLeap。 void main() 精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 7 页,共 22 页8 / 22 CDate d。 d.SetDate(2001,2,28)。 cout:。 d.Display() 。 d.AddDay() 。 cout:。 d.Display() 。 4、程序内容如下所示。#include class Tc private: double unlead,lead,total。int unprice,price 。 public: Tc()u
16、nprice=17 。price=16。 void getdata() coutunlead。 coutlead。 total=unprice*unlead+price*lead 。 void disp() cout 总收入: totalendl 。 。void main() Tc A。 A.getdata()。 A.disp()。 5、程序内容如下所示。#include class CFactorial int value 。int fact。 public: 精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 8 页,共 22 页9 / 22 CF
17、actorial(int val) 。void CalculateFactorial() 。void Display() 。CFactorial:CFactorial(int val) value=val。 fact=1。 void CFactorial:CalculateFactorial() int i=value 。 while(i1) fact*=i- 。 void CFactorial:Display() coutvalue!=factendl。 void main() CFactorial A(5) 。 A.CalculateFactorial() 。 A.Display() 。
18、6、程序内容如下所示。#include #include class rectangle private: float ledge,sedge。 public: rectangle() 。rectangle(float a,float b) ledge=a。sedge=b。float area() return ledge*sedge。void addarea(rectangle r1,rectangle r2) 精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 9 页,共 22 页10 / 22 cout 总面积: r1.ledge*r1.sedg
19、e+ r2.ledge*r2.sedgeendl 。 。void main() rectangle A(3.5,2.5),B(4.2,3.8),C 。 C.addarea(A,B)。 7、程序内容如下所示。#include #include class rectangle private: float ledge,sedge。 public: rectangle() 。rectangle(float a,float b) ledge=a。sedge=b。float area() return ledge*sedge。void showlength() cout 周长: (ledge+sedge
20、)*2endl 。 rectangle tlength(rectangle r2) rectangle temp。 temp.ledge=ledge+r2.ledge 。 temp.sedge=sedge+r2.sedge 。 return temp。 。void main() rectangle A(3.5,2.5),B(4.2,3.8) 。 coutA 。 A.showlength() 。精选学习资料 - - - - - - - - - 名师归纳总结 - - - - - - -第 10 页,共 22 页11 / 22 coutB 。 B.showlength() 。 rectangle C
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年面向对象程序设计C山师第六章习题答案 2022 面向 对象 程序设计 第六 习题 答案
限制150内