李爱华、程磊_面向对象程序设计课后答案(完整版).doc
《李爱华、程磊_面向对象程序设计课后答案(完整版).doc》由会员分享,可在线阅读,更多相关《李爱华、程磊_面向对象程序设计课后答案(完整版).doc(27页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、【精品文档】如有侵权,请联系网站删除,仅供学习与交流李爱华、程磊_面向对象程序设计课后答案(完整版).精品文档.第二章2-4#include using namespace std;Add(int a,int b);int main()int x,y,sum;coutxy;sum = add(x,y);cout x+y=sumendl;Add(int a,int b)return a+b;2-5(1)this is a C+ program.(2)x=50.6 y=10 z=Ax=216.34 y=10 z=Ax=216.34 y=2 z=Ax=216.34 y=2 z=E(3)xyz5001
2、00005001500150050020015002-6#include using namespace std; int main() int *p,*init; int countp=0; int countn=0; p = new int20; init = p; for(int i=0;i*p; p+; p = p-20; for( i=0;i0) countp+; if(*p0) countn+; cout*p ; p+; cout正数有:countpendl; cout负数有:countnendl; p = init;delete p;return 0;2-7不做要求#includ
3、e /#include using namespace std;void checkagescore(string name,int age)if (name = exit) throw name;if(age50)throw age;int main()string name;int age;for(int i=0 ;iage ;trycheckagescore(name,age);catch( string)coutexception :name is exitendl;continue;catch(int)coutexception :age is not properendl;cont
4、inue;coutname:name age :ageendl;return 0;第三章3-1(1)A(2)C(3)B(4)C(5)C(6)B (7)B(8)C(9)C3-7(1)main()函数中 p1.age = 30;语句是错误的。age 是类的私有成员(2)构造函数应当给常数据成员和引用成员初始化,将构造函数改为:A(int a1,int b1):a(a1),b(b1)或A(int a1 ):a(a1),b(a)再将main中的A a(1,2); 改为A a(1);(3)(1)在Test 类中添加语句:void print();void Print() coutx-y=x-yendl;
5、改为void Test:Print() coutx-y=x-yendl;main函数中Init(38,15);改为:A.Init(38,15);Print();改为:A.Print(); 3-8(1)Constructing AConstructing BDestructing BDestructing A(2)double a,double bpoint & pp.x3-9class box int len1,len2,len3;public: box(int l1,int l2,int l3)len1 = l1;len2 = l2; len3 = l3; long volumn()retu
6、rn len1*len2*len3;3-10class Testint m1,m2; public:void Init(int a,int b)m1 = a;m2 = b;void Pring()coutm1 m2endl;3-11略3-12第四章4-6(1)D(2)D(3)D(4)D(5)B(6)D4-7(1)static int count = 0;这样初始化静态成员值是不对的将其改为static int count;在类外,main函数前加int Sample:count = 0;(2)#include /#include using namespace std;class Ctestp
7、rivate:int x; const int y1;public:const int y2;Ctest(int i1,int i2):y1(i1),y2(i2)y1 =10;/y1 为常量不能赋值x = y1;int readme() const;int Ctest:readme ()constint i;i = x;x+; /常函数内不能改变成员值return x;int main()Ctest c(2,8);int i = c.y2;c.y2 = i;/y2为常量,不能改值i = c.y1;/y1私有,类外不能访问return 0;将出错语句全部注释4-8(1)题中印刷错误,将class
8、 C构造函数改为:C()coutconstructor C:;运行结果为:constructor Aconstructor Bconstructor C(2)40(3)3434-9#include#includeclass Date int year; int month; int day; public: Date(int y,int m,int d) year=y;month=m;day=d; void disp() coutyear month dayendl; friend int count_day(Date &d,int k); friend int l(int year); fr
9、iend int h(Date &d1,Date &d2);int count_day(Date &d,int k) static int day_tab212=31,28,31,30,31,30,31,31,30,31,30,31, 31,29,31,30,31,30,31,31,30,31,30,31; / 使用二维数组存放各月天数,第一行对应非闰年,第二行对应闰年 int j,i,s; if(l(d.year) j=1;/闰年,取1 else j=0;/非闰年,取0 if(k)/K非0时 s=d.day; for(i=1;id.month;i+)/d.month为输入的月份 s+=day
10、_tabji-1; else/K为0时 s=day_tabjd.month-d.day; for(i=d.month+1; i=12; i+) s+=day_tabji-1; return s;/S为相差的天数int l(int year) if(year%4=0&year%100!=0|year%400=0) / 是闰年 return 1; else / 不是闰年 return 0;int h(Date &d1,Date &d2) int days,day1,day2,y; if(d1.yeard2.year)/第一个日期年份小于第二个日期年份 days=count_day(d1,0); f
11、or(y=d1.year+1;yd2.year;y+) if(l(y)/闰年 days+=366L; else/非闰年 days+=365L; days+=count_day(d2,1); else if(d1.year=d2.year) day1=count_day(d1,1); day2=count_day(d2,1); days=day2-day1; else days=-1; return days;void main() int year1,year2,month1,month2,day1,day2; cout输入日期1year1month1day1; cout输入日期2year2m
12、onth2day2; Date d1( year1, month1, day1),d2( year2, month2, day2); int ds=h(d1,d2); cout输出结果:=0) d1.disp(); printf(与); d2.disp(); printf(之间有%d天nn,ds); else/第一个日期小于第二个日期 cout时间错误!endl;4-10 #include #include class Studentint number;char name20;public:Student(int i=0,char *s=0)/构造学生对象 number=i; strcpy(
13、name,s);void Print()/输出结果 coutNumber:numberendl;coutName:namest2.number;/返回成员number的比较结果int main()Student st5=Student(65,Li),Student(78,Zhang),Student(80,wang),Student(92,zhao),Student(50,zhen);int max = 0;int min = 0;for(int i=1;i5;i+)if(!greaterthan(stmax,sti)max = i;if(!greaterthan(sti,stmin)min
14、= i;cout最大成绩:endl; stmax.Print ();cout最小成绩:endl;stmin.Print ();return 0;4-11#include #include using namespace std;class Bookchar *name;char*author;int sale;public:Book() name = 0; author = 0; sale = -1;Book(char* a ,char* b,int c) name = new charstrlen(a)+1;strcpy(name,a);author = new charstrlen(b)+
15、1;strcpy(author,b);sale = c;void print()coutautor authorendl;coutname nameendl;coutprice saleendl;Book() if(!name ) delete name;if(!author)delete author;int main() Book b1(c+,li ai hua,12); Book b2;return 0;第五章5-8 改错题答案不唯一(1) class DC int x;public:DC()x =100;(2)编译无错,但逻辑错误,可改为: class BCprotected:int
16、x;public:BC(int i=0)x = iclass DC:private BCpublic:DC(int i):BC(i)(3)将DC构造函数改为:DC(int i):BC(i)y = 0;5-9(1) base class(2)(10,5)(3,9-18,33)(13,19)(13,19-18,33)(13,19)5-10#include using namespace std;class Shapeint x,y;public:Shape(int ix,int iy)x = ix; y = iy;virtual void show()coutpos: x yendl;class
17、Circle :public Shapeint radius;public:Circle(int ix,int iy,int r):Shape(ix,iy),radius(r)void show() Shape:show ();coutcircle: radiusendl;class Rect :public Shapeint width,higth;public:Rect(int ix,int iy,int iw,int ih):Shape(ix,iy),width(iw),higth(ih)void show() Shape:show ();coutwidth and higth: wid
18、th higthendl;int main()Shape s1(1,1);Rect r1(2,2,8,8);Circle c1(3,3,9);r1.show ();c1.show();return 0;5-11#include class vehicle / 定义汽车类 protected: int date; / 年份 float price; /价格public: vehicle(int date,float price); int get_date(); float get_price(); float date_load(); void show(); class car:public
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 李爱华 程磊 面向 对象 程序设计 课后 答案 完整版
限制150内