李爱华、程磊_面向对象程序设计课后答案(完整版)_第1页
李爱华、程磊_面向对象程序设计课后答案(完整版)_第2页
李爱华、程磊_面向对象程序设计课后答案(完整版)_第3页
李爱华、程磊_面向对象程序设计课后答案(完整版)_第4页
李爱华、程磊_面向对象程序设计课后答案(完整版)_第5页
已阅读5页,还剩36页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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 10000500200 15002-6#include using namespa

2、ce 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 std;void checkagescore(string name,int

3、 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;continue;coutname:name age :ageendl;return 0;第三章3-1(1 )A (2)C (3)B (4

4、)C (5) C(6)B(7)B (8)C (9)C3-7(1 ) main() 函数中p1.age = 30; 语句是错误的。age 是类的私有成员 2) 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 Test:Print()coutx-y=x-yendl;main 函数中Init

5、(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 boxint len1,len2,len3;public:box(int l1,int l2,int l3)len1 = l1;len2 = l2; len3 = l3; long volumn()return len1*len2*len3;3-10class Testint m1,m2;public

6、: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 Ctestprivate:int x; const int y1;p

7、ublic: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 C 构造函数改为:C()coutcons

8、tructor C:;运行结果为:constructor Aconstructor Bconstructor C(2)40(3)3(4)34-9#include#includeclass Dateint 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);friend int h(Date &d1,Date &d2)

9、;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;闰年,取1else j=0;/ 非闰年,取0if(k)/K 非 0 时s=d.day;for(i=1;id.month;i+)/d.month 为输入的月份s+=day_tabji-1;else/K 为 0 时s=day_tabjd.mon

10、th-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);for(y=d1.year+1;yd2.year;y+)if(l(y)/ 闰年days+=

11、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;elsedays=-1;return days;void main()int year1,year2,month1,month2,day1,day2;cout 输入日期1year1month1day1;cout 输入日期2year2month2day2;Date d1( year1, month1, day1),d2( year2, month

12、2, day2);int ds=h(d1,d2);cout 输出结果:=0)d1.disp(); printf( 与 );d2.disp(); printf( 之间有 %d 天 nn,ds);else/第一个日期小于第二个日期 cout 时间错误!endl;4-10#include#includeclass Studentint number;char name20;public:Student(int i=0,char *s=0)/构造学生对象 number=i;strcpy(name,s);void Print()/输出结果 coutNumber:numberendl;coutName:n

13、amest2.number;/返回成员number 的比较结果int main()Studentst5=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.

14、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)+1; strcpy(author,b);sale = c;void print()coutautor author

15、endl;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 intx; public:DC()x =100;(2)编译无错,但逻辑错误,可改为 :class BCprotected:int x;public:BC(int i=0)x = i;class DC:private BCpublic:DC(int

16、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 Shapeint radius; public:Circle(int ix,i

17、nt 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 higthendl;int main() Shape s1(1,1);Rect r1(2,2

18、,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 vehicle / 定义小车类int passenger load; / 载人数public:car(in

19、t date,float price,int passengers=4);int get_passengers();void show();class truck:public vehicle / 定义卡车类float payload; / 载重量public:truck(int date,float price,float max_load=24000.00);float efficiency();void show();vehicle:vehicle(int date,float price)vehicle:date=date;vehicle:price=price;int vehicle

20、:get_date()return date;float vehicle:get_price()return price;void vehicle:show()cout 年份 : date 年 endl;cout 价格 : price 元 endl;car:car(int date, float price,int passengers) :vehicle (date, price)passenger_load=passengers;int car:get_passengers ()return passenger_load;void car:show()cout 车型:小车 endl;veh

21、icle:show();cout 载人 : passenger_load 人 endl; cout endl;truck: truck(int date, float price,float max_load):vehicle(date,price)payload=max_load;float truck:efficiency()return payload;void truck:show()cout 车型 :卡车 endl;vehicle: show ();cout 载重 : efficiency() endl;cout endl;void main ()car car1(2001,2000

22、,5);truck tru1(2002,8000,340000);cout 输出结果 endl;car1. show ();tru1. show ();第六章6-4d=3D:fun();6-5C:print(),cinfo=2C:print(),cinfo=2D:print(),dinfo=4B 类不能定义对象,否则编译通不过,因为B 未定义基类A 中的虚函数print(), 它也是个虚基类。6-6#include using namespace std;class Mammal public:virtual void Speak()coutin Mammalendl;class Dog:pu

23、blic Mammal public: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 *pbase;pbase = new Derived;delete pbase

24、;结果将不能正常执行子类析构6-8 #include using namespace std;class Shapepublic:virtual double Area() = 0;class Circle :public Shapedouble radius;public:Circle(double r):radius(r)double Area() return 3.14*radius*radius;class Square :public Shapedouble radius;public:Square(double r):radius(r)double Area() return 6*

25、radius*radius;class Rectangle :public Shapedouble width,radius;public:Rectangle(double w,double r):radius(r)width=w; double Area() return width*radius;class Trapezoid :public Shapedouble height,radius,length;public:Trapezoid(double h,double r,double l):radius(r)height=h;length=l; double Area() retur

26、n height*(radius+length)/2;class Triangle :public Shapedouble height,radius;public:Triangle(double h,double r):radius(r)height=h;double Area() return height*radius/2;int main()double AreaSum= 0;Shape * pS6;pS1 = new Circle(1);pS2 = new Square(2);pS3 = new Rectangle(1,2);pS4 = new Trapezoid(4,2,5);pS

27、5 = new Triangle(4,2);AreaSum += pS1-Area();AreaSum += pS2-Area();AreaSum += pS3-Area();AreaSum += pS4-Area();AreaSum += pS5-Area();cout 总面积是:AreaSumendl;cout 各三维图形面积如下:endl;cout 圆形 :Area()endl;cout 正方形:Area()endl;cout 长方形:Area()endl;cout 梯形 :Area()endl;cout 三角形:Area()endl;return 0;6-9#include using

28、 namespace std;class Studentpublic:virtual void show() = 0;class Junior :public 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#incl

29、ude #include using namespace std;class vectorint x,y;public:vector(int ix=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.sh

30、ow();v1.show();return 0;7-2#include class Complexprivate: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)

31、;void Print();void Complex:Print()coutreal + imageiendl;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 | ima

32、ge!=c.image);Complex Complex :operator -()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.Pr

33、int();c3+=c2;c3.Print();if(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

34、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+;if(month12) yea

35、r+;month = 1; elseday+= i; break;return *this;void Date:showDate() coutyear.month.dayendl;int main()Date obj(1996,1,1); obj.showDate (); obj = obj+59; obj.showDate(); return 0;7-4 以 +, =为例#include#include using namespace std; class String char *sbuf; int length;public: String() length=0;sbuf=new cha

36、r;sbuf0=0;String(char *s)/用字符串初始化length=strlen(s);sbuf=new charlength+1;strcpy(sbuf,s);String (String& str)length=str.length ;sbuf=new charlength+1;strcpy(sbuf,str.sbuf );String()delete sbuf;String & operator =(String& str)if(sbuf = str.sbuf )return *this;elsesbuf = new charstr.length +1;strcpy(sbuf

37、,str.sbuf ); return *this;String operator +(String& str)/ 此函数需配合拷贝构造函数String st;st.length = length + str.length ;st.sbuf = new charst.length+1 ;st.sbuf 0 = 0;strcpy(st.sbuf,sbuf);strcat(st.sbuf,str.sbuf );return st;char & operator(int i)if(i=length) / 对下标进行检查,超出范围则报错退出程序cout 下标越界错误!endl;exit(0);retu

38、rn sbufi; void Show()coutsbufendl;);int main()(String s1(hello);String s2(world),s3 ;s3 = s1+s2; /s3.Show ();return 0;)7-6#include/using namespace std;class polynomial(int n,i;float *a;public :polynomial( int x)(n=x;a= new float n;polynomial ( const polynomial & p) /增加一个拷贝复制构造函数(n=p.n;a= new float p

39、.n;for ( int i= 0; ip.n; i+)( ai=p.ai;)polynomial()() polynomial()( deletea;)void seta( int y, float x)(ay=x;) void print()(cout y= an- 1 MxA(M n- 1= 0;i-)cout+ ai,八(i);cout=x.n)polynomial t(n);copy(t,*this );for (i= 0;ix.n;i+)/ix.nt.ai=t.ai+x.ai;return t; else polynomial t(x.n); copy(t,x);for (i= 0

40、;i=x.n)polynomial t(n);copy(t,*this );for (i= 0;i=x.n;i+) t.ai=t.ai-x.ai;return t; elsepolynomial t(x.n);copy(t,x);for (i= 0;i=n;i+)t.ai=t.ai-ai;return t;friend void copy(polynomial &x,polynomial &y);;void copy(polynomial &x,polynomial &y)(int i;if (x.ny.n)(cout 无法复制endl;x.print();y.print();system(p

41、ause);for (i= 0;i=y.n-1;i+)x.ai=y.ai;void main()polynomial j( 3),k( 4),h( 4);j.seta( 0, 1);j.seta(1,2);j.seta(2, 3);k.seta(0, 5);k.seta(1,6);k.seta(2, 7);k.seta(3, 7);j.print();k.print();polynomial m=k+j;/在这里再定义一个mcopy(h,k+j);h.print();/system(pause);第八章8-11 ) template T fun(T a)(2) template T test(

42、T a)(3) template class Arraypublic:void fun();template void Array:fun() (4)Array a1,a2,a3;8-4#include using namespace std;template T max(T a,T b,T c) T temp;temp=ab?a:b;temp=tempc?temp:c; return temp;int main()coutmax(11,29,22)endl;coutmax(3.14f,28.3f,6.7f)endl;coutmax(c,b,a)endl;return 0;8-5 及 8-6#include#include using namespace std;template void sort_bubble(T arr,int n) for(int i=0; in-1;i+)for(int j=0;jn-1-i;j+)if( arrjar

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论