C++ 类、对象、模板相关选择题、填空题

一、选择

1、以下程序运行的结果为

template<class T>                      
class Num                                
 {                             
    T x;                         
    pubilc:                          
    Num(){}                                 
    Num(T x){this->x=x;}              
    Num<T>&operator+(const Num<T>&x2)
    {     
        static Num<T>temp;                      
        temp.x=x+x2.x;                           
        return temp;                           
    }                                        
     void disp()                            
    {                    
        cout<<"x="<<x;                         
    }                                      
};                                     
int main()                             
{                                     
Num<int> A(3.8),B(4.8);                
A=A+B;    
A.disp();                                   
return 0;                               
}

 【 答案: A】【这道题主要考察静态成员,运算符重载】
A x=7
B x=8
C x=3
D x=4

2、模板对类型的参数化提供了很好的支持,因此 【 答案: b】【考察类模板的相关定义】

A.类模板的主要作用是生成抽象类

B.类模板实例化时,编辑器将根据给出的模板实参生成一个类

C.在 类模板中的数据成员都具有同样类型

D. 模板中的成员函数都没有返回值

3、有如下模板:

template<typename T,typename U>
T fun(U u){return u;}
下列对模板函数fun的调用中错误的是 【 答案: D】 。【考察模板函数定义后如何去调用】

A. a<int,int>(97);    

B. a<char,int>(97);     

C. a<double,int>(97);      

D. a(97);

4、关于关键字class和typename,下列表述中正确的是 【 答案: D】【考察关键字class和typename的作用以及区别】

A.程序中的typename都可以替换为class

B.程序中的class都可以替换为typename

C.在模板形参表中只能用typename来声明参数的类型

D.在模板形参表中只能用class或typename来声明参数的类型

5、运行下列程序的结果为 【 答案: B】。【考察模板函数定义、参数以及调用】

#include <iostream.h>
template <class T>
void swap(T &a, T &b)
{
    T temp;
    temp = a;
    a = b;
    b = temp;
}
void swap(int &a, int &b)
{
   int temp;
   temp = a;
   a = b;
   b = temp;
   cout<<"swap two int!"<<endl;
}
int main(void)
 {
   int i=1, j=2;
   double x=1.1, y=2.2;
   char a='x', b='z';
   swap(i,j);                                 
   swap(a,b);
   swap(x,y);
   cout<<i<<","<<j<<endl;
   cout<<a<<","<<b<<endl;
   cout<<x<<","<<y<<endl;
 }
A                        B                           C                              D

2,1                  swap two int           swap tow int          swap tow int

z,x                   2,1                       swap tow int          swap tow int

2.2,1.1             z,x                       2,1                       swap tow int

                          2.2,1.1                 z,x                        2,1     

                                                        2.2,1.1                  z,x

6、有以下程序:

#include<iostream>
using namespace std;
class A
{
public:
static int a;
void init() {a=1;}
A(int a=2)
{
init ();
a++;
}
};
int A::a=0;
A obj;
int main()
{
cout<<obj.a;
return 0;
}
运行时输出的结果是(  )。 【 答案: B】【考察静态成员】
A 0
B 1
C 2
D 3

7、下列程序的执行结果是

#include<iostream>
Using namespace std;
Class Test{
Public:
  Test()  {n+=2;}
  ~Test() {n-=3;}
  Static int getNum(){return n;}
Private:
 Static int n;
};
Int Test::n=1;
Int main()
{
  Test *p=new Test;
    Delete p;
    Cout<<”n=”<<Test::getNum()<<endl;
    Return 0;
}
}
 【 答案: A】【考察静态成员这个知识点】
A n=0
B n=1
C n=2
D n=3

8、静态成员函数没有 【 答案: B】【考察静态成员函数相关性质】
A 返回值
B this指针
C 指针参数
D 返回类型

9、由于常对象不能被更更新,因此(   ) 【 答案: A】【考察常对象相关性质】
A 通过常对象只能调用它的常成员函数
B 通过常对象只能调用静态成员函数
C 常对象的成员都是常成员
D 通过常对象可以调用任何不改变对象值的成员函数

10、以下类模板,正确的实例化方式为:      

class Tadd                              
{                                     
    T x,y;                                   
      pubilc:                            
    Tadd(T a,T b):x(a),y(b){ }          
    int add() { return x+y;}                
};
 【 答案: D】【考察模板的实例化】
A Tadd<char>K;
B Tadd<double,double>K(3.4,4.8);
C Tadd<char*>K('3','4');
D Tadd<float>K('3','4');

11、以下类模板:

 template<class T1,class T2=int, int num=10> class Tclass{…};
正确的实例化方式为() 【 答案: A】【考察模板实例化】
A Tclass<char&,char>C1;
B class<char*,  char>C1;
C Tclass<>C1;
D Tclass<char,100,int>

12、关于在调用模板函数时模板实参的使用,下列描述正确的是 【 答案: D】 。【考察模板中虚拟类型参数所对应的模板实参以及常规参数所对应的模板实参】

A 对于虚拟类型参数所对应的模板实参,如果能从模板函数的实参中获得相同的信息,则都可以省略。

B 对于虚拟类型参数所对应的模板实参,如果它们是参数表中的最后的若干个参数,则都可以省略。

C 对于虚拟类型参数所对应的模板实参,若能够省略则必须省略。

D 对于常规参数所对应的模板实参,任何情况下都不能省略。

13、设有函数模板

template <class T>
T max(T a,T b)
{
       return  (a>b)?a:b;
}

则下列语句中对该函数模板的错误使用是 【 答案: C】。【考察模板函数中实参和形参】

A max(1,2)        B max(2.3,4.5)        C max(1,2.3)        D max(‘a’,’b’)

二、填空

1、下面是3个数字求和的类模板程序。请将该定义补充完整。

template <class T,①【 答案: int size=3】>
class sum
{
    T temp,m[size];
public:
    sum(T a,T b,T c){m[0]=a;m[1]=b;m[2]=c;}
    T s(){return ② 【  正确答案: m[0]+m[1]+m[2]】;}  //计算三个数的和
};

2、有如下程序段,错误的语句是 【 答案:  】,应改正为【 答案: A<double> obj(1.1,2.2);】。

template<class T> //①
class A
{
    T x,y;//②
public:
    A(T i,T j):x(i),y(j){}//③
};
A(double) obj(1.1,2.2);// ④

3、

下面是一个函数模板,用于实现从3个整数或浮点数中找出最大值。请将函数模板的定义补充完整。

template<class T>
T max(①【  答案: T a,T b】 ,T c)
{
    T x;
    x=(a>b)?(a):(b);
    ② 【  答案: return (x>c)?(x):(c)】 ;
}

4、

下面的程序输出结果是【 答案: 10】 

#include<iostream.h>
template<class T>
T sum(T *x,int size=0)
{
    T a=0;
    for(int i=0;i<size;i++)
       a+=x[i];
    return a;
}
void main()
{
    int array[]={1,2,3,4};
    int arraysum=sum(array,4);
    cout<<arraysum<<endl;
}

5、

运行下列程序的输出结果为【 答案: 21】。

#include<iostream.h>
template <class T>
class Tstack
{
  enum {size=1000};
  T stack[size];
  int top;
public:
  Tstack( ):top(0){ }
  bool push(const T &i){
      if(top<size)
        {
           stack[top++]=i;
         return true;
       }
    else
       return false;
    }
  bool pop(T &value){
      if(top!=0)
        {
           value=stack[--top] ;
         return true;
       }
    else
       return false;
  }
};
void main()
{
    Tstack<int> a;
    int x;
    a.push(1);
    a.push(2);
    a.pop(x);
    cout<<x;
    a.pop(x);
    cout<<x<<endl;
}

6、

有如下函数模板定义,错误的语句是【 答案:  】 ,应该正为【 答案: template <class T> 或 去掉分号 或 template <class T> 去掉分号 或 template <class T>; 去掉分号】 。

template <class T>;//①
T fun(T x)//②
{
    return x;//③
}

7、模板是实现类属机制的一种工具,其功能非常强大,它既允许用户构造类属函数,即 【 答案: 函数模板】;也允许用户构造类属类,即【 答案: 类模板】。

猜你喜欢

转载自blog.csdn.net/weixin_74287172/article/details/134493928