2C 语言2试题B含答案Word格式文档下载.docx
《2C 语言2试题B含答案Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《2C 语言2试题B含答案Word格式文档下载.docx(17页珍藏版)》请在冰豆网上搜索。
voidprint();
};
ClassCDerived:
publicCBase{public:
voidmain(){CDerivedd;
CDerived*ptr=&
d;
/*……*/}
能正确调用Cbase类中print()方法的语句是(C)
A.d.print()B.CBase:
print()
C.ptr->
CBase:
print()D.ptr->
7.如果CInterface是抽象类,下列语句错误的是(A)
A.CInterfaceobj;
B.CInteface*getInterface();
C.CInterface*ptr;
D.char*getName(CInterface&
);
8.针对如下代码段,正确的说法是(D)
ClassCInterface
{public:
virtualconstchar*getID()const=0;
/*……*/}
A.CInterface是抽象类B.getID函数是纯虚函数
C.getID函数没有执行代码D.A,B,C说法都正确
9.对如下类模板错误使用是(A)
template<
typenameT>
classTany{/*……*/};
A.CArrayany;
B.CArray<
int>
Intany
C.doubleget(constTany<
double>
D.template<
Tget(constTany<
T>
*);
10.用于处理字符串流的是(A)
A.strstreamB.iosC.fstreamD.iosteam
二.阅读下列程序,写出执行结果(每题6分,共24分)
1.//输入输出流
#include<
fstream.h>
iostream.h>
stdlib.h>
voidmain()
{charch='
a'
;
ofstreamoutfile;
outfile.open("
f.txt"
if(!
outfile)
{cout<
"
f.txtcannotopen!
endl;
abort();
}
while(ch<
='
f'
)
{outfile<
ch;
ch++;
outfile.close();
fstreaminfile("
ios:
in);
while(infile>
>
ch)
cout<
char(ch+3)<
"
}
答案:
defghi
2.//虚函数、继承性与多态性
classBase
virtualvoidf()
Base"
};
classSubClass:
publicBase
SubClass"
voidtest(Base&
b)
{b.f();
{Basebc;
SubClasssc;
test(bc);
test(sc);
答案:
Base
SubClass
3.//模板
constintNUM=4;
template<
classT>
voidtraverse(Ta[])
{for(inti=0;
i<
NUM;
i++)cout<
a[i];
voidbubbleSort(Ta[])
i++)
for(intj=i+1;
j<
j++)
if(a[i]>
a[j])swap(a,i,j);
classT>
voidswap(Ta[],inti,intk)
{Ttemp;
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
voidmain()
{inta[NUM]={4,3,2,1};
doubleb[NUM]={4.4,3.3,2.2,1.1};
bubbleSort(a);
traverse(a);
bubbleSort(b);
traverse(b);
1234
1.12.23.34.4
4.//浅复制、深复制与自定义复制构造函数。
string.h>
className
Name(char*pn);
Name(constName&
Obj);
~Name();
protected:
char*pName;
intsize;
Name:
Name(char*pn)
{cout<
Construct"
pn<
pName=newchar[strlen(pn)+1];
if(pName!
=0)strcpy(pName,pn);
size=strlen(pn);
Name(constName&
Obj)
Copy"
Obj.pName<
intoitsownblock\n"
pName=newchar[strlen(Obj.pName)+1];
=0)strcpy(pName,Obj.pName);
size=Obj.size;
~Name()
Destruct"
pName<
pName[0]='
\0'
delete[]pName;
pName=NULL;
size=0;
{NameObj1("
anne"
NameObj2=Obj1;
constructanne
copyanneintointsownblock
destructanne
三.简答题(每小题4分,共20分)
1.有以下程序:
#include<
iostream.h>
classX
{private:
inta;
doubley;
//……
};
sizeof(X)<
执行结果如图1所示,这是否说明X:
a和X:
y占有存储空间?
类的数据成员是什么时候建立存储空间的?
举例说明,并请写一个初始化类X数据成员的函数。
不是。
创建对象时分配存储空间。
例如:
在main函数中有XXobj;
在类定义中加入public:
X(inta1=0,doubley1=0){a=a1;
y=y1;
},执行时创建对象Xobj,分配存储空间X.a,X.y。
2.有以下程序,类B是类A的派生类。
你认为该程序能够等价地定义为类B包含类A吗?
如果可以,请你写出类B的定义。
classA
{inta;
public:
A(intaa){a=aa;
}
classB:
publicA
{intb;
public:
B(intaa,intbb):
A(aa){b=bb;
voidout(){cout<
a<
'
\t'
b<
{Bx(1,2);
x.out();
classB
Aba;
B(intaa,intbb):
ba(aa){b=bb;
ba.a<
3.有程序:
{protected:
A(intaa){a=aa;
voidprint(){cout<
a="
classX:
X(intaa,doubleyy):
A(aa){y=yy;
\ty="
y<
voidmain()
{A*p=newX(12,3.1425);
p->
print();
输出结果如图2所示。
p->
print()为什么不能输出X类对象的全部数据成员?
请你给出两种修改程序的方法,可以调用X:
print()输出数据12和3.1425。
(1)定义虚函数virtualvoidprint();
(2)用X类指针建立对象X*p=newX(12,3.1425);
(3)做指针强类型转换((X*)p)->
4.若有说明:
template<
voidfun(T,T);
inta,doublex;
以下语句
fun(a,a);
fun(x,x);
fun(a,x);
有错误吗?
若出错,请分析原因,并给出解决方案。
重载voidfun(intdouble);
5.以下程序不能正确显示文件中的数据。
请分析原因,并修改相应代码。
{ofstreamfout("
d:
DATA.dat"
out|ios:
binary);
inti,j;
for(i=0;
20;
i++)fout.write((char*)&
i,sizeof(int));
fout.close();
//输出文件数据:
ifstreamfin("
);
while(fin){fin>
j;
j<
fin.close();
in|ios:
for(i=0;
i++)
{fin.read((char*)&
j,sizeof(int));
ends;
fin.close();
四.根据程序输出填空。
(每空2分,共24分)
1.程序运行效果如图3。
//成员和友员
classBBank;
classCBank
{intbalance;
public:
CBank(intb=0){balance=b;
}
voidgetbalance();
voiddisp(){cout<
ThesavingsinChinaBankis:
<
balance<
endl;
(1)total(CBank,BBank);
//friendvoid
(2)getbalance()//voidCBank:
{cout<
InputthesavingsinChinaBank:
cin>
balance;
classBBank
public:
BBank(intb=0){balance=b;
voidgetbalance()
InputthesavingsinBusinessBank:
cin>
balance;
voiddisp()
ThesavingsinBusinessBankis:
balance<
(3)voidtotal(CBank,BBank);
//friend
voidtotal(CBankA,BBankB)
{cout<
Totalsavingsis:
A.balance+B.balance<
{CBankX(100);
BBankY;
X.disp();
Y.disp();
(4);
//Y.getbalance()
total(X,Y);
2.程序运行效果如图4所示。
重载运算符
classCRect
{intlength,width;
CRect(){};
CRect(intl,intw){length=l;
width=w;
voiddisp(){cout<
Theareais:
length*width<
CRect&
(5)(CRectr)//operator=
{length=r.length;
width=r.width;
return(6);
//*this
CRectoperator+(CRectr)
{CRecttemp;
(7)
//temp.length=r.length+length;
temp.width=r.width+width;
returntemp;
{CRectr1(3,3),r2(5,8),r3(2,4);
r1.disp();
r2.disp();
r3.disp();
r1=r2=r2+r3;
3.程序运行效果如图5所示。
//虚继承
Base(inti){cout<
Baseconstructorcalled"
i<
classBase1:
(8)Base//virtualpublic
Base1(inti,intj):
Base(j)
Base1constructorcalled"
classBase2:
(9)//publicBase
Base2(inti,intj):
Base(j)
Base2constructorcalled"
classDerived:
publicBase2,publicBase1
Derived(inti,inta,intb,intc)
(10)Base1(i,a),Base2(i,b),Base(c)
Derivedconstructorcalled"
intmain()
{DerivedobjD(1,2,3,4);
return0;
4.程序运行效果如图6所示。
//静态数据
classcounter
{(10);
//staticintnum
intt;
counter(inta){t=a;
voidshow()const
t<
'
num<
(10)=5;
//intcounter:
num
voidmain()
{countera
(1),b
(2),c(3);
a:
a.show();
b:
b.show();
c:
c.show();
5.程序运行效果如图7所示。
//继承
classTwoDShape//二维图形类
{doublewidth,height;
TwoDShape((11)){width=w;
height=h;
}//doublew=0,doubleh=0
voidshowDim(){cout<
Widthandheightare"
width<
and"
height<
\n"
doublegetWidth(){returnwidth;
doublegetHeight(){returnheight;
voidsetWidth(doublew){width=w;
voidsetHeight(doubleh){height=h;
classTriangle:
publicTwoDShape
{charstyle[20];
Triangle(12){strcpy(style,str);
//(char*str,doublew=0,doubleh=0):
TwoDShape(w,h)
doublearea(){returngetWidth()*getHeight()/2;
voidshowStyle(){cout<
Triangleis"
style<
intmain()
{Trianglect1("
right"
7.0,12.0),ct2("
);
ct1.showStyle();
ct1.showDim();
cout<
Areais"
ct1.area()<
ct2.showStyle();
ct2.showDim();
ct2.area()<
五、完成程序。
(第1小题4分,第2小题8分,共12分)
1.根据程序输出(如图8所示),以最小形式补充A类的成员函数。
{inta,b,c;
//成员函数
voidmain()
{Aa1(11,22,33);
Aa2(a1);
a1:
a1.out();
a2:
a2.out();
A(intaa,intbb,intcc)
Initialobject\n"
a=aa;
b=bb;
c=cc;
A(A&
obj)
Copyinitialobject\n"
a=obj.a;
b=obj.b;
c=obj.c;
voidout(){cout<
c<
2.本程序包含了time类和Date类的声明。
请设计一个Birthtime类,它继承了time类和Date类,并且还有一项学生的名字Studentname。
并设计主函数显示一个学生的名字和出生日期、时间。
输出效果如图9所示。
classTime
Time(inth,intm,ints)