C++程序的设计教程_面向对象分册(郑秋生)完整答案_第1页
C++程序的设计教程_面向对象分册(郑秋生)完整答案_第2页
C++程序的设计教程_面向对象分册(郑秋生)完整答案_第3页
C++程序的设计教程_面向对象分册(郑秋生)完整答案_第4页
C++程序的设计教程_面向对象分册(郑秋生)完整答案_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

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

文档简介

1、第1章类和对象一、选择题1.C 2.B3.C4.A5.C 6.A 7.C 8 c 9A 10 C 二、阅读题 1 x=2,y=32 x=2,y=3 x!=y3 Cstatic:va1=0cs1 .val=1cs2.val=2cs1 .val=4cs2.val=4四、改错题#include <string.h>#include <iostream.h> class person(public:person(int n,char* nam,char s)num=n; strcpy(name,nam); sex=s;cout«HConstructor called.

2、n«endl;person()cout«HDestructor called.H«endl;)void display()cout«nnum: H«num«endl;cout«f,name: M«name«endl;cout«Hsex: ,f«sex«endl«endl;)private:int num;char name10;char sex;);int main()(person s1 (10010/"Wang_liH,T); s1 .display(

3、);person s2(10011 ,'Zhang_fun','m');s2.display();return 0;)五、编程题5 1#include <iostream>using namespace std;class CBox(public :CBox(double l=0,double w=0,double h=0); double area(); double volume ();private :double lengh;double width; double high;CBox:CBox(double I,double w,doubl

4、e h) dengh=l; width=w; high=h;double CBox:area()(return 2*(lengh*width+lengh*high+width*high);double CBox:volume ()(return lengh*width*high;void main()CBox box1 (4,5,6);cout«box1 .area()«endl;cout«box1 .volume()«endl;5.2#include <iostream> using namespace std; class CPoint

5、public :CPoint(double a=0,double b=0)x=a;y=b;CPoint(CPoint & p) (x=p.x;y=p-y;void print() (coutvv"("vvxvv”Jvvyvv”)”;private :double x,y;);class CLinepublic:CLine(double x1 =0,double y1 =0,double x2=0,double y2=0):p1(x1 ,y1),p2(x2,y2) ( CLine(CPoint x,CPoint y):p1(x),p2(y) ( CLine(CLine

6、 &lin) (p1=lin.p1;p2=lin.p2;void DrawLine()(cout«nLine form'1; p1.print(); cout«Hton;p2.print(); cout«endl;void Linedel() cout«ndelete lineM«endl;void move(CPoint &x,CPoint &y)cout«nmove lineH«endl;p1=X;p2=y;private :CPoint p1 ,p2;);void main()CPoin

7、t pointl (1,5),point2(5,8),point3(20,30),point4(40,50); CLine linel (pointl ,point2);CLine Iine2(2,3,8,12); linel .DrawLine (); line2.DrawLine (); Iine2.move(point3,point4); line2.DrawLine (); Iine2=line1;Iine2.DrawLine (); linel .Linedel ();5.3#include <iostream> using namespace std; class CC

8、omplexpublic:CComplex(double, double);CComplex(CComplex &c); / 复数类的拷贝构造函数声明 double GetReal();double Getlmag();void Print();private:double real;double imag;);CComplex:CComplex (double r=0.0, double i=0.0) real = r;imag = i;cout«"调用两个参数的构造函数*'«endl;CComplex:CComplex (CComplex &a

9、mp;c) /复数类的拷贝构造函数定义 real = c.real;imag = c.imag;coutvv”调用拷贝构造函数H«endl; )double CComplex:GetReal()return real;double CComplex:Getlmag()return imag;void CComplex:Print() / 显示复数值cout « "(" « real «« imag «« endl;)CComplex add(CComplex &x,CComplex &y)

10、 /普通函数完成两个数的加法,对象作为 函数参数, return CComplex(x.GetReal() +y.GetReal() ,x.Getlmag ()+y.Getlmag (); )void main(void) CComplex a(3.0,4.0), b(5.6,7.9);CComplex c(a); 调用复数类的拷贝构造函数cout « "a = H;a. Print();cout « ”b = f'b. Print();cout « "c =c. Print(); cout«Hc=a+bH«endl

11、; c=add(a,b); cout « “c = "; c.Print (); )5.4include <iostream>#include <string> using namespace std;class CStudent /类声明public:CStudent(char *,float,float,float); CStudent(CStudent &s);CStudent。;void display();friend float avg(CStudent &s); private:char *name;float grad

12、3;);CStudent:CStudent(char *na,float a,float b,float c) (name=new charstrlen(na)+1; strcpy(name,na);grad0=a;grad1=b;grad2=c;)CStudent:CStudent(CStudent &s) name=new charstrlen()+1 ; strcpy();gradO=s.gradO;grad1=s.grad 1;grad2=s.grad 2;)CStudent:>CStudent() delete name;)void C

13、Student:display() int i;cout«Hname:n«name«endl;for(i=0;i<3;i+)cout<<"grad"«i«":"«gradi«endl;)float avg(CStudent &s)/普通函数,需要引用私有成员,声明为学生类的友元函数4 return (s.grad0+s.grad1 +s.grad2)/3;)int main()CStudent studlf'张三 ”,79,98,82);/ 定义对象s

14、tudl .display();cout «', 平均成绩:n«avg(stud1 )«endl;return 0;)5.5#include <iostream> using namespace std; class CString public :CString(); /缺省构造函数,初始化为空串CString(char ch,int nRepeat);/用一个字符重复n次,初始化字符串 CString(const char*psz); /用一个字符串初始化CString(CString &stringser); 11 拷贝构造函数

15、-CString();int GetLength() const;bool IsEmptyO const;char GetAt(int nindex) const;void Setat(int nindex,char ch);void Print();int compare(CString& string);int compare(const char* psz)const;void Vacate();private :char *s;);CString:CString()s=NULL;)CString:CString(CString& stringser) s=new cha

16、rstrlen(stringser.s)+1 ;if(s!=O)strcpy(s,stringser.s);)CString:CString(char ch,int nRepeat) <s=new charnRepeat+1;for(int i=O;i<nRepeat;i+) si=ch;snRepeat='O' )CString:CString(const char*psz) s=new charstrlen(psz)+1;if(s!=O)strcpy(s,psz);)int CString:GetLength() constint i=O;while (si!O

17、)i+;return i;)bool CString:lsEmpty() const if(s=NULL)return 1;elsereturn 0;)void CString:Vacate() (s0='0'cout«MNow have vacated string.If«endl;)char CString:GetAt(int nindex) const if(nindex>1 &&nindex<=GetLength()+1) return s nindex-1;elsereturn 'O'void CStri

18、ng:Setat(int nindex,char ch)if(nindex>1 &&nindex<=GetLength()+1) s nindex-1=ch;)void CString:Print()cout«s«endl;)int CString:compare(CString& string) if(strcmp(s,string.s)>0)return 1;else if(strcmp(s,string.s)<0)return -1;elsereturn 0;)int CString:compare(const char

19、* psz)constif(strcmp(s,psz)>0)return 1;else if(strcmp(s5psz)<0)return -1;else return 0;)CString:>CString()(/cout«endl«"析构:"«s«endl;if(s!=NULL)delete 口 s;void main()char a4="y";char b4;CString c1("Hellow"),c2(c1); cout«"Stringc1 is:

20、"«"c1.Print(); cout«endl;cout«"Stringc2 is:"«"M; c2.Print(); cout«endl;CString c3('b3);cout«"Stringc3 is:"«" c3.Print(); cout«endl;cout«"以下是对串的基本操作*«endl;int num=c1 .GetLength(); char ch;cout«&quo

21、t;c1 的长度是n«num«endl;ch=c1 .GetAt(5);coutvv”获得字符串c1中第"vv5vv”个字符是vvchvvendl; coutvv”下面是插入字符串c1中一个特殊 字符 kTvvendl;c1.Setat(5,'d');COUt«H插入后字符串C1变为:n«nc1.Print();llllllllllllllllllllllcout«endl;if(c1.lsEmpty()=1)cout«"String is empty/'«endl; elseco

22、ut«MString isn't empty."«endl;coutvv”下面是判断字符串c1清空"«endl; c1 .Vacate();cout«n 清空后输出字符串 c1 :“vv” n" c1.Print();coutvv”字符串已被清空"«endl;coutvv”请按任意键继续"«endl; cin»b;lllllllllllllllllllllllllllCString c5=c2;cout«nString c5=c2 is:M«n H

23、;c5.Print(); cout«endl;*cout«n以下是对串的比较H«endl;int temp=pare(c3); cout«"以下比较 c2 与 c3"«endl; if(temp>0)cout«"Stringc2>Stringc3"«endl; else if(temp<0) cout«"Stringc2<Stringc3"«endl; else cout«"Stringc9=Strin

24、gc4"«endl; cout«endl;cout«n以下比较c2与任意字符串Goodboy!n«endl;if(pare("Goodboy!n)>0) cout«"Stringc2>'Goodboy!"'«endl;else if(pare("Goodboy!")<0) cout«"Stringc2<'Goodboy!H«endl;elsecout«"Stringc2 =

25、9;Goodboy!"'«endl;第二章继承和派生一、选择题1 - D2.B3. D一、阅读程序题四、编程题1.1#include <iostream.h>#include<string.h>#define PAI 3.14class Circlepublic:Circle()r=O;Circle (double d)r=d;double area()return(PAI*r*r);void displayl ()cout«"桌子的面积:,'«area()«endl;private:doubl

26、e r;class Table public:Table()heig=O;Table (double h) heig=h;void display2()cout«n 桌子的高度:,«heig«endl; private:double heig;);class Roundtable : public Circle,public Table public:Roundtable()strcpy(color,H 白色 *');Roundtable(double a,double b,char* c):Circle(a),Table(b)strcpy(color,c

27、); void display () display1();display2();cout«n 桌子的颜色:,«color«endl; private: char color20;);void main() Roundtable s(2.0,3.0,“ 黑色”);s.display(); )1.2如果Age在基类中被定义为私有的,SetAge(int n)不能直接给Age赋值,如果Age是基类的公有成员 变量,则可以直接赋值。class Animal public:Animal();int Age;);class Dog:public Animal public:

28、Dog();Void SetAge(int n) (Age=n;1.3 #include <iostream.h>class Rectanglepublic:Rectangle (double I,double w)length=l,width=w double area()return length*width; void display1();private:double length;double width;);void Rectangle:display1 ()cout«n 长:,f«length«endl; cout«M 宽:lf

29、«width«endl; class Cuboid:public Rectangle public:Cuboid (double L,double w,double h):Rectangle(L5w)high=h,volume=L*w*high ; double vol()return area()*high;void show (); private: double high;double volume;);void Cuboid:show()displayl ();cout«"高:H«high«endl; cout«&q

30、uot;体积:"«vol()«endl;)void main()Cuboid cub (10,20,30);cub.show();)1.4#include <iostream.h>class Vehiclepublic:Vehicle():maxspeed(O),weight(O)Vehicle(double m,double w):maxspeed(m)5weight(w);void run () cout«"可以奔跑"«endl;void stop () cout«M 可以停止奔跑"vve

31、ndl; private:double maxspeed;double weight;);class Bicycle:virtual public Vehiclepublic:Bicycle (double m,double w,double h):Vehicle(m,w),height(h) private:double height;);class Motorcar: virtual public Vehicle public:Motorcar (double m,double w,double s):Vehicle(m,w)3setnum(s) private: double setnu

32、m;class Motorcycle:public Bicycle,public Motorcar(public:Motorcycle(double m,double w,double h,double s):Bicycle(m,w,h),Motorcar(m,w5s), Vehicle(m,w) );void main()Motorcycle s1 (300,10,6,30);s1.run();s1.stop();)如果不把Vehicle设置为虚基类,会产生二义性问题。第3章多态性3.1 简答题3.1.1 哪些运算符可以重载?几乎所有的运算符都可以重载,除了以下的几个运算符:3.1.2 运算

33、符重载后,优先级和结合性如何?用户重新定义运算符,不改变原运算符的的优先级和结合性。同时运算符重载后,也不改变运算符 的语法结构,即单目运算符只能重载为单目运算符,双目运算符只能重载为双目运算符。3.1.3 编译程序如何选用运算符函数?在每一种编译系统中,运算符实际上都对应一个函数,只是这种运算对用户具有透明性,使用者并不 知道函数的存在。运算符重载实际上是运算符函数的重载,所以运算符的重载实际上是函数的重载。编译程序对运算符重载的选择,遵循着函数重载的选择原则。当遇到不很明显的运算符时,编译程序 将去寻找参数相匹配的运算符函数。3.1.4 重载运算符有哪些限制?(1) 不可臆造新的运算符(2

34、) 坚持四个不能改变。不能改变运算符原有的优先级不能改变运算符原有的结合性不能改变运算符原有的语法结构不能改变运算符原有的操作数个数但允许改变运算符的返回类型(3 )C+规定,运算符中,参数类型都是内部类型时,不允许重载。“:、?:这五 个运算符不能重载。3.1.5 运算符重载必须遵循哪些原则?运算符重载可以使程序更加简洁,使表达式更加直观,增强可读性。但是,运算符重载使用不宜过多,否则会带来一定的麻烦。运算符重载必须遵循以下原则:(1)重载运算符含义必须清楚。(2)重载运算符不能有二义性。32填空题3.2.1 C+中多态性包括两种多态性:山 和&。前者是通过也实现的,而后者是通过也和

35、&来实现的。答案:(1)编译时的(2 )运行时的(3)函数和运算符的重载(4)类继承关系(5)虚函数3.2.2 纯虚函数定义时在函数参数表后加£L1,它表明程序员对函数&,其本质是将指向函数体的指针定为(31 o答案:(1) =0(2)不定义(3) NULL3.2.3在基类中将一个成员函数说明成虚函数后,在其派生类中只要£H、丝1和&L完全一样就认为是虚函数,而不必再加关键字(41。如有任何不同,则认为是而不是虚函数。除了非成员函数不能作为虚函数外,£61、 LZ1和 18)也不能作为虚函数。答案:(1)同虚函数名(2 )同参数表(3)同返

36、回类型。如基类中返回基类指针,而派生类中返回派生类指针是允许的(4) virtual(5)重载(6)静态成员函数(7 )内联函数 (8 )构造函数第三大题答案:3. 1答案:#in clude<iostream.h>#in clude<stri ng.h> class CStude nt (char n ame20;char n ativeplace20;char code30;int age;float score;public :CStude nt(char *, char*,char*,i nt,float);CStude nt(CStude nt &s)

37、;void display();float operator+(CStude nt s1);CStudent:CStudent(char *name, char*native,char*codejnt agejloat score)(strcpy(this->name,name); strcpy(this->nativeplace,native);strcpy(this->code,code);this->age=age; this->score=score;)CStudent:CStudent(CStudent &s) strcpy(this->n

38、ame,); strcpy(this->nativeplace5s.nativeplace);strcpy(this->code,s.code);this->age=s.age; this->score=s.score;)void CStudent:display() (H«age«ncout«name«n H«nativeplace«n H«code«n H«score«endl;)float CStudent:operator +(CStudent s1)

39、 return this->score+s1 .score;) void main()(CStudent w(',whlH,HzhengzhouH;,13783580182130,90); w.display();CStudent c(,'ctm";,zhengzhou"quot;,30,90); c.display();cout«w+c«endl;)32答案:#include<iostream.h>#include<string.h>#include<math.h>cl

40、ass CTriangle (float a,b,c;public:CTriangle(float a,float bjloat c) (this->a=a;this->b=b;this->c=c; float GetArea()<float t=(a+b+c)/2;return sqrt(t*(t-a)*(t-b)*(t-c); )float operator +(CTriangle t)return this->GetArea()+t.GetArea(); );void main() CTriangle tr1(3,4,5),tr2(6,8,10); cout

41、«tr1 +tr2«endl;)33答案:#include<iostream.h> class BaseClass<public: virtual void f1 () cout«"BaseClass:f1 ()"«endl; void f2() cout«"BaseClass:f2()',«endl; class DerivedClass: public BaseClass (public:void f1 () cout«"DerivedClass:f1 (

42、),'«endl; void f2() cout«"DerivedClass:f2()"«endl;void main()DerivedClass d1;BaseClass* Bptr;DerivedClass* Dptr;Bptr=&d1;Bptr->f1();Bptr->f2();Dptr=&d1;Dptr->f1();Dptr->f2();习题四、选择题1 - D2 A3 B4 - C 5. C 6 C7 A 二、简答题1什么叫做流?流的提取和插入是指什么?I/O流在C+中起着怎样的作用?答

43、:流是一种抽象,它负责在数据的生产者(程序/文件)和数据的消费者(文件/程序)之间建立联系,并 管理数据的流动。一般意义下的读操作在流数据抽象中被称为(从流中)提取,写操作被称为(向流中)插入。完成数据从动态(内存)至I静态(外存)的转换,或着是从静态(外存)到动态(内存)的转换。2什么是字节流、字符流和二进制流?答:根据对字节内容的解释方式,字节流分为字符流(也称文本流)和二进制流。字符流将字节流的每个字节按ASCH字符解释,它在数据传输时需作转换,效率较低。例如源程序文件和文本文件都是字符流。由于ASCH字符是标准的所以字符流可以直接编辑,显示或打印,字符流产生的文件通行于各类计算机。二

44、进制流将字节流的每个字节以二进制方式解释,但各类 计算机对数据的二进制存放格式各有差异,可移植性较 差。3 cerr和clog作用是什么?有何区别?答:对于输 人提示信息或输出结果而言,cerr作用是向标准错误设它在数据传输时不作任何转换,故效率高。 且无法人工阅读,故二进制流产生的文件和clog的用法相同,但作用不同。cerr 的备(standard error程序时,device )在显示籀触袋嘲第总、°cerr流中的信息只能往往不希望程序运行时的出错信息被送到其他文件,而要求在显示器上及时输出,这时应该用cerr,cerr流中的信息是用户根据需要指定的。clog流对象也是标准

45、错误流,它是console log的缩写。它的作用和cerr相同,都是在终端显示 器上显示出错信息。不同的是 cerr不经过缓冲区,直接向显示器上输出有关信息,而clog中的信息存放在缓冲区中,缓冲区满后或遇 endl时向显示器输出。4 -用什么方法来控制输入输出流中出现的错误?答:为提高程序的可靠性,应在程序中检测I/。流的操作是否正常。当检测到流操作出现错误时,可以通 过异常处理来解决问题。5比较读写文本文件与二进制文件的异同。答:从文件编码的方式来看,文件可分为ASCII码文件和二进制码文件两种。 ASCII文件也称为文本文件,这种文件在磁盘中存放时每个字符对应一个字节,用于存 放对应的

46、ASCII码。例如,数5678的存储形式为: ASM 00110101 00110110 00110111 00111000 JJJ J十进制码:5678共占用4个字节。ASCII码文件可在屏幕上按字符显示,例如源程序文件就是ASCII文件,用DOS命令TYPE可显示文件的内容。由于是按字符显示,因此能读懂文件内容。 二进制文件是按二进制的编码方式来存放文件的。例如,数5678的存储形式为:00010110 00101110只占二个字节。二进制文件虽然也可在屏幕上显示,但其内容无法读懂。C+系统在处理这些文件时,并不区分类型,都看成是字符流,按字节进行处理。输入输出字符流的开始和结束只由程序控

47、制而不受物理符号(如回车符)的控制。因此也把这种文件称作“流式文件”。 6随机读写是什么意思,常用于哪种类型的文件? 答:在C+中可以由程序移动文件指针,从而实现文件的随机访问,即可读写流中任意一段内容。一般文本 文件很难准确定位,所以随机访问多用于二进制文件。 7文件流和字符串流有什么区别? 答:文件在C+看来是字符流或二进制流,文件流是以外存文件为输入输出对象的数据,所以文件流是与设 备相关的。可以把流的概念应用到字符串(String )上。这样字符串就可以看作字符串流。字符串流不是 以外存文件为输入输出的对象,而以内存中用户定义的字符数组为输入输出的对象。字符串流是与内存相 吴,所以也称

48、内存流。可以用输入输出操作来完成字符串流的操作。三、编程题 1 x= 468y= -3.42565x=468 y=-3.42565 x=+468*y=-3.43* 2生成一个名称为data.txt文件,内容:This is test data 3 源程序。#include <iostream> #include <iomanip> using namespace std; int main() ( for(int n=8;n>0;n-) cout«setw(20-n)«"*"«setfill('*,)«setw(2*n-1)<<" ')«setw(20-n)«endl;n«setfillC return 0;)4源程序。#include<fstream.h> void main(void) (char f2256;coutvv”请输入目标文件名?cin»f2;ofstream out(f2);if(!out) cout«Hn

温馨提示

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

评论

0/150

提交评论