};
voidmain(){Derivedobj;}
Whichofthefollowingiscorrectoutput?
A.BaseConstructorB.DerivedConstructor
DerivedConstructorBaseConstructor
BaseDestructorBaseDestructor
DerivedDestructorDerivedDestructor
C.BaseConstructorD.DerivedConstructor
DerivedConstructorBaseConstructor
DerivedDestructorDerivedDestructor
BaseDestructorBaseDestructor
16.Suppose:
classBase{
public:
virtualvoidfun(){cout<<1;}
};
classDerived:
publicBase{
public:
voidfun(){cout<<2;}
};
voidmain(){
Base*pB=newDerived;
pB->fun();
deletepB;
}
Whichofthefollowingiscorrectoutput?
A.1B.12C.2D.21
17.Whichoneofthefollowingoperatorsinc++cannotbeoverload?
A.?
:
B.[]C.&&D.new
18.Whichofthefollowingcodemarked
(1),
(2),(3)and(4)iswrong?
classA{
public:
voidfun(){}
};
classB{
public:
voidfun(){}//
(1)
voidgun(){fun();}//
(2)
};
classC:
publicA,publicB{
private:
intb;
public:
voidgun(){}//(3)
voidhun(){fun();}//(4)
};
A.(4)B.(3)C.
(2)D.
(1)
19.Whichofthefollowingwillbecalledbyfun(8,3.1)?
A.fun(double,double)
Btemplatefun(T1,T2)
Cfun(double,int)
Dfun(charfloat)
20.Suppose:
template
classTAdd{
Tx,y;
public:
TAdd(Ta,Tb):
x(a),y(b){}
intadd(){returnx+y;}
};
voidmain()
{
TAddA(3.9,4.9);
TAddB(4.5,6.8);
cout<}
whichofthefollowingiscorrectoutput?
A.811
B.810
C.711
D.710
21.Suppose:
classTest{
public:
Test(){n+=2;}
~Test(){n-=3;}
staticintgetNum(){returnn;}
private:
staticintn;
};
intTest:
:
n=0;
voidmain()
{
Test*p=newTest;
deletep;
cout<<”n=”<:
getNum()<}
Whichofthefollowingiscorrectoutput?
A.n=2B.n=1C.n=0D.n=-1
22.Suppose:
classA{
public:
A(){cout<<"A";}
};
classB:
publicA{
public:
B(){cout<<"B";}
};
classC:
publicA{
Bb;
public:
C(){cout<<"C";}
};
voidmain(){
Cobj;
}
Whichofthefollowingiscorrectoutput?
A.ABACB.AABCC.ABCD.CAB
23.Suppose:
classONE{
public:
virtualvoidf(){cout<<"1";}
};
classTWO:
publicONE{
public:
TWO(){cout<<"2";}
};
共6页第3页
专业
班级
学号
姓名
classTHREE:
publicTWO{
public:
THREE(){cout<<"3";}
virtualvoidf(){TWO:
:
f();cout<<"3";}
};
voidmain()
{
ONEaa,*p;
TWObb;
THREEcc;
p=&cc;
p->f();
}
Whichofthefollowingiscorrectoutput?
A.22313B.21313C.2113D.2231
24.Suppose:
classLocation
{
public:
Location(intxx,intyy):
x(xx),y(yy){}
Location&operator+(Location&offset);
intgetX(){returnx;}
intgetY(){returny;}
private:
intx;
inty;
};
Location&Location:
:
operator+(Location&offset)
{
x=x+2*offset.x;
y=y+2*offset.y;
return*this;
}
voidmain()
{
Locationp1(10,20),off(8,8);
cout<<"("<p1=p1+off;
cout<<"("<}
Whichofthefollowingiscorrectoutput?
A.(10,20)(18,28)
B.(10,20)(26,36)
C.(10,20)(8,8)
D.(10,20)(16,16)
共6页第4页
25.Suppose:
template
classNum{
Tx;
public:
Num(){}
Num(Tx){this->x=x;}
Num&operator+(constNum&x2){
staticNumtemp;
temp.x=x+x2.x;
returntemp;
}
voiddisp(){cout<<"x="<};
voidmain(){
NumA(2.8),B(3.8);
A=A+B;
A.disp();
}
Whichofthefollowingiscorrectoutput?
A.x=8B.x=7C.x=6D.x=5
得分
PartII、Pleasereadfollowingprogramsandgivetheresultswhicharemarkedwith【code1】to【code5】inprograms.(Totalquestions40points,each2points)
1.Supposetheprogram’soutputis:
Ctriangle:
a=3b=4c=5
Perimeter:
12
Area:
6
Pleasewritedownyouranswerinappointedposition.
#include
#include
usingnamespacestd;
classCtriangle{
public:
Ctriangle(doublex,doubley,doublez){
a=x;
b=y;
c=z;
}
doubleGetPerimeter(){//计算三角形周长
【code1】
}
doubleGetArea(){//计算三角形面积
doublep=GetPerimeter()/2;
returnsqrt(p*(p-a)*(p-b)*(p-c));
}
专业
班级
学号
姓名
voiddisplay(){
cout<<"Ctriangle:
"<<"a="<【code2】//调用GetPerimeter()成员函数求当前对象的周长
【code3】//调用GetArea()成员函数求当前对象的面积
}
private:
doublea;
doubleb;
doublec;
};
voidmain()
{
【code4】//定义一个边长为3,4,5的Triangle类的对象T
【code5】//调用T的成员函数display()输出
}
2.Supposetheprogram’soutputis:
ABA-B-A-A
Pleasewritedownyouranswerinappointedposition.
#include
usingnamespacestd;
classA{
public:
A(){【code1】}//在命令行窗口输出字符串
~A(){【code2】}//在命令行窗口输出字符串
};
classB:
【code3】{//类B保护派生自类A
A*p;
public:
B(){
cout<<”B”;
【code4】//产生一个A类的动态对象,并将首地址赋给指针p
}
~B(){
cout<<”-B”;
【code5】//释放指针p所指向的内存空间
}
};
voidmain(){
Bobj;
}
3.Supposetheprogram’soutputis:
CreatingB
EndofB
EndofA
Pleasewritedownyouranswerinappointedposition.
#include
usingnamespacestd;
classA{
public:
A(){}
共6页第5页
【code1】{cout<<”EndofA”<};
classB:
【code2】//类B公有派生自类A
{
public:
B(){【code3】}//在命令行窗口输出字符串
~B(){【code4】}//在命令行窗口输出字符串
};
voidmain()
{
【code5】//产生B类的动态对象,并将其首地址赋值给A类的指针pa
deletep;//释放指针pa所指向的内存空间
}
4.Supposetheprogram’soutputis:
Catiscrying!
Dogiscrying!
Pleasewritedownyouranswerinappointedposition.
#include
usingnamespacestd;
classAnimal{//定义抽象类Animal
public:
【code1】//定义纯虚函数cry()
};
classCat:
publicAnimal
{
public:
voidcry(){
【code2】//在命令行窗口输出字符串
}
}
classDog:
publicAnimal
{
public:
voidcry(){
【code3】//在命令行窗口输出字符串
}
}
voidmain(){
【code4】//声明一个指向抽象类Animal的指针pA
pA=newCat();
pA->cry()