C++笔试题目及答案 .pdfVIP

  • 215
  • 0
  • 约5.38千字
  • 约 16页
  • 2024-07-14 发布于河南
  • 举报

C++笔试题目及答案

C++笔试题目及答案篇1

1.What’soverloadfunctioninC++?

“重载”是指编写一个与已有函数同名但参数表不同的函

数。构成重载的条件是:在相同的范围内(比如说在同一个类

中),函数名字相同,但函数参数不同(要么参数类型不同,

要么参数个数不同,要么两者都不同)

2.A.What’sinlinefunctioninC++?

内联函数是指嵌入代码,就是在调用函数的地方不是跳

转,而是直接把代码写到那里去。它与普通函数相比能提高

效率,因为它不需要中断调用,在编译的时候内联函数可以

直接把代码镶嵌到目标代码中去,省去了函数调用的开销,

但是它是以代码膨胀为代码的(以增加空间消耗为代价)

1

B.Whenwouldyouuseinlinefunction?

(1)一个函数不断的被重复调用

(2)函数只有简单的几行,且函数内部包含:

for,while,switch语句。

C.Pleasewritesamplecode.

voidFoo(intx,inty);

inlinevoidFoo(intx,inty){...}

需要注意的是:关键字inline必须与函数定义体放在一

起才能使函数构成内联,仅将inline放在函数声明前不起任

何作用。

3.Whichofthefollowingarelegal?Forthoseusages

thatareillegal,explainwhy.

constintbuf;

2

不合法。因为定义const变量(常量)时必须进行初始化,

而buf没有初始化。

intcnt=0;

constintsz=cnt;

合法。

cnt++;sz++;

不合法。因为修改了const变量sz的值。

4.PleasepointouttheerrorsinthefollowingC++code.

Andwhy?

switch(ival)

{

3

case1,3,5,7,9:oddcnt++;

break;

case2,4,6,8,10:evencnt++;

break;

}

貌似case不能把所有的情况并列列出来,没见过case1,

3,5,7,9:这种写法,改为case1:oddcnt++;case3:oddcnt++;

C++笔试题目及答案篇2

1.Giventhefollowingbaseandderivedclassdefinitions:

classBase

4

{

public:

foo(int);

protected:

int_bar;

double_foo_bar;

};

classDerived:publicBase

{

public:

5

foo(string);

boolbar(Base*pb);

voidfoobar();

protected:

string_bar;

};

Identifywhatiswrongwitheachofthefollowingcode

fragmentsandhoweachmightbefixed:

(a)Derivedd;d.foo(1024);

错误:

文档评论(0)

1亿VIP精品文档

相关文档