C++练习题及解答----练习题(二)

文章作者 100test 发表时间 2007:04:06 21:28:26
来源 100Test.Com百考试题网


一,[理解问答题] 请回答下面有模板的定义问题:

1.下列模板的定义是否合法的?若为非法的,请简单扼要说明理由。

(1) template class Container1.

template class Container1.

(2) template class Container2.

(3) template class Container3.

(4) template class Container2.

(5) template class Container5.

2关于类List的如下定义中有若干错误,请指出其所在行号并改正 (但不要求补充实现成员函数)

1 template class ListItem.

2

3 template class List

4 {

5 public:

6 List(): front(NULL), end(NULL){}

7 List (const List &.).

8 ~List().

9 void insert(ListItem *ptr, elemType value).

10 int remove(elemType value).

11 int size( ) { return _size. }

12 private:

13 ListItem *front.

14 ListItem *end.

15 }.

二、[理解问答题] val_ary是一个类模板,类模板的每个实例类实现了某个具体的数据类型的数组,如val_ary是一个整型的数组类。可以通过’[ ]’运算符来访问数组中的每个元素。还有一个模板函数inv(),其函数原型为:

template val_ary inv(const val_ary &. x).

该函数的作用是将作为参数的数组x的每个元素的符号取反,并返回得到的新的数组。如原来的数组为:

4 -13 -5 7 -1

将这个数组作为参数传递给函数inv后,函数返回的数组就变成:

4 13 5 -7 1

要求:阅读下列程序,回答后面的问题。

/*********************************************************** *********/

#include

#include //该头文件中定义了模板类val_ary和模板函数inv()

#define A_SIZE 10

typedef val_ary INTARY.

void main()

{

INTARY iarray(A_SIZE). //定义一长度为A_SIZE的数组对象

for (int i = 0. i < A_SIZE. i ) iarray =i.//赋初始值

cout << "Size of iarray = " << iarray.size() << endl.

cout << "The values of iarray before calling inv():\n".

for (i = 0. i < A_SIZE. i ) cout << iarray << " ".

cout << endl.

INTARY inv_array = inv(iarray).

cout << "The result of iarray after calling inv():\n".

for (i = 0. i < A_SIZE. i ) cout << inv_array << " ".

cout << endl.

}

/*********************************************************** **********/

问题1,写出程序的输出结果

问题2,关于程序中的语句: INTARY iarray(A_SIZE).

下列说法哪些是正确的,哪些是错误的?在下表相应的位置写上“对”或“错”

题号

A

B

C

D

E

/

A 该语句定义了一个对象irray,这个对象是类val_ary的实例

B 该语句说明了一个函数原型,函数的名字为iarray,参数为A_SIZE,函数的返回值类型为INTARY

C 模板类val_ary一定有一个只带一个参数的构造函数

D 模板类val_ary一定有一个只带两个参数的构造函数

E A_SIZE将作为参数传递给val_ary的构造函数,初始化val_ary对象

问题3下面是模板函数inv()的实现。这个实现中有错误,指出错误并写出正确的实现。

template val_ary inv(const val_ary&. x)

{

for(int i=0. i x=-x.

return x.

}

问题4,从上面的程序中,你可以推断出,val_ary模板类中至少重载了哪个或哪些C 的运算符?

三,[理解问答题]阅读下面的程序,写出程序运行的结果,并给以简单扼要的说明。

//********************************************************** *****/

#include

class Cla_Base {

private:

//...其他成员

public:

virtual void fun_Disply(long num) {cout << "class Cla_Base: " << num << endl. }

void fun_Disply (char * str) { cout << "class Cla_Base: " << str << endl . }

void fun_Disply () { cout << "Disply in class Cla_Base without parameter!\n" . }

}.

class Cla_Sub: public Cla_Base {

private:

static int obj_n.

//...其他成员

public:

Cla_Sub() { obj_n . }

~Cla_Sub() { obj_n --. }

static int GetObj_n() { return obj_n. }.

void fun_Disply (long num) { cout << "class Cla_Sub: " << num << endl . }

void fun_Disply (char * str) { cout << "class Cla_Sub: " << str << endl . }

void fun_Disply () { cout << "Disply in class Cla_Sub without parameter!\n". }

}.

int Cla_Sub::obj_n = 0.

void main(int argc, char* argv[])

{

Cla_Base *pBase.

Cla_Sub Sub1,*pSub = new Cla_Sub[5].

pBase = &.Sub1.

pBase-> fun_Disply ("Hello!").

pBase-> fun_Disply (2000).

pBase-> fun_Disply ().

pSub-> fun_Disply ("Hi!").

pSub-> fun_Disply ().

cout<<"There are "<GetObj_n()<<" objects"<

0delete []pSub.

cout<<"There are "<

}



相关文章


06年9月等级考试二级C 笔试模拟题(3)
06年9月等级考试二级C 笔试模拟题(2)
06年9月等级考试二级C 笔试模拟题(1)
百度之星程序设计大赛总决赛题目及解答
C++练习题及解答----练习题(二)
C++练习题及解答----练习题(二)参考答案
C++练习题及解答----练习题(一)参考答案
精选二级C 练习试题
C++练习题及解答----练习题(一)
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛