




已阅读5页,还剩34页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
新标准C+程序设计教材11-20章课后题答案第11章:1简述结构化程序设计有什么不足,面向对象的程序如何改进这些不足。 答案: 结构化程序设计的缺点: (1)用户要求难以在系统分析阶段准确定义,致使系统在交付使用时产生许多问题。 (2)用系统开发每个阶段的成果来进行控制,不适应事物变化的要求。 (3)系统的开发周期长。 面向对象的程序设计如何改进这些不足: 面向对象程序设计技术汲取了结构忧程序设计中好的思想,并将这些思想与一些新的、强大的理念相结台,从而蛤程序设计工作提供了一种全新的方法。通常,在面向对象的程序设计风格中,会将一个问题分解为一些相互关联的子集,每个子集内部都包含了相关的数据和函数。同时会以某种方式将这些子集分为不同等级,而一个对象就是已定义的某个类型的变量。 2以下说怯正确的是( )。 A.每个对象内部都有成员函数的实现代码 B.一个类的私有成员函数内部不能访问本类的私有成员变量 C.类的成员函数之间可以互相调用 D.编写一个类时,至少要编写一个成员函数 答案:C3以下对类A的定义正确的是( )。 Aclass A Bclass A private: int v; int v; A * next; public: void Func() void Func() ; Cclass A D. class A int v; int v; public: public: void Func(); A next; ; void Func() A::void Func() ; 答案:B4假设有以下类A: class A public: int func(int a) return a * a; ;以下程序段不正确的是( )。AA a; a.func(5);BA * p = new A;p-func(5);CA a;Ar =a ;r.func(5);DA a,b;if(a!=b)a.func(5);答案:D5以下程序段不正确的是(A)。 Aint main() class A int v; A a;a.v= 3; return 0; Bint main() class A public: int v; A * p; ; A a; a.p=&a; return 0; Cint 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; 答案:A 6实现一个学生信息处理程序。输入:姓名,年龄,学号(整数)。第一学年平均成绩 第二学年平均成绩,第三学年平均成绩,第四学年平均成绩。输出:姓名,年龄,学号, 4年平均成绩。例如: 输入: Tom 18 7817 80 80 90 70 输出: Tom,18,7817,80 要求实现一个代表学生的类,并非所有成员变量都是私有的。 答案:#include #include#include /#includeusing namespace std;class Student private: int age, score1, score2, score3, score4; char name100, num100; double average;public: Student(char aname, int aage, char anum,int ascore1, int ascore2, int ascore3, int ascore4) strcpy(name, aname); age = aage; strcpy(num, anum); score1 = ascore1; score2 = ascore2; score3 = ascore3; score4 = ascore4; double getAverage() return (score1 + score2 + score3 + score4) / 4; char * getName() return name; int getAge() return age; char * getNum() return num; ;int main() char name100, a, num100; int age, score1, score2, score3, score4; cin.getline(name, 100, ); cin age; / a = getchar(); cin.getline(num, 100, ); cin score1 score2 score3 score4; Student s(name, age, num, score1, score2, score3, score4); cout s.getName() , s.getAge() , s.getNum() , s.getAverage(); return 0;第12章1以下说法中正确的是( )。A 一个类一定会有无参构造函数B构造函数的返回值类型是voidC一个类只能定义一个析构函数,但可以定义多个构造函数D一个类只能定义一个构造函数,但可以定义多个析构函数答案:C2对于强过new运算符牛成的对象( )。A程程序结束时自动析构B执行delete操作时才能析构C在包含new语句的函数返回时自动析构D在执行delete操作时会析构,如果没有执行delete操作,则在程序结束时自动析构答案:D3如果某函数的返回值是个对象,则该函数被调用时,返回的对象( )。A是通过复制构造函数初始化的B是通过无参构造函数初始化的C用哪个构造函数初始化取决于函数的return语句是怎么写的D不需要初始化答案:C4以下说法LE确的是( )。A在静态成员函数中可以调用同类的其他任何成员函数Bconst成员函数不能作用于非const对象C在静态成员函数中不能使用this指针D静态成员变量每个对象有各自的一份答案:C5以下关于this指针的说法中不正确的是( )。Aconst成员函数内部不可以使用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=”nendl; ;int main()CSample array12;CSample array22=6,8;CSample 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() Sample 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(int n):_ ;int GetVal()return val;int main()B b1(2);coutb1.GetVal(),b1.A:GetVal()n;return 0;答案:Val(4),A(6)9下面程序的输出结果是05请填空:#includeusing namespace std;class Apublic:int val;A(_)val=n; _ GetObj()return _;int main()A a;couta.valendl;a.GetObj()=5;couta.valendl;答案:int n=0A&*this10下面程序的输出结果是:10请补充Sample类的成员函数,不能增加成员变量#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;coutv=2*obj.v;11.下面程序的输出结构是:5,55,5请填空:#includeusing namespace std;class Basepublic:int k;Base(int n):k(n); class Big public:int v;Base b;Big _ ;Big_ ; ;int main()Big a1(5);Big a2=a1;couta1.v,a1.b.kendl;couta2.v,a2.b.kendl;return 0;答案:(int n):v(n),b(n)(Big &x):v(x.v),b(x.b.k)12完成附录“魔静世界大作业”中提到的第一阶段作业(省略)第13章1如果将运算符“”重载为某个类的成员运算符(也即成员函数),则该成员函数的参数个数是( )。A0个 B . 1个 C . 2个 D3个答案:B 2如果将运算符“*”重载为菜个类的成员运算符(也即成员函数),则该成员函数的参数个数是( )。A0个 B . 1个 C . 2个 D . 0个或1个均可答案:D3下面程序的输出是 3+4i 5+6i请补充Complex类的成员函数,不能加成员变量。#include#includeusing namespace std;class Complexprivate:double r,i;public:void Print()coutr+iiendl; /在这里补充/在这里补充;int main()Complex a;a=3+4i;a.Print();a=5+6i;a.Print();return 0;答案:Complex &operator=(const char t) if(!t) r=0.0; i=0.0; else r = t0 - 0; i = t2 - 0; return *this; 4下面的Mylnt类只有一个成员变量。Mylnt类内部的部分代码被隐藏了。假设下面的程序能编译通过,且输出结果是:4,1 请写出被隐藏的部分(要求编写的内容必须是能全部救进Mylnt类内部的,Mylnt的成员函数不允许使用静态变量)。#includeusing namespace std;class MyIntint nVal;public:MyInt(int n)nVal=n;int ReturnVal()return nVal;/ 在这里补充/在这里补充;int main()MyInt objInt(10);objInt-2-1-3;coutobjInt.ReturnVal();cout,;objInt-2-1;coutobjInt.ReturnVal();return 0; 答案: MyInt& operator - (int i) nVal -= i; return *this;5下面的程序输出结果是 (4,5) (7,8)请填空:#includeusing namespace std;class Pointprivate:int x;int y;public:Point(int x_,int y_):x(x_),y(y_);/_;_operator(_,const Point & p)_;Return_;int main()coutPoint(4,5)Point(7,8);return 0;答案:friend ostream& operator (ostream &o,const Point &p);friend ostream &ostream &oo(p.x,p.y)endlo6编写一个二维数组类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 namespace 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)*row*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; ;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-efgh-abcd-2. abcd-3.4. abcd-efgh-5. efgh-6. c7. abcd-8. ijAl-9. ijAl-mnop10. qrst-abcd-11. abcd-qrst-abcd- uvw xyzaboutbigmetakeabcdqrst-abcd-答案: #include #include #include #include using namespace std;class MyString : public string public: MyString():string() ; MyString( const char * s):string(s); MyString( 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. s1 s2 s3 s4 endl;s4 = s3;s3 = s1 + s3;cout 2. s1 endl;cout 3. s2 endl;cout 4. s3 endl;cout 5. s4 endl;cout 6. s12 endl;s2 = s1;s1 = ijkl-;s12 = A ;cout 7. s2 endl;cout 8. s1 endl;s1 += mnop;cout 9. s1 endl;s4 = qrst- + s2;cout 10. s4 endl;s1 = s2 + s4 + uvw + xyz;cout 11. s1 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) endl;return 0;第14章1以下说法不正确的是(假设在公有派生情况下)( )。A可以将基类对象赋值给派生类对象B可以将派生类对象的地址赋值给基类指针c可以将派生类对象赋值给基类的引用D可以将派生类对象赋值给基类对象答案:A2写出下面程序的输出结果。#includeusing namespace std;class Bpublic:B()coutB_Conendl;B()coutB_Desendl;class C:public Bpublic:C()coutC_Conendl;C()coutC_Desendl;int main()C * pc = new C;delete pc;return 0;答案:B_ConC_ConC_DesB_Des3写出下面程序的输出结果。#includeusing namespace std;class Basepublic:int val;Base()coutBase Constructorendl;Base()coutBase Destructorsubstr(s,l); ;5.完成附魔兽世界作业中的的二阶段。(省略)第15章1以下说法正确的是( )A.在虚函数中不能使用this指针B.在构造甬数中调用虚函数,不是动态联编C.抽象类的成员函数都是纯虚函数D.构造函数和析构函数都不是虚构函数答案:B2写出下面程序的输出结果#includeusing namespace std;class Apublic:A()virtual void func()coutA:funcendl;A()virtual void fund()coutA:fundendl;class B:public Apublic:B()func(); void fun()func();B()fund();class C:public Bpublic:C()void func()coutC:funcendl;C()fund();void fund()coutC:fundendl;int main()C c;return 0;答案:A:funcC:fundA:fund3写出下面程序的输出结果。#includeusing namespace std;class Apublic:virtual A()coutDestructAendl;class B:public Apublic:virtual B()coutDestructBendl;class C:public Bpublic:virtual C()coutDestructCendl;int main()A * pa = new C;delete pa;A a;return 0;答案:DestructCDestructBDestructADestructA4写出下面程序的输出结果:#includeusing namespace std;class Apublic:virtual void func()coutA:funcendl; virtual void fund()coutA:fundendl;void fun()coutA:funendl;class B:public Apublic:B()func();void fun()func();class C:public Bpublic:C()void func()coutC:funcendl;void fund()coutC:fundfun();B * pb = new C();pb-fun();return 0; 答案:A:funcA:funA:funcC:func5下面程序的输出结果是#includeusing namespace std;class Aprivate:int nVal;public:void Fun()coutA:Funendl;void Do()coutA:Doendl;class B:public Apublic:virtual void Do()coutB:Doendl; ;class C:public Bpublic:void Do()coutC:Doendl;void Fun()coutC:Funendl;void Call(B& p)p.Fun();p.Do();int main()C c;Call(c);system(PAUSE);return 0;答案:A:FunC:Do6下面程序的输出结果是destructor Bdestructor A请完整写出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()coutA:Funendl;virtual void Do()coutA:Doendl; ;class B:public Apublic:virtual void Do()coutB:Doendl;class C:public Bpublic:void Do()coutC:Doendl;void Fun()coutC:FunFun();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是用于从文件读取数据的类: ofstream是用于向文件写入数据的类: iostream是既能用千输入,又能输出的类: fstream是既能从文件读取数据,又能向文件写入数据的类。 ios是个抽象基类,它派生出istream和ostream; istream派生出ifstream,ostream派生出ofstream,istream和ostream共同派生了 iostream; iostream派生出fstream。2 cin是哪个类的对象?cout是哪个类的对象?答案:cln是istream类的对象:cout是ostream类的对象。3编写程序,读取一行文字,然后将此行文字颠倒后输出输人样倒: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.;coutsn;swap(s);coutsendl;return 0; 4编写程序,输入若干个实数,对于每个实数,先以非科学计数法输出,小数点后面保留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 cout 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结束 输入样倒: 23 16 输出样例: 17 0000000017 10 0000000010答案:#include #include using namespace std ;int main() int x ; while( cinx ) cout hex x endl; cout setw(10) setfill(0) x endl ; return 0;6primf和scanf与cout和cin相比有什么优势?数据操作 数据尺寸输入输出流耗时(s)GoodBoy读一千万次cinscanf38.5704.204scanf写一千万次coutprintf5.2066.202cout第17章 1打开文件只读不写,可以用哪个类?打开文件只写不读,可以用哪个类,打开文件既读又写,用哪个类?答案: 打开文件只读不写用ifstream 打开文件只写不读用ofstream打开文件叉读又写用fstream 2编程将一个c+程序文件中的注释以及函数体内部的语句全部去掉后输出到一个新的文件。假设“*”形式的注释没有嵌套。若程序名为proccpp,则以命令行方式运行之:Procpp test1.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;voi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 电石炉培训知识课件
- 2.5直线与圆的位置关系 分层练习(含答案)数学苏科版九年级上册
- 北民大附中开学考试题及答案
- 高热危重患者护理课件
- Linoleic-acid-suitable-for-cell-culture-生命科学试剂-MCE
- 2-Methylleucine-CoA-2-Methylleucine-coenzyme-A-生命科学试剂-MCE
- 北海高中分班考试试卷及答案
- 电焊工基础知识培训课件
- 高温季节安全知识培训课件
- 音序考试题及答案
- 工业厂房租赁管理办法
- 2025年综合类-农艺师考试-农艺师考试-园艺工考试-高级花卉工考试历年真题摘选带答案(5卷100题)
- 辽宁动物检疫管理办法
- 小学六年级综合实践环境保护计划
- 肺癌免疫治疗病例分享
- 2025年汽车智能驾驶技术及产业发展白皮书-清华大学
- 2025云南师范大学辅导员考试题库
- 2025年国考行测试题及答案解析
- 联邦学习框架下的设备故障智能诊断算法研究
- 财务岗位安全培训课件
- 2025年贵州省中考语文试卷真题(含答案)
评论
0/150
提交评论