ly新标准C程序设计教材1120章课后题答案_第1页
ly新标准C程序设计教材1120章课后题答案_第2页
ly新标准C程序设计教材1120章课后题答案_第3页
ly新标准C程序设计教材1120章课后题答案_第4页
ly新标准C程序设计教材1120章课后题答案_第5页
已阅读5页,还剩38页未读 继续免费阅读

下载本文档

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

文档简介

1、word完美格式新标准C+程序设计教材11-20章课后题答案第11章:1 简述结构化程序设计有什么不足,面向对象的程序如何改进这些不足。答案:结构化程序设计的缺点:(1) 用户要求难以在系统分析阶段准确定义,致使系统在交付使用时产生许多问题。(2) 用系统开发每个阶段的成果来进行控制,不适应事物变化的要求。(3) 系统的开发周期长。面向对象的程序设计如何改进这些不足:面向对象程序设计技术汲取了结构忧程序设计中好的思想,并将这些思想与一些新的、强大的理念相 结台,从而蛤程序设计工作提供了一种全新的方法。通常,在面向对象的程序设计风格中,会将一个问题 分解为一些相互关联的子集,每个子集内部都包含了

2、相关的数据和函数。同时会以某种方式将这些子集分 为不同等级,而一个对象就是已定义的某个类型的变量。2. 以下说怯正确的是()。A. 每个对象内部都有成员函数的实现代码B. 一个类的私有成员函数内部不能访问本类的私有成员变量C. 类的成员函数之间可以互相调用D. 编写一个类时,至少要编写一个成员函数答案:C3以下对类A的定义正确的是()B. class Aint v; A * next;void Func() A . class Aprivate: int v;public: void Func() C . class Aint v;public : void Func();D. class A

3、 int v;public:A next void Func() 精心整理学习帮手;A: : void Func() 答案:B4 假设有以下类A:class Apublic:int func(int a) return a * a; ;以下程序段不正确的是()。A. A a; a.func(5);B. A * p =new A;p-func(5);C. A a ;A& r =a;r.func(5);D. A a,b;if(a!=b)a.func(5);答案:D5以下程序段不正确的是(A)。A int main() class A int v; A a; a.v= 3;return 0;B in

4、t main() class A public: int v; A * p; ;A a; a.p=&a; return 0;C int main() class A public: int v; ;A * p = new A;p_ v =4; delete p;return 0;D. im main() class A public: int v; A * p; ;A a: a. p = new A; delete a.p;return 0;答案:A6 实现一个学生信息处理程序。输入:姓名,年龄,学号(整数)。第一学年平均成绩第二学年平均成绩,第三学年平均成绩,第四学年平均成绩。输岀:姓名,年

5、龄,学号,4 年平均成绩。例如:输入:Tom 18 7817 80 80 90 70输出:Tom,18,7817,80要求实现一个代表学生的类,并非所有成员变量都是私有的。答案:#include #include#include#includeusing namespace std;class Student private:int age, scorel, score2, score3, score4;char name100, num100;double average;public:Student(char aname, int aage, char anum,int ascorel,

6、int ascore2, int ascore3, intascore4)strcpy(name, aname);age = aage;strcpy(num, anum);scorel = ascorel;score2 = ascore2;score3 = ascore3;score4 = ascore4;int main() 第12章1 以下说法中正确的是()。A. 个类一定会有无参构造函数B. 构造函数的返回值类型是voidC. 一个类只能定义一个析构函数,但可以定义多个构造函数D. 个类只能定义一个构造函数,但可以定义多个析构函数答案:C2.对于强过new运算符牛成的对象()。A. 程程

7、序结束时自动析构B. 执行delete操作时才能析构C. 在包含new语句的函数返回时自动析构D. 在执行delete操作时会析构,如果没有执行delete操作,则在程序结束时自动析构答案:D3如果某函数的返回值是个对象,则该函数被调用时,返回的对象()。A. 是通过复制构造函数初始化的B. 是通过无参构造函数初始化的C. 用哪个构造函数初始化取决于函数的return语句是怎么写的D. 不需要初始化答案:C4.以下说法LE确的是()。A. 在静态成员函数中可以调用同类的其他任何成员函数B. const成员函数不能作用于非const对象C. 在静态成员函数中不能使用this指针D. 静态成员变量

8、每个对象有各自的一份答案:C5以下关于this指针的说法中不正确的是()。A. const成员函数内部不可以使用 this指针B. 成员函数内的this指针,指向成员函数所作用的对象C. 在构造函数内部可以使用this指针D. 在析构函数内部可以使用this指针答案:A6. 请写出下面程序的输出结果。class CSample int x;public:CSample() cout ” C1 endl; CSample(int n) x=n; cout” C2,x=” vvnvvendl;int main()CSample array12;CSample array22=6,8;CSample

9、 array32=12;CSample *array4=new Csample3;Return 0;答案:C1C1C2,x=6C2,x=8;C2,x=12C1C1C1C17下面程序的输岀结果是。#include using namespace std;class Sample public:int v;Sample() Sample(int n) :v(n) ;Sample(const Sample & x) v = 2 + x.v; ;Sample & PrintAndDouble(Sample o)cout o.v;o.v = 2 * o.v;return o;int main()Samp

10、le a(5);Sample b = a;Sample c = PrintAndDouble(b);cout endl;cout c.v endl;/cout hex c.v endl;Sample d;d = a;cout d.v;return 0;答案:92058请写出下面程序的运行结果:4,6请填空:#includeusing namespace std;class Aint val;public:A(int n)val=n;int GetVal()return val;;class B:public Aprivate:int val;public:B(i nt n):;int GetV

11、al() return val;int main()B b1(2);coutvb1.GetVal()vv,vvb1 A:GetVal()n; return 0;答案:Val,A(6)9下面程序的输岀结果是05请填空:#includeusing namespace std;class Apublic:int val;A()val=n;GetObj()return;int main()A a;coutvva.valvvendl;a.GetObj()=5;coutvva.valvvendl;答案:int n=0A&*this10.下面程序的输出结果是:10请补充Sample类的成员函数,不能增加成员

12、变量#includeusing namespace std;class Samplepublic:int v;Sample(int n):v(n);Sample(Sample & obj) this-v = 2 * obj.v;;int main()Sample a(5);Sample b=a;coutvvb.v;return 0;答案:Sample(Sample & obj)This-v=2*obj.v;11.下面程序的输岀结构是:5, 55, 5请填空:#includeusing namespace std;class Basepublic:int k;Base(int n):k(n);c

13、lass Big public:int v;Base b;Big;Big;int main()Big a1(5);Big a2=a1;coutva1.vvv,vva1.b.kvvendl;coutvva2.vvv,vva2.b.k#includevcstringusing namespace std;class Complexprivate:double r,i;public:void Print()coutvvrvv+vvivvivve ndl;/在这里补充/在这里补充;int main()Complex a;a=3+4i;a.Print();a=5+6i;a.Print();return

14、0;答案:Complex &operator=(const char t) if(!t) r=0.0; i=0.0; else r = t0 - O;i = t2 - O; return *this;4 下面的Mylnt类只有一个成员变量。Mylnt类内部的部分代码被隐藏了。假设下面的程序能编译通过,且输岀结果是:4,1请写出被隐藏的部分(要求编写的内容必须是能全部救进Mylnt类内部的,Mylnt的成员函数不允许使用静态变量)。#includeusing namespace std;class MyIntint nVal;public:MyInt(int n)nVal=n;int Retur

15、nVal()return nVal;/在这里补充/在这里补充;int main()Mylnt objlnt(1O); objlnt-2-1-3;coutvobjlnt.ReturnVal();coutvv,;objlnt-2-1;coutvobjlnt.ReturnVal(); return 0;答案:MyInt & operator - (int i)5下面的程序输岀结果是(4,5 )(7,8)请填空:#includeusing namespace std;class Pointprivate:int x;int y;public:Point(int x_,int y_):x(x_),y(y

16、_);/;operator(,const Poi nt & p)Return: int main()coutvvPoint(4,5)vvPoint(7,8);return 0;答案:friend ostream& operator (ostream &o,const Point &p);friend ostream &ostream &o0(卩以,卩$)6*41o6 编写一个二维数组类Array2,使得程序的输出结果是0,1,2,3,4,5,6,7,8,9,10,11, next0,1,2,3,4,5,6,7,8,9,10,11,答案:#include #include using names

17、pace std; class Array2private:int row;/数组行数int column;/数组列数int* ptr;/指向二维数组的指针public:Array2()ptr = NULL;Array2(int paraRow, int paraColumn):row(paraRow),column(paraColumn)ptr = new introw * column;Array2(Array2& a):row(a.row),column(a.column)ptr = new introw * column;memcpy(ptr, a.ptr, sizeof(int)*r

18、ow*column);Array2& operator= (const Array2 &a)if (ptr) delete ptr;row = a.row;column = a.column;ptr = new introw * column;memcpy(ptr, a.ptr, sizeof(int)*row*column);return *this;Array2()if (ptr) delete ptr;int* operator (int i)return ptr + i*column;int& operator。(int i, int j)return ptri*column + j;

19、;int main()Array2 a(3,4);int i,j;for( i = 0;i 3; +i )for( j = 0; j 4; j + )aij = i * 4 + j;for( i = 0;i 3; +i ) for( j = 0; j 4; j + ) cout a(i,j) ,;cout endl;cout next endl;Array2 b; b = a;for( i = 0;i 3; +i ) for( j = 0; j 4; j + ) cout bij ,;cout endl;return 0;7.编写一个Mystring类,使得程序的输出结果是1. abcd-ef

20、gh-abcd-2. abcd-3.3. abcd-efgh-4. efgh-5. c6. abcd-7. ijAl-8. ijAl-mnop9. qrst-abcd-10. abcd-qrst-abcd- uvw xyzaboutbigmetakeabcd qrst-abcd-答案:#include #include #include #include using namespace std;class MyString : public string public:MyString():string() ;MyString( const char * s):string(s);MyStri

21、ng( const string & s ): string(s);MyString operator。( int s, int l) return this-substr(s,l);int main()MyString s1(abcd-),s2,s3(efgh-),s4(s1);MyString SArray4 = big,me,about,take; cout 1. si s2 s3 s4 endl; s4 = s3;s3 = si + s3;cout 2. si endl;cout 3. s2 endl;cout 4. s3 endl;cout 5. s4 endl;cout 6. s1

22、2 endl;s2 = si;si = ijkl-;si2 = A;cout 7. s2 endl;cout 8. si endl;si += mnop;cout 9. si endl;s4 = qrst- + s2;cout i0. s4 endl;si = s2 + s4 + uvw + xyz;cout 11. si endl; sort(SArray,SArray+4);for( int i = 0;i 4;i + )cout SArrayi endl;s1的从下标0开始长度为4的子串cout s1(0,4) endl;s1的从下标5开始长度为10的子串cout s1(5,10) en

23、dl;return 0;第14章()。1 以下说法不正确的是(假设在公有派生情况下)A. 可以将基类对象赋值给派生类对象B. 可以将派生类对象的地址赋值给基类指针c.可以将派生类对象赋值给基类的引用D.可以将派生类对象赋值给基类对象答案:A2写岀下面程序的输岀结果。#includeusing namespace std;class Bpublic:B()coutvB_Convendl;B()coutvB_Desvendl;class C:public Bpublic:C()coutvC_Convendl;C()coutvvC_Desvvendl;int main()C * pc = new C

24、;delete pc;return 0;答案:B_ConC_ConC_DesB_Des3. 写出下面程序的输出结果。#includeusing namespace std;class Basepublic:int val;Base()coutvBase Constructorendl;Base()coutBase Destructorsubstr(s,l);5.完成附魔兽世界作业中的的二阶段。(省略)第15章1 以下说法正确的是()A. 在虚函数中不能使用this指针B. 在构造甬数中调用虚函数,不是动态联编C. 抽象类的成员函数都是纯虚函数D. 构造函数和析构函数都不是虚构函数答案:B2写岀

25、下面程序的输岀结果#includeusing namespace std;class Apublic:A()virtual void func()coutvA:funcvendl;A()virtual void fund()coutvA:fundvendl;class B:public Apublic:B()func();void fun()func();B()fund();class C:public Bpublic:C()void func() coutvC:funcvendl;C()fund();void fund() coutvC:fundvendl;;int main()C c;re

26、turn 0;答案:A:funcC:fundA:fund3.写出下面程序的输出结果。#includeusing namespace std;class Apublic:virtual A();class B:public Apublic:virtual B() coutvvDestructBvvendl;class C:public Bpublic:virtual C() coutvvDestructCvvendl;int main()A * pa = new C;delete pa;A a;return 0;答案:DestructCDestructBDestructADestructA4.

27、写出下面程序的输出结果:#includeusing namespace std;class Apublic:virtual void func() coutvA:funcvendl;virtual void fund() coutvA:fundvendl;void fun() coutvA:funvfun();B * pb = new C();pb_fun();return 0;答案:A:funcA:funA:funcC:func5下面程序的输岀结果是#includeusing namespace std;class Aprivate:int nVal;public:void Fun() co

28、utvA:Funvendl;void Do() coutvA:Dovendl;class B:public Apublic:virtual void Do() coutvB:Dovendl;class C:public Bpublic:void Do() coutvvC:Dovvendl;void Fun() coutvvC:Funvvendl;void Call(B & p)p.Fun();p.Do();int main()C c;Call(c);system(PAUSE);return 0;答案:A:FunC:Do6下面程序的输岀结果是destructor Bdestructor A请完整

29、写出class A。限制条件:不能为 class A编写构造函数。#includeusing namespace std;class A;class B:public AB()coutdestructor Bendl;int main()A * pa;pa = new B;delete pa;return 0;答案:public:virtual A()coutdestructor Aendl;7下面的程序输岀结果是A:FunA:DoA:FunC:Do请填空:#includeusing namespace std;class Aprivate:int nVal;public:void Fun()

30、 coutvA:Funvendl;virtual void Do() coutvA:DovFun();p-Do();int main() Call(new A();Call(new C();答案:A *p8完成附录魔兽世界大作业中提到的终极版本作业。(省略)第16章1. C+标准类库中有哪几个流类,用途分别如何?它们之问的关系如何?答案:c+ 标准库中有 istream , ostream , ifstream , ofstream , iostream , fstream 等流类。 istream是用于输入的流类:ostream 是用于输出的流类:ifstream是用于从文件读取数据的类:o

31、fstream是用于向文件写入数据的类:iostream 是既能用千输入,又能输出的类:fstream是既能从文件读取数据,又能向文件写入数据的类os 是个抽象基类,它派生出istream 和ostream;stream 派生出 ifstream ,ostream 派生出 ofstream , istream 和 ostream 共同派生了ostream ; iostream 派生出 fstream 。2 cin是哪个类的对象?cout是哪个类的对象?答案:cln 是istream 类的对象: cout 是 ostream 类的对象。3编写程序,读取一行文字,然后将此行文字颠倒后输岀输人样倒:

32、These are 100 dogs输出样例:.sgod 001 eseht答案:#include#include using namespace std;void swap(char *s)int len, i;char tmp;len = strlen(s)-1;for (i = 0; i = len/2; +i)tmp = si;si = slen-i;slen-i = tmp;int main(void)char s = There are 100 dods.;coutvsvvn;swap(s);coutvvsvvendl;return 0;4 编写程序,输入若干个实数,对于每个实数,

33、先以非科学计数法输岀,小数点后面保留5位有效数字;再以科学计数法输出,小数点后面保留7位有效数字。输入以 Ctrl+Z结束。输入样例:12.34123456.892255输出样例:12.340001.2340000e+0011.2346e+0051.2345689e+005答案:#include using namespace std; int main()double num;while(cin num)/ctrl + z是到达尾部cout.setf(ios:fixed);/用定点格式显示浮点数cout.precision(5);/sets this number to be five co

34、ut num endl; cout.unsetf(ios:fixed);/remove the setf(ios:fixed)cout.setf(ios:scientific);cout.precision(7);cout num endl;return 0;5编写程序,输入若干个整数。对于每个整数,先将该整数以十六进制输岀,然后再将整数以10个字符的宽度输出,宽度不足时在左边补0输入人以Ctrl+Z结束.输入样倒:2316输出样例:170000000017100000000010答案:#include #include using namespace std ;int main()int x

35、 ;while( cinx ) cout hex x endl;cout setw(10) setfill(0) x endl ;return 0;6. primf和scanf与cout和cin相比有什么优势?数据操作数据尺寸输入输岀流耗时(s)GoodBoy读一千万次cin38.570scanfscanf4.204写一千万次cout5.206coutprintf6.202第17章1 打开文件只读不写,可以用哪个类?打开文件只写不读,可以用哪个类,打开文件 既读又写,用哪个类?答案:打打开文件只读不写用ifstream开文件只写不读用ofstream打开文件叉读又写用fstream2 编程将一

36、个C+程序文件中的注释以及函数体内部的语句全部去掉后输岀到一个新的文件。假设/ * /”形式的注释没有嵌套。若程序名为proccpp,则以命令行方式运行之:Procpp testl.cpp result2.cpp能够将testl. cpp 处理后的结果生成为result2. cpp 文件。输入样例:#include#includeusing namepsace std;/*test sample*/void func( int n)if(n)cout ” in func ” f;retun 0;输出样例:#include # include using namespace std;void f

37、unc() int main() 答案:#include #include #include using namespace std;int main() int deleteline;string line;ifstream fin;fin.open(example.txt);fstream temp;temp.open(temp.txt);int i=0;while (getline(fin,line)line.replace(line.find(deleteline),deleteline .1 ength(),);/temp line =10&i=1 &i =6)temp line e

38、ndl;temp.close();fin.close();return 0;3,编写一个程序 doublefile , exe,运行方式如下: doubleflie 文件名则能将指定文件内容加倍,文件未必是文本文件。例如有some, txt文件内容如下:ThLs is a filePlease double lt最后一行后面没有换行。则经过处理后This is a file.Please double it, This is a file.Please double id.答案:#include #include #include using namespace std;int main()i

39、fstream in(1.txt);ofstream out(2.txt);|string line;string content;some, txt 文件内容如下if(in) /有该文件while (getline (in, line) / line中不包括每行的换行符cout line endl;content=content+line+n:out line endl; /输入到 2.txt 中cout content;out content; else /没有该文件cout no such file endl;in.close();out.close();return 0;4编写一个学生

40、记录处理程序。学生记录用以下类表示claas char name10;int ID;double gpa;学生记录文件名为students. dat.要求是二进制文件,文件中的每个记录和上述类的对象相对应。students. dat最初不存在。程序可以多次运行,如果第一敬运行程序后students . dat生成了,则以后再运行时,程序应基干已经存在的students. dat进行操作。程序运行时能接受以下几种命令:1) Add姓名学号成绩例如 I Add Tom 1234567 78.5添加学生信息姓名和学号都不会包含空格。姓名由最多9个字母组成-学号是整数。可能重名,但学号不会重复。如果发

41、现相同学号的学生已经存在,则不添加学生信息,而是输出:“Aiready entered ”。2) Search 姓名根据姓名查找学生信息,并输岀。如果有重名的,把重名的学生信息全部输岀。辅岀格式为;每个学生信息输出为一行,查不到则输出“Not FoundH。3) Search 学号根据学号查找学生信息,并输出。输出格式为:姓名学号成绩每个学生信息输出为一行。查不到则输出“Not Found。4) Modify学号成绩根据学号修改学生的成绩并输出“ Done。如果找不到该学号的学生,则输出“N otFound。假定学生记录非常多,所以不能采取用一个大数组把全部学生记录都读取到内存的做法。答案:#

42、include #include #include using namespace std; class Studentchar name50;int stuID;double grades;public:void Add() Jcin.getline(name,50);cin stuID grades;void showData()cout name stuID grades;int retname()return 1;void setdata() cout vDone;/* function to write in a binary file.*/void write_record()ofstream outFile;outFile.open(student.dat, ios:binary | ios:app);Student obj;obj.Add();outFile.write(char* )&obj, sizeof(obj);outFile.close();* function to display records of filevoid display()ifstream inFile;inFi

温馨提示

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

评论

0/150

提交评论