};
intmain(){
testA(3);
return0;
}运行时输出的结果是3destructor
21、下列运算符重载函数中,属于类Value的成员函数的是(ACD)。
A)Valueoperator+(Value);
B)Valueoperator-(Value,Value);
C)Valueoperator*(int);
D)Valueoperator/(Value);
22、下列函数模板的定义中,不合法的是CD
A)templateTabs(Tx){returnx<0?
-x:
x;}
B)templateTabs(Tx){returnx<0?
-x:
x;}
C)templateTabs(Tx){returnx<0?
-x:
x;}
D)templateTabs(Tx){returmx<0?
-x:
x;}
23、有如下程序:
#include
usingnamespacestd;
classpoint{
public:
staticintnumber;
public:
point(){number++;}
~point(){number--;}
};
intpoint:
:
number=0;
voidmain(){
point*ptr;
pointA,B;
{
point*ptr_point=newpoint[3];
ptr=ptr_point;
}
pointC;
cout<:
number<delete[]ptr;
}运行时输出的结果是6
24、下列函数原型声明中,正确的是ABD
A)intfnction(intm,intn);
B)intfunction(int,int);
C)intfunction(intm=3,intn);
D)intfunction(int&m,int&n);
25、若有函数模板mySwap和一些变量定义如下:
template
voidmySwap(TX,TY);
doubled1,d2;inti1,i2;
下列对mySwap的调用中,正确的是ABD
A)mySwap(i1,i2)B)mySwap(d1,d2)
C)mySwap(i1,d1)D)mySwap(i2,d2)
26、下列程序的输出是3,请填充程序中的空缺,使该行形成一个运算符重载函数的定义。
#include
usingnamespacestd;
classMyNumber{
intn;
public:
MyNumber(intk):
n(k){}
----operator----int()const{returnn;}
};
intmain(){
MyNumbernum1(3);
cout<return0;
}
27、有如下程序:
//没有错误
#include
#include
usingstd:
:
cout;
classPoint{
public:
frienddoubledistance(constPoint&p);//p距原点的距离
Point(intxx=0,intyy=0):
x(xx),Y(yy){}//①改为point(intxx,intyy):
x(XX),y(YY)
private:
intx,Y;
};
doubledistance(constPoint&p){//②
returnsqrt(p.x*p.x+p.Y*p.Y);
}
intmain(){
Pointp1(3,4);
cout<return0;
}
28、若已定义了类Vehicle,则下列派生类定义中,正确的是acd
A)classCar:
Vehicle{/*类体略*/},
B)classCar:
publicCar{/*类体略*/};
C)classCar:
publicVehicle{/*类体略*/};
D)classCar:
virtualpublicVehicle{/*类体略*/};
29、使用VC6打开工程test4_1,此工程包含一个源程序文件test4_1.cpp,但该程序运行有问题,请改正程序中的错误,使程序的输出结果如下:
Constructor2
Constructor1
i=0
i=10
Destructor
源程序文件test4_1.cpp清单如下:
#include<iostream.h>
classCSample{
inti;
public;
CSample(){cout<<"Constructorl"<<endl;i=0;}
CSample(intval){cout<<"Constructor2"<<endl;i=val;}
~CSample(){cout<<"Destructor"<<endl;}
voiddisp();
};
/**********found
(1)********/
voiddisp(){//voidCSample:
:
disp()声明错了
cout<<"i=”<<i<<endl;
}
voidmain(){
CSample*a,b(10);
/**********found
(2)*********/指向不明CSamplec;a=&c;a->disp();
a->disp();
/**********found(3)*********/
b->disp();普通对象b.disp();
}
30、请将下列模板类Data补充完整。
template
classData
{
public:
voidput(Tv)
{val=v;}
_T__get()//返回数据成员val的值,返回类型不加转换
{returnval;}
private:
Tval;
};
31、根据程序中的注释将下列缺失部分补充完整。
#include"iostream.h"
classPoint{
intx,y;//点的x和y坐标
public:
Point(intxx=0,intyy=0):
x(xx),y(yy){}
};
classCircle{
Pointcenter;//圆心位置
Intradius;//半径
public:
//利用cx和cy分别初始化圆心的x和y坐标
----Circle(intcx,intcy,intr):
center(cx,cy)-----,radius(r){}
voidarea(){cout<<3.14159*radius*radius<};
32、下列程序的输出结果是__`___042_____________。
#include
usingnamespacestd;
classTest{
public:
Test(){cnt++;}
~Test(){cnt--;}
staticintCount(){returncnt;}
private:
staticintcnt;
};
intTest:
:
cnt=0;
intmain(){
cout<:
Count()<<'';
Testtl,t2;
Test*pT3=newTest;
Test*pT4=newTest;
cout<:
Count()<<'';
deletepT4;
deletepT3;
cout<:
Count()<return0;
}
33、使用VC6打开工程test4_2。
此工程包含一个源程序文件test4_2.cpp,其对一个学校的教师和考生情况进行了描述。
由于教师和考生之间有相同的信息,如姓名、年龄等,所以可抽象出一个基类person,考生类student和教师类teacher作为person类的派生类。
请按要求完成下列操作,将程序中的类定义补充完整。
(1)定义基类person的私有数据成员name和age,分别用于表示姓名和年龄,name为字符指针,age为血型的数据。
请在注释“//**1**”之后添加适当的语句。
(2分)
(2)完成基类person的重载构造函数person(char*n,inta)的定义,把数据成员name,age分别初始化为参数n,a的值,请在注释“//**2**”之后添加适当的语句。
(2分)
(3)根据主函数main中对s1的声明,定义派生类student的构造函数。
在注释“//**3**”之后添加适当的语句。
(3分)
(4)完成派生类teacher成员函数voidsetinf(ehar*n,inta,char*d,char*1)的定义,其功能为记录教师的姓名、年龄、院,系与职称信息。
请在注释“//**4**”之后添加适当的语句。
(2分)
输出结果如下:
刘雷(45)destructor蒋军(23)destructor
#include<iostream.h>
#include
classperson{
//*********************1*******************