C++笔试题目带答案Word格式.docx

上传人:b****2 文档编号:1442074 上传时间:2023-04-30 格式:DOCX 页数:21 大小:20.41KB
下载 相关 举报
C++笔试题目带答案Word格式.docx_第1页
第1页 / 共21页
C++笔试题目带答案Word格式.docx_第2页
第2页 / 共21页
C++笔试题目带答案Word格式.docx_第3页
第3页 / 共21页
C++笔试题目带答案Word格式.docx_第4页
第4页 / 共21页
C++笔试题目带答案Word格式.docx_第5页
第5页 / 共21页
C++笔试题目带答案Word格式.docx_第6页
第6页 / 共21页
C++笔试题目带答案Word格式.docx_第7页
第7页 / 共21页
C++笔试题目带答案Word格式.docx_第8页
第8页 / 共21页
C++笔试题目带答案Word格式.docx_第9页
第9页 / 共21页
C++笔试题目带答案Word格式.docx_第10页
第10页 / 共21页
C++笔试题目带答案Word格式.docx_第11页
第11页 / 共21页
C++笔试题目带答案Word格式.docx_第12页
第12页 / 共21页
C++笔试题目带答案Word格式.docx_第13页
第13页 / 共21页
C++笔试题目带答案Word格式.docx_第14页
第14页 / 共21页
C++笔试题目带答案Word格式.docx_第15页
第15页 / 共21页
C++笔试题目带答案Word格式.docx_第16页
第16页 / 共21页
C++笔试题目带答案Word格式.docx_第17页
第17页 / 共21页
C++笔试题目带答案Word格式.docx_第18页
第18页 / 共21页
C++笔试题目带答案Word格式.docx_第19页
第19页 / 共21页
C++笔试题目带答案Word格式.docx_第20页
第20页 / 共21页
亲,该文档总共21页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

C++笔试题目带答案Word格式.docx

《C++笔试题目带答案Word格式.docx》由会员分享,可在线阅读,更多相关《C++笔试题目带答案Word格式.docx(21页珍藏版)》请在冰点文库上搜索。

C++笔试题目带答案Word格式.docx

5.下列关于虚函数的说明中,正确的是:

B

A.从虚基类继承的函数都是虚函数B.虚函数不得是静态成员函数

C.只能通过指针或者引用调用虚函数D.抽象类中的中的成员函数都是虚函数。

6.已知Value是个类,value是Value的一个对象。

下列以非成员函数形式重载的运算符函数原型中,正确的是:

A

A.Valueoperator+(Valuev,inti);

B.Valueoperator+(Valuev=value,inti);

C.Valueoperator+(Valuev,int=0);

D.Valueoperator+(Valuev=value,inti=0);

7.有如下类的定义:

ClassMyClass

intvalue;

MyClass(intn):

value(n){}

intgetValue()const{returnvalue;

}

则类Myclass的构造函数的个数是:

A.1个B.2个

C.3个D.4个

还有默认拷贝构造函数,应该选B

8.有如下类的定义:

classConstants

staticdoubleGetPI(void){return3.14159;

Constantsconstants;

下列各组语句中,能输出3.14159的是:

A.cout<

<

constants->

GetPI();

和cout<

Constants:

:

B.cout<

constants.GetPI();

Constants.GetPI();

C.cout<

Constants->

D.cout<

9.有如下程序:

#include<

iostream>

usingnamespacestd;

classVAC

intf()const{return3;

intf(){return5;

intmain()

VACv1;

constVACv2;

cout<

v1.f()<

v2.f();

return0;

运行时的输出结果是:

A.53B.35

C.55D.33

10.有如下类声明:

classBase

protected:

intamount;

Base(intn=0):

amount(n){}

intgetAmount()const{returnamount;

classDerived:

publicBase

Derived(intm,intn):

value(m),Base(n){}

intgetData()const{returnvalue+amount;

已知x是一个Derived对象,则下列表达式中正确的是:

A.x.value+x.getAmount();

B.x.getData()+x.getAmount();

C.x.getData()–x.amount;

D.x.value+x.amount;

二、填空题(8*2=16)

400_64444return*this_DogspeakVoice

1.下列中a的值是___400_____

#defineAAA200

#defineBBBAAA+100

inta=BBB*2

2.以下为WindowsNT下的32位C++程序,请计算sizeof的值。

  charstr[]=“Hello”;

char*p=str;

intn=10;

请计算

sizeof(str)=____5_____

sizeof(p)=____4___

sizeof(n)=____4____

voidFunc(charstr[100])

 //请计算

 sizeof(str)=___4______

void*p=malloc(100);

//请计算

sizeof(p)=_____4____

3.补充完整下面的类定义:

classXCH{

char*a;

XCH(char*aa){//构造函数

a=newchar[strlen(aa)+1];

strcpy(a,aa);

}

XCH&

operator=(constXCH&

x){//重载赋值函数

delete[]a;

a=newchar[strlen(x.a)+1];

strcpy(a,x.a);

______;

~XCH(){delete[]a;

____return*this______________

______________________________________________________

4.请写出下面程序的输出结果

#include<

classAnimal{

virtualchar*getType()const{return"

Animal"

;

virtualchar*getVoice()const{return"

Voice"

classDog:

publicAnimal{

char*getType()const{return"

Dog"

char*getVoice()const{return"

Woof"

voidtype(Animal&

a){cout<

a.getType();

voidspeak(Animala){cout<

a.getVoice();

intmain(){

Dogd;

type(d);

cout<

"

speak"

speak(d);

endl;

}

______DogspeakVoice____________

__________________

三、问答题(5*10+9+15=74)

1.编写类String的拷贝构造函数和赋值函数(可以调用C++/C的字符串库函数)(15)。

已知类String的原型为:

classString

{

 public:

  String(constchar*str=NULL);

//普通构造函数

  String(constString&

other);

//拷贝构造函数

  ~String(void);

//析构函数

  String&

operate=(constString&

//赋值函数

private:

  char*m_data;

//用于保存字符串

请编写String的上述4个函数。

//String的析构函数

String:

~String(void)//3分

delete[]m_data;

//由于m_data是内部数据类型,也可以写成deletem_data;

//String的普通构造函数

String(constchar*str)//6分

if(str==NULL)

{

m_data=newchar[1];

//若能加NULL判断则更好

*m_data=‘\0’;

}

else

intlength=strlen(str);

m_data=newchar[length+1];

//若能加NULL判断则更好

strcpy(m_data,str);

//拷贝构造函数

String(constString&

other)//3分

{

intlength=strlen(other.m_data);

strcpy(m_data,other.m_data);

//赋值函数

String&

operate=(constString&

other)//13分

//

(1)检查自赋值//4分

if(this==&

other)

return*this;

//

(2)释放原有的内存资源//3分

//(3)分配新的内存资源,并复制内容//3分

//(4)返回本对象的引用//3分

2.不调用C++/C的字符串库函数,请编写函数strcmp的实现(10)。

3.

intstrcmp(constchar*src,constchar*dst)

char*strcpy(char*strDest,constchar*strSrc)

char*address=strDest;

while(*strDest++=*strSrc++)

NULL;

returnaddress;

4.F(n)=F(n-1)+F(n-2),F(0)=1,F

(1)=1.分别用递归和循环求F(5)(10)。

5.

publicintR(intnum){

if(num<

=0){

num=1;

}elseif(num==1){

}else{

num=R(num-1)+R(num-2);

}

returnnum;

publicintc(intnum){

inta=1;

intb=1;

intc=0;

for(inti=0;

i<

num-2;

i++){

c=a+b;

a=b;

b=c;

returnc;

6.写一算法,对单链表实现就地逆置(不要构造新结点)(10)。

7.

node*reserve(node*head)

node*p1,*p2,*p3;

if((head==NULL)||(head->

next==NULL))

returnhead;

p1=head;

p2=p1->

next;

while(p2!

=NULL)

p3=p2->

p2->

next=p1;

p1=p2;

p2=p3;

head->

next=NULL;

8.从冒泡排序、直接插入排序、二分插入排序和选择排序四种排序算法中,选择其中一种算法,写出它的实现?

(10)

9.

stdio.h>

time.h>

math.h>

malloc.h>

voidBubbleSort(int*L,intN)

{//冒泡

inti,j;

intt;

for(i=1;

i<

=N;

i++)

for(j=N;

j>

i;

j--)

if(L[j]<

L[j-1])

t=L[j];

L[j]=L[j-1];

L[j-1]=t;

intSelectMinKey(int*L,intN,intn)

inti,min=n;

for(i=n+1;

if(L[i]<

L[min])

min=i;

returnmin;

voidSelectSort(int*L,intN)

{//选择

N;

j=SelectMinKey(L,N,i);

if(i!

=j)

t=L[i];

L[i]=L[j];

L[j]=t;

voidInsertSort(int*L,intN)

{//插入

for(i=2;

L[i-1])

L[0]=L[i];

L[i]=L[i-1];

for(j=i-2;

L[0]<

L[j];

L[j+1]=L[j];

L[j+1]=L[0];

voidShellInsert(int*L,intN,intdk)

{//对顺序表L作一趟希尔插入排序。

本算法对算法10.1作了以下修改:

//1.前后记录位置的增量是dk,而不是1;

//2.r[0]只是暂存单元,不是哨兵。

当j<

=0时,插入位置已找到。

for(i=dk+1;

++i)

L[i-dk])

{//需将L.r[i]插入有序增量子表

//暂存在L.r[0]

for(j=i-dk;

(j>

0&

&

L[j]);

j-=dk)

L[j+dk]=L[j];

//记录后移,查找插入位置

L[j+dk]=L[0];

//插入

}//ShellInsert

voidShellSt(int*L,intN,intdlta[],intt)

{//算法10.5

//按增量序列dlta[0..t-1]对顺序表L作希尔排序。

for(intk=0;

k<

t;

++k)

ShellInsert(L,N,dlta[k]);

//一趟增量为dlta[k]的插入排序

}//ShellSort

voidShellSort(int*L,intN)

{//希尔

intt=(int)log(N);

intk,*dlta;

dlta=(int*)malloc(t*4);

//产生增量序列

for(k=0;

k++)

dlta[k]=(int)pow(2,t-k)-1;

ShellSt(L,N,dlta,t);

intN=250;

inti,j,k;

intti[16];

int*L;

srand(time(NULL));

printf("

长度\t|冒泡\t|选择\t|插入\t|希尔\n"

);

--------+-------------------------------------------------------------"

for(j=0;

N<

100000;

j++)

L=(int*)malloc((N+1)*4);

t=0;

L[i]=rand();

ti[t++]=clock();

BubbleSort(L,N);

SelectSort(L,N);

InsertSort(L,N);

ShellSort(L,N);

\n%d\t"

N);

4;

|%d\t"

(ti[2*k+1]-ti[2*k]));

N*=5;

\n\n"

10.一个类中,const类型成员函数的主要作用是什么?

在该函数中可以调用该类的什么类型的成员变量和成员函数?

该类的一个非const对象可以调用const成员函数吗?

11.

(1)可以定义const常量,具有不可变性。

(2)便于进行类型检查,使编译器对处理内容有更多了解,消除了一些隐患。

例如:

voidf(constinti)编译器就会知道i是一个常量,不允许修改;

(3)可以避免意义模糊的数字出现,同样可以很方便地进行参数的调整和修改。

同宏定义一样,可以做到不变则已,一变都变!

(1)中,如果想修改Max的内容,只需要:

constintMax=youwant;

即可!

(4)可以保护被修饰的东西,防止意外的修改,增强程序的健壮性。

还是上面的例子,如果在函数体内修改了i,编译器就会报错;

例如:

voidf(constinti)

(5)为函数重载提供了一个参考。

classA{......

voidf(inti)//一个函数

voidf(inti)const//上一个函数的重载......

(6)可以节省空间,避免不必要的内存分配。

const定义常量从汇编的角度来看,只是给出了对应的内存地址,而不是象#define一样给出的是立即数,所以,const定义的常量在程序运行过程中只有一份拷贝,而#define定义的常量在内存中有若干个拷贝。

(7)提高了效率。

编译器通常不为普通const常量分配存储空间,而是将它们保存在符号表中,这使得它成为一个编译期间的常量,没有了存储与读内存的操作,使得它的效率也很高。

不能修改类的数据成员,不能在函数中调用其他不是const的函数.

1)const对象调用的是const成员函数。

2)非const成员函数可以调用const成员函数,这样做一般是用来避免代码重复,但引入了一个casting动作。

12.下面两个语句的执行效果相同吗?

为什么?

(9)

13.

语句1:

while(*p++){};

语句2:

while(*p){p++;

区别在于退出循环后,p的值不一样

while(*p++);

//当*p=0时,退出循环,此时p++仍然执行了

while(*p)p++;

//当*p=0时,退出循环,此时p++不再被执行

例如char*p="

ABCD"

执行完第一个while循环后,p指向的是'

\0'

后面的一个字节,*p的结果是未知的

而如果是执行第二个循环,则p指向的是'

也就是'

D'

后面的一字节,即*p='

\0

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

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2