有如下类定义:class Test{public:Test(){ a = 0; c = 0;} // ①int f(int a)const{this->a = a;} // ②static int g(){return a;} // ③void h(intB . {Test::b = b;}; // ④private:int a;static int b;const int c;};int Test::b = 0;在标注号码的行中,能被正确编译的是A . ①B . ②C . ③D . ④

题目

有如下类定义:

class Test

{

public:

Test(){ a = 0; c = 0;} // ①

int f(int a)const{this->a = a;} // ②

static int g(){return a;} // ③

void h(int

B . {Test::b = b;}; // ④

private:

int a;

static int b;

const int c;

};

int Test::b = 0;

在标注号码的行中,能被正确编译的是

A . ①

B . ②

C . ③

D . ④


相似考题
更多“有如下类定义:class Test{public:Test(){ a = 0; c = 0;} // ①int f(int a)const{this-> ”相关问题
  • 第1题:

    已知程序中已经定义了函数test,其原型是int test(int, int, int);,则下列重载形式中正确的是

    A.char test(int,int,int);

    B.double test(int,int,double);

    C.int test(int,int,int=0);

    D.float test(int,int,float=3.5F);


    正确答案:B
    解析:本题考核函数重载。所谓函数重载,是指同一个函数名可以对应着多个函数的实现。每种实现对应着一个函数体,这些函数的名字相同,但是函数的参数的类型不同。这就是说,进行函数重载时,要求同名函数在参数个数上不同,或者参数类型上不同。不可以根据函数返回值类型来重载函数。A选项中定义只是函数的返回类型不同,不符合重载的条件,C选项也不符合。对于:D选项,形参列表中带有默认参数,如果原型的最后一个也带有默认参数,则会造成不知道该调用哪个函数的错误。

  • 第2题:

    有如下类定义: class Test { int x_,y_; public: Test ():a_(0) ,b_(0) {} Test(int a,int b=0) :a_(a),b_(b){} }; 若执行语句 Test x(2) ,y[3],*z[4]; 则Test类的构造函数被调用的次数是( )。

    A.2次

    B.3次

    C.4次

    D.5次


    正确答案:C
    解析:定义对象x时执行一次构造函数(调用Test(int a,int b=0) ),定义对象数组y时,构造函数(注意,该构造函数必须是没有任何参数的)的执行次数与数组中对象的个数相等,即需要执行3次,最后定义一个指向Test对象的指针数组,它并不会创建对象,从而也就不会去执行构造函数,故构造函数一共执行了4次。

  • 第3题:

    如下程序编译时发生错误,错误的原因是show函数实现语句错误,则正确的语句应该为______。

    include<iostream.h>

    class test

    {

    private:

    int hum;

    public:

    test(int);

    void show( );

    };

    test::test(int n){num=n;}

    test::show( ){cout<<num<<endl;}

    void main( )

    {

    test T(10):

    T.show( );

    }


    正确答案:void test::show( ){coutnumendl;}
    void test::show( ){coutnumendl;} 解析:show成员函数的声明和实现不一致,即实现部分应有void修饰符,这样才能编译通过。

  • 第4题:

    有如下类定义: class Test { public: Test{a=0;c=0;}//① int f(im A.const{this->a=a;}//② static int g{return a;f//③ void h(int B.{Test:.b=b;};//④ private: int a; static int b; const int C; }; int Test::b=0: 在标注号码的行中,能被正确编译的是( )。

    A.①

    B.②

    C.③

    D.④


    正确答案:D
    只能通过构造函数的参数初始化列表对常数据成员进行初始化,本题中常数据成员为C。①通过默认构造函数初始化c,所以不正确。常成员函数只能引用本类中数据成员,而不能修改它,所以②不正确。静态成员函数由于没有this指针,所以不能访问本类中的非静态成员,所以③错误。

  • 第5题:

    已知程序中已经定义了函数test,其原型是int test (int,int,int);,则下列重载形式中正确的是( )。

    A.char test(int, int, int);

    B.double test (int,int,double);

    C.int test(int ,int, int=0);

    D.float test(int,int,float=3.5F);


    正确答案:B