C++语言程序设计期末考试试题及答案[1]

上传人:xy****7 文档编号:43049479 上传时间:2018-06-04 格式:DOC 页数:19 大小:40.50KB
返回 下载 相关 举报
C++语言程序设计期末考试试题及答案[1]_第1页
第1页 / 共19页
C++语言程序设计期末考试试题及答案[1]_第2页
第2页 / 共19页
C++语言程序设计期末考试试题及答案[1]_第3页
第3页 / 共19页
C++语言程序设计期末考试试题及答案[1]_第4页
第4页 / 共19页
C++语言程序设计期末考试试题及答案[1]_第5页
第5页 / 共19页
点击查看更多>>
资源描述

《C++语言程序设计期末考试试题及答案[1]》由会员分享,可在线阅读,更多相关《C++语言程序设计期末考试试题及答案[1](19页珍藏版)》请在金锄头文库上搜索。

1、 1C+语言程序设计语言程序设计语言程序设计语言程序设计 期末考试期末考试期末考试期末考试试题及答案试题及答案试题及答案试题及答案 姓名 _ 学号 _ 班号 _ 题 号 一 二(1) 二(2) 三 总 分 成 绩 一一一一、填空填空填空填空 1在类中必须声明成员函数的 原型 ,成员函数的 实现 部分可以写在类外。 2如果需要在被调函数运行期间,改变主调函数中实参变量的值,则函数的形参应该是 引用 类型或 指针 类型。 3 抽象 类只能作为基类使用,而不能声明它的对象。 4进行函数重载时,被重载的同名函数如果都没有用const修饰,则它们的形参 个数 或 类型 必须不同。 5通过一个 常 对象只

2、能调用它的常成员函数,不能调用其他成员函数。 6函数的递归调用是指函数直接或间接地调用 自身 。 7拷贝构造函数的形参必须是 本类对象的引用 。 二二二二、阅读下列程序阅读下列程序阅读下列程序阅读下列程序,写出其运行时的输出结果写出其运行时的输出结果写出其运行时的输出结果写出其运行时的输出结果 如果程序运行时会出现错误,请简要描述错误原因。 1请在以下两题中任选一题,该题得分即为本小题得分。如两题都答,则取两题得分之平均值为本小题得分。 (1)程序: #include #include class Base private: char msg30; protected: int n; publ

3、ic: Base(char s,int m=0):n(m) strcpy(msg,s); void output(void) 2 coutnendlmsgendl; ; class Derived1:public Base private: int n; public: Derived1(int m=1): Base(Base,m-1) n=m; void output(void) coutnendl; Base:output(); ; class Derived2:public Derived1 private: int n; public: Derived2(int m=2): Deriv

4、ed1(m-1) n=m; void output(void) coutnendl; Derived1:output(); ; int main() Base B(Base Class,1); Derived2 D; B.output(); D.output(); 运行结果: 1 Base Class 2 1 0 Base (2)程序: #include class Samp public: void Setij(int a,int b)i=a,j=b; Samp() coutDestroying.iendl; int GetMuti()return i*j; protected: int i

5、; int j; ; 3int main() Samp *p; p=new Samp5; if(!p) coutAllocation errorn; return 1; for(int j=0;j5;j+) pj.Setij(j,j); for(int k=0;k5;k+) coutMutik is: pk.GetMuti()endl; deletep; return 0; 运行结果: Muti0 is:0 Muti1 is:1 Muti2 is:4 Muti3 is:9 Muti4 is:16 Destroying.4 Destroying.3 Destroying.2 Destroying

6、.1 Destroying.0 2请在以下两题中任选一题,该题得分即为本小题得分。如两题都答,则取两题得分之平均值为本小题得分。 (1)程序: #include #include class Vector public: Vector(int s=100); int& Elem(int ndx); void Display(void); void Set(void); Vector(void); protected: int size; int *buffer; ; Vector:Vector(int s) buffer=new intsize=s; 4int& Vector:Elem(int

7、 ndx) if(ndx=size) couterror in indexendl; exit(1); return bufferndx; void Vector:Display(void) for(int j=0; jsize; j+) coutElem(j)endl; void Vector:Set(void) for(int j=0; jsize; j+) Elem(j)=j+1; Vector:Vector(void) delete buffer; int main() Vector a(10); Vector b(a); a.Set(); b.Display(); 运行结果: 1 2

8、 3 4 5 6 7 8 9 10 最后出现错误信息,原因是:声明对象b是进行的是浅拷贝,b与a共用同一个buffer,程序结束前调用析构函数时对同一内存区进行了两次释放。 5(2)程序: #include class CAT public: CAT(); CAT(const &CAT); CAT(); int GetAge() return *itsAge; void SetAge( int age ) *itsAge=age; protected: int * itsAge; ; CAT:CAT() itsAge=new int; *itsAge=5; CAT:CAT() delete i

9、tsAge; itsAge=NULL; int main() CAT a; coutas age:a.GetAge()endl; a.SetAge(6); CAT b(a); coutas age:a.GetAge()endl; coutbs age:b.GetAge()endl; a.SetAge(7); coutas age:a.GetAge()endl; coutbs age:b.GetAge()endl; 运行结果: as age:5 as age:6 bs age:6 as age:7 bs age:7 最后出现错误信息,原因是:声明对象b是进行的是浅拷贝,b与a共用同一个buffe

10、r,程序结束前调用析构函数时对同一内存区进行了两次释放。 三三三三、阅读下列程序及说明和注释信息阅读下列程序及说明和注释信息阅读下列程序及说明和注释信息阅读下列程序及说明和注释信息,在方框中填写适当的程序段在方框中填写适当的程序段在方框中填写适当的程序段在方框中填写适当的程序段,使程序完成指定的功能使程序完成指定的功能使程序完成指定的功能使程序完成指定的功能 程序功能说明程序功能说明程序功能说明程序功能说明:从键盘读入两个分别按由小到大次序排列的整数序列,每个序列10个整数,整数间以空白符分隔。用这两个序列分别构造两个单链表,每个 - 6 -链表有10个结点,结点的数据分别按由小到大次序排列。

11、然后将两个链表合成为一个新的链表,新链表的结点数据仍然按由小到大次序排列。最后按次序输出合并后新链表各结点的数据。 程序运行结果程序运行结果程序运行结果程序运行结果如下,带下划线部分表示输入内容,其余是输出内容: 1 3 5 7 9 11 13 15 17 19 2 4 6 8 10 12 14 16 18 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include #include /类定义部分 template class Node private: Node *next; /指向后继节点的指针 public: T data

12、; /数据域 Node (const T& item, Node* ptrnext = NULL); / 构造函数 void InsertAfter(Node *p); /在本节点之后插入一个同类节点p Node *DeleteAfter(void); /删除本节点的后继节点,返回其地址 Node *NextNode(void) const; / 获取后继节点的地址 ; template class LinkedList private: Node *front, *rear; / 表头和表尾指针 Node *prevPtr, *currPtr; /记录表当前遍历位置的指针,由插入和删除操作更

13、新 int size; / 表中的元素个数 int position; / 当前元素在表中的位置序号。由函数Reset使用 Node *GetNode(const T& item,Node *ptrNext=NULL); / 生成新节点,数据域为item,指针域为ptrNext void FreeNode(Node *p); /释放节点 - 7 - void CopyList(const LinkedList& L); / 将链表L 拷贝到当前表 /(假设当前表为空)。被拷贝构造函数、operator=调用 public: LinkedList(void); / 构造函数 LinkedList

14、(const LinkedList& L); /拷贝构造函数 LinkedList(void); / 析构函数 LinkedList& operator= (const LinkedList& L);/重载赋值运算符 int ListSize(void) const; /返回链表中元素个数(size) int ListEmpty(void) const; /size为0时返回TRUE,否则返回FALSE void Reset(int pos = 0); /将指针currPtr移动到序号为pos的节点, /prevPtr相应移动,position记录当前节点的序号 void Next(void)

15、; /使prevPtr和currPtr移动到下一个节点 int EndOfList(void) const; / currPtr等于NULL时返回TRUE, 否则返回FALSE int CurrentPosition(void) const; /返回数据成员position void InsertFront(const T& item); /在表头插入一个数据域为item的节点 void InsertRear(const T& item); /在表尾添加一个数据域为item的节点 void InsertAt(const T& item); /在当前节点之前插入一个数据域为item的节点 voi

16、d InsertAfter(const T& item); /在当前节点之后插入一个数据域为item的节点 T DeleteFront(void); /删除头节点,释放节点空间,更新prevPtr、currPtr和size void DeleteAt(void); /删除当前节点,释放节点空间,更新prevPtr、currPtr和size T& Data(void); / 返回对当前节点成员data的引用 void ClearList(void); / 清空链表:释放所有节点的内存空间。 ; /类实现部分略. template void MergeList(LinkedList* la, Li

17、nkedList* lb,LinkedList* lc) /合并链表la和lb,构成新链表lc。 /函数结束后,程序的数据所占内存空间总数不因此函数的运行而增加。 - 8 - while ( !la-ListEmpty() &!lb-ListEmpty() if (la-Data()Data() lc-InsertRear(la-Data(); la-DeleteAt(); else lc-InsertRear(lb-Data(); lb-DeleteAt(); while ( !la-ListEmpty() ) lc-InsertRear(la-Data(); la-DeleteAt();

18、while ( !lb-ListEmpty() ) lc-InsertRear(lb-Data(); lb-DeleteAt(); int main() LinkedList la, lb, lc; int item, i; /读如数据建立链表la for (i=0;i item; la.InsertRear(item); - 9 - la.Reset(); /读如数据建立链表lb for (i=0;i item; lb.InsertRear(item); lb.Reset(); MergeList(&la, &lb, &lc);/合并链表 lc.Reset(); / 输出各节点数据,直到链表尾 while(!lc.EndOfList() cout lc.Data() ; lc.Next(); / 使currPtr指向下一个节点 cout endl;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号