自测题8面向对象二资料Word文档格式.docx
- 文档编号:7802875
- 上传时间:2023-05-09
- 格式:DOCX
- 页数:23
- 大小:20.50KB
自测题8面向对象二资料Word文档格式.docx
《自测题8面向对象二资料Word文档格式.docx》由会员分享,可在线阅读,更多相关《自测题8面向对象二资料Word文档格式.docx(23页珍藏版)》请在冰点文库上搜索。
D不用调用析构函数
//答案:
C
重载、多态和虚函数
40、下列运算符中,运算符在C++中不能重载。
A?
:
B+C-D<
=
A
41、下列运算符中,运算符在C++中不能重载。
A&
&
B[]C:
:
Dnew
C
42、如果一个类至少有一个纯虚函数,那么就称该类为。
A抽象类B虚基类C派生类D以上都不是
43、下列描述中,是抽象类的特性。
A可以说明虚函数
B可以进行构造函数重载
C可以定义友元函数
D不能说明其对象
44、关于动态联编的下列描述中,是错误的。
A动态联编是以虚函数为基础的
B动态联编是在运行时确定所调用的函数代码的
C动态联编调用函数操作是指向对象的指针或对象引用
D动态联编是在编译时确定操作函数的
45、关于虚拟成员函数,以下叙述正确的是
A析构函数同构造函数一样,不能是虚拟的
B在一个类中被定义为虚拟的成员函数,其所有的派生类中相同的成员函数都是虚拟的
C带有虚拟函数的类不能直接生成对象,只有其子类才能生成对象
D虚拟成员函数同普通成员函数不同,不能调用父类的虚拟成员函数
46、关于虚函数的描述中,是正确的
A虚函数是一个static类型的成员函数B虚函数是一个非成员函数
C基类中说明了虚函数后,派生类中与其对应的函数可不必说明为虚函数D派生类的虚函数与基类的虚函数具有不同的参数个数和类型
填空题:
C++概述
47、写出除class,private,protected,public之外的任意五个C++新增的关键
字、、、、。
参考〃答案:
catch,,const,delete,friend,new,operator,template,this,throw,try,virtual
48、是指一种事物保留了另一种事物的全部特征,并且有自身的独有特征。
继承
49、C++语言中使用函数重载、模板、等概念来支持多态性。
虚函数
50、在C++中有两种类型的注释,一种是C语言中使用的注释符(/*•/),另一种是。
//
51、在C++类中可以包含、和三种具有不同访问控制权的成员。
public、protected>
private
类和对象
52、类中的数据和成员函数默认类型为。
私有
53、任何类中允许有三种权限的数据成员
publicprivateprotected
54、静态数据成员在定义或说明时前面要加上关键字
static
55、以下程序运行的结果。
#include“iostream.h”
classCSample
{
private:
inti;
public:
CSample();
CSample(intval);
voidDisplay();
~CSample();
};
CSample:
CSample()
cout<
<
“Constructor1”<
endl;
i=0;
}
CSample(intval)
“Constructor2”<
endl;
i=val;
voidCSample:
Display()
”i=”<
i<
~CSample()
“Destructor”<
voidmain()
CSamplea,b(10);
a.Display();
b.Display();
Constructor1
Constructor2i=0i=10DestructorDestructor
56、以下程序执行的结果是
#include<
iostream.h>
classB{
intx,y;
public:
B(){x=y=0;
cout<
B(inti){x=i;
y=0;
B(inti,intj){x=i;
y=j;
~B(){cout<
voidprint(){cout<
voidmain(){
B*ptr;
ptr=newB[3];
ptr[0]=B();
ptr[1]=B(5);
ptr[2]=B(2,3);
for(inti=0;
i<
3;
i++)ptr[i].print();
delete[]ptr;
}//答案:
Constructor1Constructor1
Constructor1”<
“Construc}tor2”<
“Constructor3”
Destructor
a
x=”
x<
y=”<
y<
Constructor2
Constructor3
x=0,y=0
x=5,y=0x=2,y=3DestructorDestructorDestructor
57、以下程序执行的结果是。
classB{
"
Constructor1"
<
Constructor2"
Constructor3"
Destructor"
x="
y="
}};
voidmain(){
ptr=newB[2];
ptr[1]=B(5);
delete[]ptr;
Constructor1
Constructor1Constructor1DestructorConstructor2Destructor
58、以下程序执行结果是。
classA
A(inti=0){m=i;
Constructor"
m<
}voidset(inti){m=i;
voidprint()const{cout<
~A(){cout<
}private:
intm;
voidfun(constA&
c)
c.print();
voidmain()
fun(5);
Constructor5
5
Destructor5
59、以下程序执行结果是。
A(inti,intj){a=i;
b=j;
voidmove(intx,inty){a+=x;
b+=y;
voidshow(){cout<
“(“<
a<
“,”<
b<
“)”<
inta,b;
classB:
privateA
B(inti,intj,intk,intl):
A(i,j)
x=k;
y=l;
voidshow()
{cout<
voidfun(){move(3,5);
voidf1(){A:
show();
intx,y;
Ae(1,2);
e.show();
Bd(3,4,5,6);
d.fun();
d.show();
d.f1();
(1,2)
5,6
(6,9)
d.fun();
d.A:
d.B:
(1,2)
60、以下程序执行结果是
#inelude<
classA{
inta;
A(intaa=0){a=aa;
}
~A(){cout<
"
DestructorA!
a<
endl;
publicA{
intb;
B(intaa=0,intbb=0):
A(aa){b=bb;
~B(){cout<
DestructorB!
b<
voidmain(){
Bx(5),y(6,7);
//后定义的变量将先被释放
B!
7
A!
6
重载多态和虚函数
61、以下程序执行结果是
classbase
{public:
base()
构“造base子对象”f();
}virtualvoidf()
调“用base:
f()”
classderived:
publicbase
derived()
构“造derived对象”f();
}voidf()
调“用derived:
f()
{derivedd;
构造base子对象调用base:
f()构造derived对象调用derived:
62、以下程序执行结果是
base(){
“Constructorbasesubobject”f();
virtualvoidf(){
“Callbase:
f()”}<
derived(){
“Constructorderivedobject<
”f();
voidf(){
“Callderived:
f()”}<
derivedd;
Constructorbasesubobject
Callbase:
f()
Constructorderivedobject
Callderived:
63、以下程序执行结果是
classSample
{private:
intx;
Sample(){x=0;
}voiddisp(){
“x=”<
voidoperator++(){x+=10;
Sampleobj;
obj.disp();
obj++;
执行obj++之后”<
x=0
执行obj++之后
x=10
64、以下程序执行结果
classbase
virtualvoidf1()
{cout<
f1fucofbase"
endl;
virtualvoidf2()
f2fucofbase"
virtualvoidf3()
f3fucofbase"
}voidf4()
f4fucofbase"
classderive:
voidf1()
f1fucofderive"
voidf2(intx)
f2fucofderive"
voidf4()
f4fucofderive"
baseobj1,*p;
deriveobj2;
p=&
obj1;
p->
f1();
f2();
f3();
obj2;
f4();
f1fucofbasef2fucofbasef3fucofbasef1fucofderivef2fucofbasef4fucofbase
解析题:
*65、指出程序中的错误,并予以改正(全部重写)
#include“iostream.h”classpoint
{intx1;
intx2;
point(intx,inty);
//
{pointdata(5,5);
data.x1<
data.x2<
构造函数定义不正确,在main()中对数据成员x1访问不正确改正:
point(intx)
x1=x;
voiddisp()
x1<
pointdata(5);
data.disp();
或
intx1;
intx2;
intdisp()
returnx1;
data.disp()<
66、分析以下程序的执行的顺序以及输出结果
#include"
iostream.h"
classSample
Sample()
构造函数"
voidfn(inti)
staticSamplec;
i="
fn(10);
fn(20);
程序中fn(inti)函数中的staticSamplec;
语句定义的是一个静态对象,仅在函数第一次进入时创建该对象,以后在进入时,c对象便始终存在,fn(inti)函数不再为其创建,所以程序执
行结果如下:
构造函数
i=10
i=20
67、给出程序执行结果#include<
classMyDataType{protected:
intm_nData;
MyDataType(){
m_nData=-1;
ConstructorMyDataType"
MyDataType&
operator=(intn){m_nData=n;
return*this;
operator=(MyDataType&
obj){m_nData=obj.m_nData;
operator+(MyDataType&
obj2){staticMyDataTypetempData;
tempData=m_nData+obj2.m_nData;
returntempData;
operatorint(){returnm_nData;
}//定义一个从MyDataType类型到int类型的转换
MyDataTypeObj1,Obj2;
Obj1=10;
Obj2=Obj1;
MyDataTypeObj3=Obj1+Obj2;
intnValue=(int)Obj3;
nValue="
nValue<
ConstructorMyDataType
ConstructorMyDataTypenValue=20
68、指出程序中的错误,改正并简述原因#include<
classBase
voidSetX(inti){x=i;
}intGetX(){returnx;
intx;
classDerived:
publicBase
voidSetY(inti){y=i;
}intGetY(){returny;
inty;
Base*p;
Baseb_ob;
Derivedd_ob;
p=&
b_ob;
p->
SetX(11);
baseobjectx:
GetX()<
d_ob;
SetX(55);
SetY(99);
derivedobjecty:
GetY()<
SetY(99)是错误的,改为d_ob.SetY(99)基类指针虽然可以指向派生类的对象,但是只可以访问派生类中从该基类继承下来的的那些成员(如GetX())。
基类指针指向的对象只认识基类,而不知道关于派生类的成员信息。
69、分析程序给出运行结果
classBase
virtualvoidshow()
{cout<
Parentclass.\n"
;
classFirst:
FirstDerivedclass.\n"
classSecond:
publicBase
voidshow(){cout<
SecondDerivedclass.\n"
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 自测 面向 对象 资料