ImageVerifierCode 换一换
格式:DOCX , 页数:32 ,大小:21.27KB ,
资源ID:6156982      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/6156982.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(李爱华程磊 面向对象程序设计课后答案.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

李爱华程磊 面向对象程序设计课后答案.docx

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)x y z500 1000 0500 1500 1500500 200 15002-6#i

2、nclude 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不做要求#include /#include using namespace st

3、d;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 ; try checkagescore(name,age); catch( string) coutexception :name is exitendl; continue; catch(int) coutexception :age is not properendl; continue; coutname:

4、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;改为void

5、 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()return len

6、1*len2*len3;3-10class Test int 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 Ctest

7、private: 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 ()const int 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、8(1)题中印刷错误,将class C构造函数改为:C()coutconstructor C:;运行结果为:constructor Aconstructor Bconstructor C(2)40(3)3434-9略4-10 #include #include class Student int number; char name20;public: Student(int i=0,char *s=0) /构造学生对象 number=i; strcpy(name,s); void Print() /输出结果 coutNumber:numberendl; coutName:namest2.num

9、ber; /返回成员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 = i; cout最大成绩:endl; stmax.Print (); cout最小成绩:endl; stmin.Pr

10、int (); return 0;4-11#include #include using namespace std;class Book char *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)+1; strcpy(author,b); sale = c; void print() cout

11、autor 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 BC protected: int x; public: BC(int i=0)x = i;cla

12、ss DC:private BC public: 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 Shape int x,y;public: Shape(int ix,int iy)x = ix; y = iy; virtual void show()coutpos: x yendl;class Circle :public

13、Shape int radius;public: Circle(int ix,int iy,int r):Shape(ix,iy),radius(r) void show() Shape:show (); coutcircle: radiusendl;class Rect :public Shape int 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: width higt

14、hendl;int main() Shape s1(1,1); Rect r1(2,2,8,8); Circle c1(3,3,9); r1.show (); c1.show(); return 0; 第六章6-4 d=3 D:fun();6-5 C:print(),cinfo=2 C:print(),cinfo=2 D:print(),dinfo=4B类不能定义对象,否则编译通不过,因为B未定义基类A中的虚函数print(),它也是个虚基类。6-6#include using namespace std;class Mammalpublic: virtual void Speak() cou

15、tin Mammalendl;class Dog:public Mammalpublic: void Speak() coutdog barkSpeak (); return 0;运行结果: dog bark6-7#include using namespace std;class BaseClasspublic: virtual BaseClass() coutdestruct Baseendl;class Derived:public BaseClasspublic: Derived() coutdestruct derivedendl; ;int main() BaseClass *pb

16、ase; pbase = new Derived; delete pbase; 结果将不能正常执行子类析构6-8 以圆形和正方形为例#include using namespace std;class Shapepublic: virtual float area() = 0;class Circle :public Shape int radius;public: Circle(int r):radius(r) float area() return 3.14*radius*radius;class Square :public Shape int width;public: Square(

17、int w):width(w) float area() return width*width;int main() float areaSum= 0; Shape * pS3; pS1 = new Circle(1); pS2 = new Square(2); areaSum += pS1-area (); areaSum += pS2-area (); coutareaSumendl; return 0; 6-9#include using namespace std;class Studentpublic: virtual void show() = 0;class Junior :pu

18、blic Studentpublic: void show()coutthis would be info for junior studentsendl;class Senior :public Studentpublic: void show()coutthis would be info for Senior studentsshow ();pstu = new Senior;pstu-show ();return 0;第七章7-1#include #include using namespace std;class vector int x,y;public: vector(int i

19、x=0,int iy=0)x =ix;y = iy; vector operator+(vector& v1) return vector(x+v1.x,y+v1.y); vector& operator+=(vector& v1) x +=v1.x; y +=v1.y; return *this; void show() cout(x,y)endl; ;int main() vector v1(1,2),v2(3,4),v3; v3 = v1+v2; v1+=v2; v3.show(); v1.show(); return 0;7-2 #include class Complex priva

20、te: double real,image;public: Complex(double x=0.0,double y=0.0) real =x; image =y; bool operator !=(const Complex &c);Complex operator -(const Complex &c); bool operator =(const Complex &c); Complex operator -(); Complex &operator +=(const Complex &c); void Print();void Complex:Print() coutreal + i

21、mageiendl; Complex Complex :operator -(const Complex &c) Complex temp(real-c.real,image-c.image);return temp;bool Complex :operator =(const Complex &c) return (real=c.real & image=c.image); bool Complex:operator !=(const Complex &c) return (real!=c.real | image!=c.image); Complex Complex :operator -

22、() return Complex(-real,-image); Complex &Complex :operator +=(const Complex &c) real+=c.real; image+=c.image;return *this; int main() Complex c1(2,7),c2(4,2),c3; c3=c1-c2; c3.Print(); if(c3=c1)coutc3 equals to c1endl; else coutc3 doesn?t equale to c1endl; c3=-c2; c3.Print(); c3+=c2; c3.Print(); if(

23、c3 != c1) coutc3!=c1endl; return 0;7-3#include using namespace std;bool rn(int y) bool flag = 0; if(y%400=0 | y%4 =0&y%100!=0) flag = 1; return flag; class Date private: int month, day, year; public: Date(int m, int d, int y); Date& operator+(int days); void showDate(); ;Date:Date(int y, int m, int d) if (m0 & m0 & d0 & y0;) int diff ; switch(month) case 1: case 3: case 5: case 7: case 8: case 10: case 12: diff = 31 -day;break; case 4: case 6: case 9: case 11: diff = 30 -day;break; case 2:if (rn(year)diff = 29 - day; else diff = 28 -day; break; if(idiff) i-= diff+1; day = 1; month+;

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1