C++ 类的定义相关选择题及其答案

编程入门 行业动态 更新时间:2024-10-28 18:32:39

C++ 类的定义相关<a href=https://www.elefans.com/category/jswz/34/1770106.html style=选择题及其答案"/>

C++ 类的定义相关选择题及其答案

1、有以下程序:

#include<iostream>
using namespace std;
class A{
public:
A(){cout<<"A";}
};
class B{public:B(){cout<<"B";}};
class C:public A{
B b;
public:
C(){cout<<"C";}
};
int main(){ C obj;  return 0;}

将会输出()【 答案: D】
A CBA
B BAC
C ACB
D ABC

2、下列( )不是构造函数的特征 【 答案: D】
A 构造函数的函数名和类名相同
B 构造函数可以重载
C 构造函数可以设置默认参数
D 构造函数必须指定类型说明

3、有关构造函数的说法不正确的是( ) 【 答案: D】
A 构造函数的名字和类的名字一样
B 构造函数在声明类变量时自动执行
C 构造函数无任何函数类型
D 构造函数有且只有一个

4、类的构造函数被自动调用执行的情况是在定义该类的( )时 【 答案: C】
A 成员函数
B 数据成员
C 对象
D 友元函数

5、有如下类声明:

class base{int x;
public:base(int n){ x=n;}
};
class derived: public base{int y;
public:derived(int a,int b);
};

下列对构造函数derived的定义,正确的是 【 答案: A】。

(a) derived::derived(int a,int b):base(a),y(b){}

(b) derived::derived(int a,int b):x(a),y(b){}

(c) derived::derived(int a,int b):base(a),derived(b){}

(d) derived::derived(int a,int b):x(a),derived(b){}

6、有以下程序:

#include<iostream>
using namespace std;
class BASE{
public:
~BASE(){cout<<"BASE";}
};
class DERIVED:public BASE{
public:
~DERIVED(){cout<<"DERIVED";}
};
int main(){DERIVED x; return 0;}

执行程序的输出结果是() 【 答案: D】

A BASE
B DERIVED
C BASEDERIVED
D DERIVEDBASE

7、( )是析构函数的特征 【 答案: A】
A 一个类中只能定义一个析构函数
B 析构函数名与类名没任何关系
C 析构函数的定义只能在类体内
D 析构函数可以有一个或多个参数

8、对于任意一个类,析构函数的个数最多为( ) 【 答案: B】
A 0
B 1
C 2
D 3

9、在下列函数原型中,可以作为类AA构造函数的是 【 答案: D】
A void AA(int)
B int AA()
C AA(int)const
D AA(int)

10、、

class Point{int x_,y_;public:Point():x_(0),y_(0){}Point(int x,int y=0):x_(x),y_(y){}
};

若执行语句:

Point a(2),b[3],*c = new Point(3);

则Point类的构造函数被调用的次数是() 【 答案: D】
A 两次
B 三次
C 四次
D 五次

11、在公有派生的情况下,派生类中定义的成员函数只能访问原基类的 【 答案: C】
A 公有成员和私有成员
B 私有成员和保护成员
C 公有成员和保护成员
D 私有成员、保护成员和公有成员

12、下面叙述不正确的是 【 答案: A】
A 基类的保护成员在派生类中仍然是保护的
B 基类的保护成员在公有派生类中仍然是保护的
C 基类的保护成员在私有派生中仍然是私有的
D 对基类成员的访问必须是无二义性的

13、下面叙述不正确的是 【 答案: D】
A 为了充分利用现有类,派生类一般使用公有派生
B 对基类成员的访问必须是无二义性的
C 赋值兼容规则也适用于多重继承的场合
D 基类的公有成员在派生类中仍然是公有的

14、在下面的类定义中,错误的语句是( )

class sample
{public:sample( int val);  //①
~  sample();      //②
private:int a=2. 5;       //③
sample();        //④

【 答案: C】
A ①②③④
B ②
C ③④
D ①②③

15、有以下类定义:

class SAMPLE
{int n;
Public:SAMPLE(int i=0):n(i){}void setValue(int n0);
};

下列关于setValue成员函数的实现正确的是()。 【 答案: B】
A SAMEPLE::setValue(int n0){n=n0;}
B void SAMEPLE::setValue(int n0){n=n0;}
C void setValue(int n0){n=n0;}
D setValue(int n0){n=n0;}

16、

#include<iostream>
using namespace std;
class Base{
protected:
Base(){cout<<'A';}
Base(char c){cout<<c;}
};class Derived:public Base{
public:
Derived(char c){cout<<c;}
};
int main()
{
Derived d1('B');
return 0;
}

执行这个程序屏幕上将显示 【 答案: C】
A B
B BA
C AB
D BB

17、

#include<iostream>
using namespace std;
class Base
{
private:void fun1()const{cout<<"fun1";}
protected:void fun2()const{cout<<"fun2";}
public: void fun3()const{cout<<"fun3";}
} ;
class Derived:protected Base
{
publc:void fun4()const{cout<<"fun4";}
};
int main()
{Derived obj;obj.fun1();     //①obj.fun2();     //②obj.fun3();     //③obj.fun4();     //④return 0;
}

其中有语法错误的语句是(  )。 【 答案: B】
A ①②③④
B ①②③
C ②③④
D ①④

18、有以下类定义:

class XA
{
int x;
public:
XA(int n){x=n;}
};
class XB:public:XA
{
int y;
public:
XB{int a,int b};
};

【 答案: B】
A XB::XB(int a,int b):x(a),y(b){}
B XB::XB(int a,int b):XA(a),y(b){}
C XB::XB(int a,int b):x(a),XB(b){}
D XB::XB(int a,int b):XA(a),XB(b){}

19、在派生类构造函数的初始化列表中不能包含() 【 答案: C】
A 基类的构造函数
B 基类对象成员的初始化
C 派生类对象成员的初始化
D 派生类中新增数据成员的初始化

20、在一个派生类对象结束其生命周期时() 【 答案: A】
A 先调用派生类的析构函数,后调用基类的析构函数
B 先调用基类的析构函数,后调用派生类的析构函数
C 如果基数没有定义析构函数,则只调用派生类的析构函数
D 如果派生类没有定义析构函数,则只调用基类的析构函数

21、有以下程序:

#inlcude<iostream>
using namespace std;
class MyClass
{public:MyClass(){cout<<“A;} MyClass(char c){cout<<c;}~MyClass(){cout<<”B”;}
}
Int main()
{MyClass p1,*p2;P2=new MyClass(‘X’);delete p2;return 0;
}

执行这个程序,计算机屏幕上将显示输出(  )。 【 答案: D】
A ABX
B ABXB
C AXB
D AXBB

22、若有以下类声明:

class MyClass{public:MyClass(){  cout<<1;  }};

执行下列语句:MyClass a,b[2],*p;程序的输出结果是()。 【 答案: B】
A 11
B 111
C 1111
D 11111

23、有以下类定义:

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;

在标注号码的行中,能被正确编译的是()。 【 答案: D】
A ①
B ②
C ③
D ④

更多推荐

C++ 类的定义相关选择题及其答案

本文发布于:2023-11-16 12:46:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621157.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:选择题   定义   答案

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!