c++语言程序设计 期末考试试题及答案

上传人:xzh****18 文档编号:34574884 上传时间:2018-02-25 格式:DOC 页数:9 大小:67.50KB
返回 下载 相关 举报
c++语言程序设计 期末考试试题及答案_第1页
第1页 / 共9页
c++语言程序设计 期末考试试题及答案_第2页
第2页 / 共9页
c++语言程序设计 期末考试试题及答案_第3页
第3页 / 共9页
c++语言程序设计 期末考试试题及答案_第4页
第4页 / 共9页
c++语言程序设计 期末考试试题及答案_第5页
第5页 / 共9页
点击查看更多>>
资源描述

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

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

2、类对象的引用 。二、阅读下列程序,写出其运行时的输出结果如果程序运行时会出现错误,请简要描述错误原因。1请在以下两题中任选一题,该题得分即为本小题得分。如两题都答,则取两题得分之平均值为本小题得分。(1)程序:#include #include class Base private:char msg30;protected:int n;public:Base(char s,int m=0):n(m) strcpy(msg,s);void output(void)2 coutclass Samppublic:void Setij(int a,int b)i=a,j=b;Samp() cout#i

3、nclude class Vectorpublic:Vector(int s=100);intvoid Display(void);void Set(void);Vector(void);protected:int size;int *buffer;Vector:Vector(int s)buffer=new intsize=s;4int& Vector:Elem(int ndx)if(ndx=size)coutclass CATpublic:CAT();CAT(const CAT();int GetAge() return *itsAge; void SetAge( int age ) *i

4、tsAge=age; protected:int * itsAge;CAT:CAT()itsAge=new int;*itsAge=5;CAT:CAT()delete itsAge;itsAge=NULL;int main()CAT a;cout#include /类定义部分template class Nodeprivate:Node *next; /指向后继节点的指针public:T data; /数据域Node (const T / 构造函数void InsertAfter(Node *p); /在本节点之后插入一个同类节点 pNode *DeleteAfter(void); /删除本节

5、点的后继节点,返回其地址Node *NextNode(void) const; / 获取后继节点的地址;template class LinkedListprivate:Node *front, *rear; / 表头和表尾指针Node *prevPtr, *currPtr; /记录表当前遍历位置的指针,由插入和删除操作更新int size; / 表中的元素个数int position; / 当前元素在表中的位置序号。由函数 Reset 使用Node *GetNode(const T/ 生成新节点,数据域为 item,指针域为 ptrNextvoid FreeNode(Node *p); /释

6、放节点- 7 -void CopyList(const LinkedList / 将链表 L 拷贝到当前表/(假设当前表为空)。被拷贝构造函数、operator=调用public:LinkedList(void); / 构造函数LinkedList(const LinkedList /拷贝构造函数LinkedList(void); / 析构函数LinkedList/重载赋值运算符int ListSize(void) const; /返回链表中元素个数(size)int ListEmpty(void) const; /size 为 0 时返回 TRUE,否则返回 FALSEvoid Reset(

7、int pos = 0); /将指针 currPtr 移动到序号为 pos 的节点,/prevPtr 相应移动,position 记录当前节点的序号void Next(void); /使 prevPtr 和 currPtr 移动到下一个节点int EndOfList(void) const; / currPtr 等于 NULL 时返回 TRUE, 否则返回 FALSEint CurrentPosition(void) const; /返回数据成员 positionvoid InsertFront(const T /在表头插入一个数据域为 item 的节点void InsertRear(cons

8、t T /在表尾添加一个数据域为 item 的节点void InsertAt(const T /在当前节点之前插入一个数据域为 item 的节点void InsertAfter(const T /在当前节点之后插入一个数据域为 item 的节点T DeleteFront(void); /删除头节点,释放节点空间,更新 prevPtr、currPtr 和 sizevoid DeleteAt(void); /删除当前节点,释放节点空间,更新 prevPtr、currPtr 和 sizeT / 返回对当前节点成员 data 的引用void ClearList(void); / 清空链表:释放所有节点

9、的内存空间。;/类实现部分略.template void MergeList(LinkedList* la, LinkedList* 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-L

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

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

当前位置:首页 > 中学教育 > 试题/考题 > 高中试题/考题

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