C++语言程序设计期末考试试题附答案_第1页
C++语言程序设计期末考试试题附答案_第2页
免费预览已结束,剩余9页可下载查看

下载本文档

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

文档简介

1、1/ 9C+语言程序设计期末考试试题及答案一、填空1 在类中必须声明成员函数的原型 ,成员函数的 实现 部分可以写在类外。2 如果需要在被调函数运行期间,改变主调函数中实参变量的值,则函数的形参应该是_ 引用类型或指针_类型。个人收集整理 勿做商业用途3. 抽象类只能作为基类使用,而不能声明它的对象。4进行函数重载时,被重载的同名函数如果都没有用const 修饰,则它们的形参 个数 或类型 必须不同。资料个人收集整理,勿做商业用途5 通过一个 常 对象只能调用它的常成员函数,不能调用其他成员函数。6函数的递归调用是指函数直接或间接地调用自身 。7 拷贝构造函数的形参必须是本类对象的引用。二、阅

2、读下列程序,写出其运行时的输出结果 如果程序运行时会出现错误,请简要描述错误原因。1 请在以下两题中任选一题,该题得分即为本小题得分。如两题都答,则取两题得分之平均值为本小题得分。(1)程序:#i nclude #in clude class Base private:char msg30;protected:int n;public:Base(char s,i nt m=0): n(m) strcpy(msg,s);void output(void) cout nen dlmsge ndl;class Derived1:public Baseprivate:int n;public:Deri

3、ved1(i nt m=1):Base(Base,m-1) n=m; void output(void) cout nen dl;Base:output();class Derived2:public Derived1private:int n;2/ 9运行结果:1Base Class210Base(2)程序:#in clude class SampSamp *p;public:p=new Samp5;void Setij(i nt a,i nt b)i=a,j=b;if(!p)Samp()coutAllocation errorn;coutDestro yin g.ie ndl;return

4、 1;int GetMuti()return i*j;for(i nt j=0;j5;j+)protected:pj.Setij(j,j);int i;for(int k=0;k5;k+)int j;coutMutik is:;pk.GetMuti()e ndl;deletep;return 0;int mai n()public:Derived2(i nt m=2):Derivedl(m-I) n=m; void output(void) cout nen dl;Derived1:output();;int mai n()Base B(Base Class,1);Derived2 D;B.o

5、utput();D.output();3/ 9运行结果:Muti0 is:04/ 9Muti1 is:1Muti2 is:4Muti3 is:9Muti4 is:16Destroyi ng.4Destro yin g.3Destroyi ng.2Destro yin g.1Destro yin g.05/ 92.请在以下两题中任选 值为本小题得分。(1)程序:#i nclude #in clude class Vectorpublic:Vector(i nt s=100);int& Elem(i nt ndx);void Display(void);void Set(void);Vec

6、tor(void);protected:int size; int *buffer;Vector:Vector(i nt s)buffer=newintsize=s;题,该题得分即为本小题得分。如两题都答,则取两题得分之平均int& Vector:Elem(int ndx)if(n dx=size)couterror in in dexendl exit(1);retur n buffer ndx;void Vector:Display(void)for(i nt j=0; jsize; j+)coutElem(j)e ndl;void Vector:Set(void)for(i nt

7、 j=0; jsize; j+)Elem(j)=j+1;6/ 9Vector:Vector(void) delete buffer;int mai n()运行结果:12345678910最后出现错误信息,原因是:声明对象 b 是进行的是浅拷贝,b 与 a 共用同一个 buffer , 程序结束前调用析构函数时对同一内存区进行了两次释放。资料个人收集整理,勿做商业用途(2)程序:#in cludeclass CATpublic:CAT();CAT(co nst & CAT);CAT();int GetAge() return *itsAge; void SetAge( int age )

8、Vector a(10);Vector b(a);a.Set();b.Display(); *itsAge=age; protected:int * itsAge;CAT:CAT()itsAge=new int;*itsAge=5;7/ 9CAT a;coutas age:a.GetAge()e ndl; a.SetAge(6);CAT b(a);coutas age:a.GetAge()e ndl;coutbs age:b.GetAge()e ndl;a.SetAge(7);coutas age:a.GetAge()e ndl; coutbs age:b.GetAge()e ndl;运行结果

9、:as age:5as age:6bs age:6as age:7bs age:7最后出现错误信息,原因是:声明对象 b 是进行的是浅拷贝,b 与 a 共用同一个 buffer , 程序结束前调用析构函数时对同一内存区进行了两次释放。资料个人收集整理,勿做商业用途三、阅读下列程序及说明和注释信息,在方框中填写适当的程序段,使程序完成指定的功能程序功能说明:从键盘读入两个分别按由小到大次序排列的整数序列,每个序列 10 个整数,整数间以空白符分隔。用这两个序列分别构造两个单链表,每个链表有10 个结点,结点的数据分别按由小到大次序排列。然后将两个链表合成为一个新的链表,新链表的结点数据 仍然按由

10、小到大次序排列。最后按次序输出合并后新链表各结点的数据。资料个人收集整理,勿做 商业用途程序运行结果如下,带下划线部分表示输入内容,其余是输出内容:1 3 5 7 9 11 13 15 17 19 2 4 6 8 10 12 14 16 18 201 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20#in clude #in clude /类定义部分CAT:CAT()delete itsAge;itsAge=NULL;int mai n()-8- / 9template class Nodeprivate:Node *next; /指向后继节点的

11、指针public:T data; / 数据域Node (const T& item, Node* ptrnext = NULL); /;template class LinkedListprivate:Node *front, *rear; /表头和表尾指针Node *prevPtr, *currPtr; /记录表当前遍历位置的指针 , 由插入和删除操作更新资料个人收集整理,勿做商业用途int size; / 表中的元素个数int position; /当前元素在表中的位置序号。由函数Node *GetNode(const T& item,Node *ptrNext=NULL)

12、;构造函数资料个人收集整理,勿做商业用途void InsertAfter(Node*p); / Node *DeleteAfter(void); /Node *NextNode(void) const; /在本节点之后插入一个同类节点 p 删除本节点的后继节点 , 返回其地址 获取后继节点的地址Reset 使用资料个人收集整理,勿做商业用途/生成新节点,数据域为 item ,指针域为 ptrNextvoid FreeNode(Node *p); / 释放节点void CopyList(const LinkedList& L); /operator= 调用资料个人收集整理,勿做商业用途p

13、ublic:LinkedList(void); / 构造函数LinkedList(const LinkedList& L); /将链表 L 拷贝到当前表假设当前表为空)。被拷贝构造函数、拷贝构造函数LinkedList(void); / 析构函数-9- / 9T DeleteFront(void); / 删除头节点 , 释放节点空间,更新 prevPtr 、 currPtr 和 size资料个人收集整理,勿做商业用途void DeleteAt(void); / 删除当前节点 , 释放节点空间,更新 prevPtr 、currPtr 和 size资料个人收集整理,勿做商业用途返回对当前节

14、点成员 data 的引用清空链表:释放所有节点的内存空间。/ 类实现部分略template void MergeList(LinkedList* la, LinkedList* lb,LinkedList* lc)资料个人收集整理,勿做商业用途LinkedList& operator= (const LinkedList& L);/重载赋值运算符资料个人收集整理,勿做商业用途int ListSize(void) const; /int ListEmpty(void) const; /size人收集整理,勿做商业用途void Reset(int pos = 0); /prevPt

15、r 序号资料个人收集整理,勿做商业用途返回链表中元素个数( size )为 0 时返回 TRUE,否则返回 FALSE资料个将指针 currPtr 移动到序号为 pos 的节点, 相应移动 ,position 记录当前节点的void Next(void); /使 prevPtr 和 currPtr 移动到下一个节点intEndOfList(void)const; / currPtr 等于 NULL 时返回 TRUE,否则返回 FALSE资料个人收集整理,勿做商业用途int CurrentPosition(void) const; /void InsertFront(const T& i

16、tem); /资料个人收集整理,勿做商业用途void InsertRear(const T& item); /料个人收集整理,勿做商业用途void InsertAt(const T& item); /的节点资料个人收集整理,勿做商业用途void InsertAfter(const T& item); /的节点资料个人收集整理,勿做商业用途返回数据成员 position在表头插入一个数据域为 item 的节点在表尾添加一个数据域为 item 的节点资在当前节点之前插入一个数据域为 item在当前节点之后插入一个数据域为 itemT& Data(void); / v

17、oidClearList(void); / ;-10- / 9II合并链表 la 和 lb,构成新链表 lc。函数结束后,程序的数据所占内存空间总数不因此函数的运行而增加。while ( !la-ListEmpty() &!lb-ListEmpty() if (la-Data()Data()lc-I nsertRear(la-Data();la-DeleteAt();elselc-I nsertRear(lb-Data();lb-DeleteAt();while ( !la-ListEmpty()lc-I nsertRear(la-Data(); la-DeleteAt();while ( !lb-ListEmpty()lc-I nsertRear(lb-Data(); lb-DeleteAt(); int mai n()Lin kedList la, lb, lc; int item, i;读如数据建立链表 lafor (i=0;i item;la.

温馨提示

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

评论

0/150

提交评论