加入收藏
设为首页
联系我们
用户名: 密码:
当前位置:$nav$ 站内搜索:

计算机等级考试二级C++练习题(1)及解答

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

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

(1) template <class Type> class Container1;

   template <class Type, int size> class Container1;

(2) template<class Type, int *ptr> class Container2;

(3) template<typename myT, class myT> class Container3;

(4) template <class T,U,class V> class Container2;

(5) template <class Type, int val = 0> class Container5;

 

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

1               template <class elemType> class ListItem;

2                

3               template<class elemType> class List

4               {

5                 public:

6                   List<elemType>(): front(NULL), end(NULL){}

7                   List<elemType> (const List<elemType> &);

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<int>是一个整型的数组类。可以通过’[ ]’运算符来访问数组中的每个元素。还有一个模板函数inv(),其函数原型为:

          template <class T> val_ary<T> inv(const val_ary<T> & x);

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

       4   -13   -5    7    -1

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

       -4   13   5   -7     1

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

 

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

#include <iostream.h>                

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

 

#define A_SIZE  10              

typedef val_ary<int>  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<int>的实例

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

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

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

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

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

template<class T> val_ary<T> inv(const val_ary<T>& x)

{

       for(int i=0; i<x.size(); i++)         x=-x;

       return x;

}

 

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

 

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

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

#include <iostream.h>

 

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 "<<pSub->GetObj_n()<<" objects"<<endl;

       delete []pSub;

       cout<<"There are "<<Cla_Sub::GetObj_n()<<" object"<<endl;

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

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

(1) 非法的,两次声明不一样

(2) 合法的

(3) 非法的,两个类型参数的名字不能相同

(4) 非法的,参数U没有类型说明

(5) 合法的

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

1               template <class elemType> class ListItem;

2                

3               template<class elemType> class List

4               {

5                 public:

6                   List (): front(NULL), end(NULL){}           //有错

7                   List (const List &);                           //有错

8                   ~List();

9                   void insert(ListItem<elemType> *ptr, elemType  value);//有错

10               int remove(elemType  value);      //有错

11               int size( ) { return  size; }

12             private:

13               ListItem<elemType> *front;                   //有错

14              ListItem<elemType> *end;                      //有错,以上错均已改正

15           };

 

二,[理解问答题]

问题1. 答:程序的输出结果为:

Size of val_array = 10

The values of val_array before calling inv():

0     1     2     3     4     5     6     7     8     9

The result of val_array after calling inv():

0    -1    -2    -3    -4    -5    -6    -7    -8    -9

 

问题2.答:

题号
 A
 B
 C
 D
 E
 
对/错
 对
 错
 对
 错
 对
 

 

问题3.答:该函数实现有以下错误:

(1)   函数的参数x为const参数,不能在函数体中被改变

(2)   在函数中应该创建一个临时对象,这个对象的内容由参数x的内容运算而来

(3)   函数返回的应是临时对象,而不是参数

 

正确的函数实现为:

template<class T> val_ary<T> inv(const val_ary<T>& x)

{

       INTARY ret_array(x);               //利用拷贝构造函数构造临时对象

       for (int i = 0; i < x.size(); i++) ret_array *= -1;         //符号取反

       return ret_array; //返回临时对象

}

 

或者

 

template<class T> val_ary<T> inv(const val_ary<T>& x)

{

       INTARY ret_array(x.size());         //构造一个与x长度相同的对象

       for (int i = 0; i < x.size(); i++) ret_array = x * (-1);//符号取反

       return ret_array;           //返回临时对象

}

 

问题4.答:重载了取数组下标的运算符’[]’

 

三,[理解问答题]

答:输出结果为:

class Cla_ Base: Hello!

class Cla_ Sub: 2000                     //动态联编和函数重载的结果应访问Cla_Sub

Disply in class Cla_Base without parameter!

class Cla_ Sub: Hi!

Disply in class Cla_Sub without parameter!

There are 6 objects                       // Sub1, Cla_Sub[5]共建6个对象

There are 1 objects                       // 还有一个对象Sub1

 发表评论:    匿名发表                 查看评论

 


地址:沈阳市大东区小北关街31号604室
电话:024-28510618   024-82890666
网址:  邮箱:tian_yu@sina.com
版权归北大创想远程教育网所有 违者必究