2023年面向对象程序设计C山师习题答案.doc





《2023年面向对象程序设计C山师习题答案.doc》由会员分享,可在线阅读,更多相关《2023年面向对象程序设计C山师习题答案.doc(37页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、第六章习题答案一、选择填空 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 calledConstructor calleda=0,b=0a=4,b
2、=82、运营该程序输出结果如下所示。a=7,b=93、运营该程序输出结果如下所示。1044、运营该程序输出结果如下所示。1035,789.5045、运营该程序输出结果如下所示。10,1,2,3,4,5,6,7,8111,12,13,14,15,16,17,18,1919,18,17,16,15,14,13,12,116、运营该程序输出结果如下所示。Starting1:Default constructor called.Default constructor called.Default constructor called.Eding1:Starting2:Constructor: a=5,
3、b=6Constructor: a=7,b=8Constructor: a=9,b=10Ending2:Destructor called.a=9,b=10Destructor called.a=7,b=8Destructor called.a=5,b=6Destructor called.a=5,b=6 Destructor called.a=3,b=4Destructor called.a=1,b=27、运营该程序输出结果如下所示。Default constructor called.Default constructor called.Default constructor called
4、.Default constructor called.Destructor called.Constructor1 called.Destructor called.Constructor2 called.Destructor called.x=0,y=0x=5,y=0x=2,y=3Destructor called.Destructor called.Destructor called.8、运营该程序输出结果如下所示。Constructor called.0Constructor called.5Destructor called.55Destructor called.59、运营该程序输
5、出结果如下所示。Constructor called.55Destructor called.510、运营该程序输出结果如下所示。Default Constructor called.Constructor:real=6.8,imag=0Constructor:real=5.6,imag=7.90+0I6.8+0I5.6+7.9IConstructor:real=1.2,imag=3.4Constructor:real=5,imag=0Default Constructor called.1.2+3.4I5+0I0+0I11、答:该程序中用string.h中所包含的函数有3种,它们是strcp
6、y( )、strcat( )和strlen( ).该程序中使用了函数重载。它们是下述两个构造函数重载:String( )和String(const char * str)。类中成员函数Setc( )的功能是用来设立字符数组中某位置的一个指定字符。类中成员函数Getc( )的功能是用来从某个字符数组中获取指定位置的字符。类中成员函数Append( )的功能是在一个字符数组中追加一个指定的字符串,即将指定的字符串添加到已知串的后边。不行。该程序中有两处使用了new运算符。运营该程序输出结果如下所示。empty.a string.9a string.ithis a string.四、改正以下程序中的
7、错误。1、该程序中point类的构造函数定义不对的,在main()中队数据成员的访问不对的,修改如下:#include class pointint x1,x2;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 Testp
8、ublic:Test()Test(int i,int j=20)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;2、程序内容如下所示。#
9、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;Product() if(name) delete name; name=0;void buy(int money) int n,r; n=money/price; if(nquantity) cout数量不够endl; else quantity-=n; r=mone
10、y%price; cout产品:name单价:price元 顾客; coutmoney元,买了n台,剩余r元endl;void get() const cout产品:name单价:price元 剩余quantity台endl;void main() Product p1(电视机,2023,15); p1.buy(7000); p1.get(); p1.buy(4500); p1.get();3、程序内容如下所示。#include #include class CDate private: int m_nDay; int m_nMonth; int m_nYear; public: CDate(
11、); CDate(int day,int month,int year); void Display(); void AddDay(); void SetDate(int day,int month,int year); CDate(); 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; ch
12、ar year5; _itoa(m_nDay,day,10); _itoa(m_nMonth,month,10); _itoa(m_nYear,year,10); coutday/month/year31) if(m_nMonth=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()
13、 bool bLeap; if (m_nYear%4!=0)bLeap=false; else if(m_nYear%100!=0)bLeap=true;else if(m_nYear%400!=0) bLeap=false; else bLeap=true; return bLeap;void main() CDate d; d.SetDate(2023,2,28); cout:; d.Display(); d.AddDay(); cout:; d.Display();4、程序内容如下所示。#include class Tc private:double unlead,lead,total;
14、int unprice,price; public:Tc()unprice=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 CFactorialint value;int fact; public:CFactorial(int val);void CalculateFactorial();vo
15、id 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();6、程序内容如下所示。#include #include class rectangle private:float
16、 ledge,sedge; public:rectangle();rectangle(float a,float b) ledge=a;sedge=b;float area() return ledge*sedge;void addarea(rectangle r1,rectangle r2) cout总面积:r1.ledge*r1.sedge+ 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 rectan
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2023 面向 对象 程序设计 习题 答案

限制150内