C 语言程序设计期末考试试题及答案.docx

上传人:b****2 文档编号:1929097 上传时间:2022-10-25 格式:DOCX 页数:14 大小:24.32KB
下载 相关 举报
C 语言程序设计期末考试试题及答案.docx_第1页
第1页 / 共14页
C 语言程序设计期末考试试题及答案.docx_第2页
第2页 / 共14页
C 语言程序设计期末考试试题及答案.docx_第3页
第3页 / 共14页
C 语言程序设计期末考试试题及答案.docx_第4页
第4页 / 共14页
C 语言程序设计期末考试试题及答案.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

C 语言程序设计期末考试试题及答案.docx

《C 语言程序设计期末考试试题及答案.docx》由会员分享,可在线阅读,更多相关《C 语言程序设计期末考试试题及答案.docx(14页珍藏版)》请在冰豆网上搜索。

C 语言程序设计期末考试试题及答案.docx

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

C++语言程序设计

期末考试试卷及答案

一、填空

1.在类中必须声明成员函数的原型,成员函数的实现部分可以写在类外。

2.如果需要在被调函数运行期间,改变主调函数中实参变量的值,则函数的形参应该是

引用类型或指针类型。

3.抽象类只能作为基类使用,而不能声明它的对象。

4.进行函数重载时,被重载的同名函数如果都没有用const修饰,则它们的形参个数

或类型必须不同。

5.通过一个常对象只能调用它的常成员函数,不能调用其他成员函数。

6.函数的递归调用是指函数直接或间接地调用自身。

7.拷贝构造函数的形参必须是本类对象的引用。

二、阅读下列程序,写出其运行时的输出结果如果程序运行时会出现错误,请简要描述错误原因。

1.请在以下两题中任选一题,该题得分即为本小题得分。

如两题都答,则取两题得分之平均值为本小题得分。

(1)程序:

{#include

private:

#include

classBase。

intnpublic:

{private:

Derived1(intm=1):

charmsg[30]。

Base(Base,m-1)

protected:

}{n=m。

intn。

voidoutput(void)public:

Base(chars[],intm=0):

n(m){cout<

:

output(){strcpy(msg,s)。

}}

voidoutput(void)

}classDerived2:

publicDerived1{cout<

{}

private:

}。

intnclassDerived1:

publicBase

1/9

public:

}intmain()Derived2(intm=2):

{

Derived1(m-1)

BaseB(BaseClass,1)。

{n=m。

}

Derived2D。

voidoutput(void)

B.output()。

{cout<

D.output()。

Derived1:

:

output()

}

}

运行结果:

1

BaseClass

2

1

0

Base

2)程序:

{#include

Samp*p。

classSamp

p=newSamp[5]{public:

if(!

p)voidSetij(inta,int

cout<

b){i=a,j=b}

{

牥潲屲湜~Samp()。

return1{。

}

cout<

j<5for(intj=0}

}。

intGetMuti(){returni*jp[j].Setij(j,j)。

protected:

k<5for(intk=0。

k++)

cout<

inti

is:

intj

<

}。

delete[]p。

return0。

}

intmain()

2/9

运行结果:

Muti[0]is:

0

Muti[1]is:

1

Muti[2]is:

4

Muti[3]is:

9

Muti[4]is:

16

Destroying..4

Destroying..3

Destroying..2

Destroying..1

Destroying..0

.请在以下两题中任选一题,该题得分即为本小题得分。

如两题都答,则取两题得分之平均2值为本小题得分。

)程序:

(1

#include

#include

classVector

int&Vector:

:

Elem(intndx){

{

public:

if(ndx<0||ndx>=size)。

Vector(ints=100){

int&Elem(intndx)。

cout<

exit

(1)voidSet(void)。

}

~Vector(void)。

protected:

returnbuffer[ndx]

}intsize。

int*buffer

voidVector:

:

Display(void)}。

{

j++)Vector:

:

Vector(ints)j

for(intj=0

{

cout<

}。

buffer=newint[size=s]}

3/9

voidVector:

:

Set(void)

intmain(){

{

j

j++)for(intj=0。

Vectora(10)Elem(j)=j+1。

}Vectorb(a)a.Set()。

b.Display()。

Vector:

:

~Vector(void)

{

}

delete[]buffer。

}

运行结果:

1

2

3

4

5

6

7

8

9

10

与a共用同一个b是进行的是浅拷贝,b最后出现错误信息,原因是:

声明对象buffer,程序结束前调用析构函数时对同一内存区进行了两次释放。

运行结果:

(2)程序:

#includeintGetAge(){return

*itsAge。

}

classCAT

voidSetAge(intage){

}public:

{*itsAge=ageprotected:

CAT()

CAT(const&CAT)。

int*itsAge。

~CAT()。

}。

4/9

intmain()

{CAT:

:

CAT()

CATa。

{

cout<

<

itsAge=newint。

a.SetAge(6)。

*itsAge=5

CATb(a)。

}

cout<

<

:

~CAT()。

cout<<'sage:

<

a.SetAge(7)deleteitsAge。

cout<

<

itsAge=NULLcout<<'sage:

<

}

a'sage:

5

a'sage:

6

b'sage:

6

a'sage:

7

b'sage:

7

是进行的是浅拷贝,b与最后出现错误信息,原因是:

声明对象ba共用同一个buffer,程序结束前调用析构函数时对同一内存区进行了两次释放。

三、阅读下列程序及说明和注释信息,在方框中填写适当的程序段,使程序完成指定的功能

程序功能说明:

从键盘读入两个分别按由小到大次序排列的整数序列,每个序列10个整数,整数间以空白符分隔。

用这两个序列分别构造两个单链表,每个链表有10个结点,结点的数据分别按由小到大次序排列。

然后将两个链表合成为一个新的链表,新链表的结点数据仍然按由小到大次序排列。

最后按次序输出合并后新链表各结点的数据。

程序运行结果如下,带下划线部分表示输入内容,其余是输出内容:

135791113151719

2468101214161820

1234567891011121314151617181920

#include

5/9

#include

//类定义部分

template

classNode

{

private:

Node*next。

//指向后继节点的指针

public:

Tdata。

//数据域

Node(constT&item,Node*ptrnext=NULL)。

//构造函数

voidInsertAfter(Node*p)。

//在本节点之后插入一个同类节点p

Node*DeleteAfter(void)。

//删除本节点的后继节点,返回其地址

Node*NextNode(void)const。

//获取后继节点的地址

}。

template

classLinkedList

{

private:

Node*front,*rear。

//表头和表尾指针

Node*prevPtr,*currPtr。

//记录表当前遍历位置的指针,由插入和删除操作更新

intsize。

//表中的元素个数

intposition。

//当前元素在表中的位置序号。

由函数Reset使用

Node*GetNode(constT&item,Node*ptrNext=NULL)。

//生成新节点,数据域为item,指针域为ptrNext

voidFreeNode(Node*p)。

//释放节点

voidCopyList(constLinkedList&L)。

//将链表L拷贝到当前表//(假设当前表为空)。

被拷贝构造函数、operator=调用

public:

LinkedList(void)。

//构造函数

LinkedList(constLinkedList&L)。

//拷贝构造函数

~LinkedList(void)。

//析构函数

LinkedList&operator=(constLinkedList&L)。

//重载赋值运算符

intListSize(void)const。

//返回链表中元素个数(size)

-6-/9

intListEmpty(void)const。

//size为0时返回TRUE,否则返回FALSE

voidReset(intpos=0)。

//将指针currPtr移动到序号为pos的节点,//prevPtr相应移动,position记录当前节点的序号

voidNext(void)。

//使prevPtr和currPtr移动到下一个节点

intEndOfList(void)const。

//currPtr等于NULL时返回TRUE,否则返回FALSE

intCurrentPo

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 幼儿教育 > 家庭教育

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1