期末考试VC++练习题带答案Word文档格式.docx
《期末考试VC++练习题带答案Word文档格式.docx》由会员分享,可在线阅读,更多相关《期末考试VC++练习题带答案Word文档格式.docx(13页珍藏版)》请在冰豆网上搜索。
A)3B)4C)5D)6
11.下列关于this指针的说法正确的是(B)
A)this指针存在于每个函数之中
B)在类的非静态函数中this指针指向调用该函数的对象
C)this指针是指向虚函数表的指针
D)this指针是指向类的函数成员的指针
12.在下列关于C++函数的叙述中,正确的是(C)
A)每个函数至少要有一个参数B)每个函数都必须返回一个值
C)函数在被调用之前必须先声明D)函数不能自己调用自己
13.下列运算符中,不能重载的是(C)
A)&
&
B)!
=C).D)->
14.下面程序的输出结果是(B)
#include<
iostream>
usingnamespacestd;
inti=0;
intfun(intn)
{
staticinta=2;
a++;
returna+n;
}
voidmain()
intk=5;
{
inti=2;
k+=fun(i);
}
k+=fun(i);
cout<
<
k;
A)13B)14C)15D)16
15.下面的程序段的运行结果为(D)
charstr[]="
job"
*p=str;
cout<
*(p+2)<
endl;
A)98B)无输出结果C)字符’b’的地址D)字符’b’
16.下面程序的输出结果是(C)
classA
public:
A(inti){x=i;
voiddispa(){cout<
x<
“,”;
private:
intx;
};
classB:
publicA
B(inti):
A(i+10){x=i;
voiddispb(){dispa();
Bb
(2);
b.dispb();
A)10,2B)12,10C)12,2D)2,2
17.下面程序的输出结果是(C)
classBase
Base(inti){cout<
i;
~Base(){}
classBase1:
virtualpublicBase
Base1(inti,intj=0):
Base(j){cout<
~Base1(){}
classBase2:
Base2(inti,intj=0):
~Base2(){}
classDerived:
publicBase2,publicBase1
Derived(inta,intb,intc,intd):
mem1(a),mem2(b),Base1(c),
Base2(d),Base(a)
{cout<
b;
private:
Base2mem2;
Base1mem1;
voidmain(){DerivedobjD(1,2,3,4);
A)134122B)123412C)14302012D)143212
18.下面程序的输出结果是(C)
classBase
virtualvoidf(){cout<
“f0+”;
voidg(){cout<
“g0+”;
publicBase
voidf(){cout<
“f+”;
“g+”;
voidmain(){Derivedd;
Base*p=&
d;
p->
f();
g();
A)f+g+B)f0+g+C)f+g0+D)f0+g0+
19.下面程序的输出结果是(C)
classSample
friendlongfun(Samples)
if(s.x<
2)return1;
returns.x*fun(Sample(s.x-1));
Sample(longa){x=a;
longx;
voidmain()
intsum=0;
for(inti=0;
i<
4;
i++)
sum+=fun(Sample(i));
sum;
}A)12B)16C)10D)34
20.以下程序的输出结果是:
(D)
intfun(char*s)
{char*p=s;
while(*p!
='
\0'
)p++;
return(p-s);
voidmain(){
cout<
fun("
abc"
)<
endl;
A.0B.1C.2D.3
21.有如下程序段:
inti=1;
while
(1)
i++;
if(i==10)break;
if(i%2==0)cout<
’*’;
执行这个程序段输出字符*的个数是(C)
A.10B.3C.4D.5
22.下列关于虚基类的描述中,错误的是(C)
A.使用虚基类可以消除由多继承产生的二义性
B.构造派生类对象时,虚基类的构造函数只被调用一次
C.声明classB:
virtualpublicA说明类B为虚基类
D.建立派生类对象时,首先调用虚基类的构造函数
23.有下类定义
ClassA{
Char*a;
Public:
A():
a(0){}
A(char*aa){//把aa所指字符串拷贝到a所指向的存储空间
A=___________________;
Strcpy(a,aa);
~A(){delete[]a;
正确的选择是(A)
A.newchar[strlen(aa)+1]B.char[strlen(aa)+1]
C.char[strlen(aa)]D.newchar[sizeof(aa)-1]
24.假定AA为一个类,a为该类私有的数据成员,GetValue()为该类公有函数成员,它返回a的值,x为该类的一个对象,则访问x对象中数据成员a的格式为(X)。
A)x.aB)x.a()C)x->
GetValue()D)x.GetValue()
25.假定AA为一个类,inta()为该类的一个成员函数,若该成员函数在类定义体外定义,则函数头为(A)。
A)intAA:
:
a()B)intAA:
a()
C)AA:
a()D)AA:
inta()
26.有如下程序:
#include<
classTest{
public:
Test(){}
~Test(){cout<
'
#'
;
intmain(){
Testtemp[2],*pTemp[2];
return0;
执行这个程序输出星号(#)的个数为(B)。
A)1B)2C)3D)4
27.有如下程序:
classMyClass{
MyClass(inti=0){cout<
1;
MyClass(constMyClass&
x){cout<
2;
MyClass&
operator=(constMyClass&
3;
return*this;
}
~MyClass(){cout<
MyClassobj1
(1),obj2
(2),obj3(obj1);
obj1=obj2;
}运行时的输出结果是(A)。
A)1123444B)11114444C)121444D)11314444
28.有如下程序:
#include<
classpoint
staticintnumber;
point(){number++;
~point(){number--;
intpoint:
number=0;
voidmain()
{point*ptr;
pointA,B;
point*ptr_point=newpoint[3];
ptr=ptr_point;
pointC;
delete[]ptr;
point:
number;
程序结果为(A)
A)3B)4C)6D)7
29.友元运算符obj++被C++编译器解释为(D)。
A.operator++(obj)B.operator++(obj,0)
B.obj.operator++()D.obj.operator++(0)
二、程序填空
1.用递归法求从1到n的立方和:
f
(1)=1(n=1)
f(n)=f(n-1)+n3(n>
1)
intf(int);
intn,s;
cout<
"
inputthenumbern:
cin>
>
n;
s=f(n);
Theresultis"
s<
return0;
intf(intn){//递归法求立方和函数
if(n==1)return1;
elsereturn(f(n-1)+n*n*n);
3.请将下列程序补充完整,使得输出结果为“DestructorDerivedDestructorBase”。
virtual~Base(){cout<
“DestructorBase”<
~Derived(){cout<
“DestructorDerived”<
};
voidmain()
{
Base*pBase=newDerived;
deletepBase;
三、阅读程序,写出运行结果
1.
cstring>
classCD
charname[20];
intnumber;
voidInit(char*aa,intbb)
strcpy(name,aa);
number=bb;
char*Getname(){returnname;
intGetnumber(){returnnumber;
voidOutput(){cout<
name<
'
number<
CDdx;
CDdy;
dx.Init("
歌唱祖国"
30);
dy.Init("
义勇军进行曲"
3*dx.Getnumber()+5);
dx.Output();
dy.Output();
歌唱祖国30
义勇军进行曲95
2.#include<
usingnamespacestd;
intf(inta){
return++a;
intg(int&
a){
voidmain(){
intm=0,n=0;
m+=f(g(m));
n+=f(f(n));
m="
m<
n="
n<
3.#include<
classDemo
Demo(){cout<
defaultconstructor\n"
Demo(constDemo&
copyconstructor\n"
DemouserCode(Demob){Democ(b);
returnc;
Demoa,d;
callinguserCode()\n"
d=userCode(a);
执行上面的程序的过程中,构造函数Demo()和Demo(constDemo&
x)被调用的次数分别是多少次?
五、编写程序
1、编写函数计算y=1!
+2!
+3!
...n!
,n作为参数传递,在主函数调用该函数并输出结果。
2、编写函数找出二维数组(5*4)中元素的最大值与最小值,并返回其最大值与最小值。
测试结果。
3、编写一个三角形类,有计算面积,显示面积的功能等,并测试。
要求有参数的构造函数及复制构造函数