08092C++2试题B含答案16.docx

上传人:b****4 文档编号:6739503 上传时间:2023-05-10 格式:DOCX 页数:17 大小:218.89KB
下载 相关 举报
08092C++2试题B含答案16.docx_第1页
第1页 / 共17页
08092C++2试题B含答案16.docx_第2页
第2页 / 共17页
08092C++2试题B含答案16.docx_第3页
第3页 / 共17页
08092C++2试题B含答案16.docx_第4页
第4页 / 共17页
08092C++2试题B含答案16.docx_第5页
第5页 / 共17页
08092C++2试题B含答案16.docx_第6页
第6页 / 共17页
08092C++2试题B含答案16.docx_第7页
第7页 / 共17页
08092C++2试题B含答案16.docx_第8页
第8页 / 共17页
08092C++2试题B含答案16.docx_第9页
第9页 / 共17页
08092C++2试题B含答案16.docx_第10页
第10页 / 共17页
08092C++2试题B含答案16.docx_第11页
第11页 / 共17页
08092C++2试题B含答案16.docx_第12页
第12页 / 共17页
08092C++2试题B含答案16.docx_第13页
第13页 / 共17页
08092C++2试题B含答案16.docx_第14页
第14页 / 共17页
08092C++2试题B含答案16.docx_第15页
第15页 / 共17页
08092C++2试题B含答案16.docx_第16页
第16页 / 共17页
08092C++2试题B含答案16.docx_第17页
第17页 / 共17页
亲,该文档总共17页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

08092C++2试题B含答案16.docx

《08092C++2试题B含答案16.docx》由会员分享,可在线阅读,更多相关《08092C++2试题B含答案16.docx(17页珍藏版)》请在冰点文库上搜索。

08092C++2试题B含答案16.docx

08092C++2试题B含答案16

姓名学号

学院专业座位号

(密封线内不答题)

……………………………………………………密………………………………………………封………………………………………线……………………………………线………………………………………

_____________________

诚信应考,考试作弊将带来严重后果!

华南理工大学期末考试2009-6-26

《高级语言程序设计

(2)》试卷B

注意事项:

1.考前请将密封线内各项信息填写清楚;

2.所有答案写在答题纸上,答在其它地方无效;

3.试卷可做草稿纸,试卷必须与答题纸同时提交;

4.考试形式:

闭卷;

5.本试卷共五大题,满分100分,考试时间120分钟。

 

一.单项选择题(每题2分,共20分)

1.下列有关C++构造函数,析构函数的说法正确的是(B)

A.系统提供默认的析构函数,不提供默认的构造函数

B.构造函数可以重载,析构函数不可以重载

C.构造函数可以提供默认参数,也可以提供返回值

D.析构函数不可以定义为虚函数

2.下列有关C++的this指针的说法正确的是(C)

A.定义一个类后,this指针就指向该类

B.调用静态成员函数会隐含传递this指针参数

C.友元函数没有this指针

D.this指针是隐含的,不能显式使用

3.只能用友元函数重载的运算符是(A)

A.<

4.在下列类声明

classCComplex{/*……*/};

能实现CComplex类型转换为CReal类型的函数是(C)

A.CComplex:

:

CComplex();B.CComplex:

:

CComplex(CReal);

C.CComplex:

:

operatorCReal();D.CComplex:

:

operatorCReal(Complex);

5.有关C++中继承的说法正确的是(B)

A.继承关系具备对称性和传递性

B.一个类可以有多个基类,也可以有多个派生类

C.一个基类不能有多个派生类,而一个派生类可以有多个基类

D.继承类层次中,基类的构造函数和析构函数都不能声明为虚的

6.有如下代码

ClassCBase{public:

voidprint();};

ClassCDerived:

publicCBase{public:

voidprint();};

voidmain(){CDerivedd;CDerived*ptr=&d;/*……*/}

能正确调用Cbase类中print()方法的语句是(C)

A.d.print()B.CBase:

:

print()

C.ptr->CBase:

:

print()D.ptr->print()

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)

templateclassTany{/*……*/};

A.CArrayany;

B.CArrayIntany

C.doubleget(constTany);

D.templateTget(constTany*);

10.用于处理字符串流的是(A)

A.strstreamB.iosC.fstreamD.iosteam

二.阅读下列程序,写出执行结果(每题6分,共24分)

1.//输入输出流

#include

#include

#include

voidmain()

{charch='a';

ofstreamoutfile;

outfile.open("f.txt");

if(!

outfile)

{cout<<"f.txtcannotopen!

"<

while(ch<='f')

{outfile<

}

outfile.close();

fstreaminfile("f.txt",ios:

:

in);

while(infile>>ch)

cout<

cout<

}

答案:

defghi

2.//虚函数、继承性与多态性

#include

classBase

{public:

virtualvoidf()

{cout<<"Base"<

};

classSubClass:

publicBase

{public:

virtualvoidf()

{cout<<"SubClass"<

};

voidtest(Base&b)

{b.f();}

voidmain()

{Basebc;

SubClasssc;

test(bc);

test(sc);

}

答案:

Base

SubClass

3.//模板

#include

constintNUM=4;

template

voidtraverse(Ta[])

{for(inti=0;i

cout<

}

template

voidbubbleSort(Ta[])

{for(inti=0;i

for(intj=i+1;j

if(a[i]>a[j])swap(a,i,j);

}

template

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.//浅复制、深复制与自定义复制构造函数。

#include

#include

className

{public:

Name(char*pn);

Name(constName&Obj);

~Name();

protected:

char*pName;

intsize;

};

Name:

:

Name(char*pn)

{cout<<"Construct"<

pName=newchar[strlen(pn)+1];

if(pName!

=0)strcpy(pName,pn);

size=strlen(pn);

}

Name:

:

Name(constName&Obj)

{cout<<"Copy"<

pName=newchar[strlen(Obj.pName)+1];

if(pName!

=0)strcpy(pName,Obj.pName);

size=Obj.size;

}

Name:

:

~Name()

{cout<<"Destruct"<

pName[0]='\0';

delete[]pName;

pName=NULL;

size=0;

}

voidmain()

{NameObj1("anne");

NameObj2=Obj1;

}

答案:

constructanne

copyanneintointsownblock

destructanne

destructanne

三.简答题(每小题4分,共20分)

1.有以下程序:

#include

classX

{private:

inta;doubley;

//……

};

voidmain()

{cout<

执行结果如图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的定义。

#include

classA

{inta;

public:

A(intaa){a=aa;}

};

classB:

publicA

{intb;

public:

B(intaa,intbb):

A(aa){b=bb;}

voidout(){cout<

};

voidmain()

{Bx(1,2);x.out();}

classB

{intb;Aba;

public:

B(intaa,intbb):

ba(aa){b=bb;}

voidout(){cout<

};

3.有程序:

#include

classA

{protected:

inta;

public:

A(intaa){a=aa;}

voidprint(){cout<<"a="<

};

classX:

publicA

{protected:

doubley;

public:

X(intaa,doubleyy):

A(aa){y=yy;}

voidprint(){cout<<"a="<

};

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)->print();

4.若有说明:

templatevoidfun(T,T);

inta,doublex;

以下语句

fun(a,a);

fun(x,x);

fun(a,x);

有错误吗?

若出错,请分析原因,并给出解决方案。

重载voidfun(intdouble);

5.以下程序不能正确显示文件中的数据。

请分析原因,并修改相应代码。

#include

voidmain()

{ofstreamfout("d:

DATA.dat",ios:

:

out|ios:

:

binary);

inti,j;

for(i=0;i<20;i++)fout.write((char*)&i,sizeof(int));

fout.close();

//输出文件数据:

ifstreamfin("d:

DATA.dat");

while(fin){fin>>j;cout<

fin.close();

}

ifstreamfin("d:

DATA.dat",ios:

:

in|ios:

:

binary);

for(i=0;i<20;i++)

{fin.read((char*)&j,sizeof(int));cout<

fin.close();

四.根据程序输出填空。

(每空2分,共24分)

1.程序运行效果如图3。

//成员和友员

#include

classBBank;

classCBank

{intbalance;

public:

CBank(intb=0){balance=b;}

voidgetbalance();

voiddisp(){cout<<"ThesavingsinChinaBankis:

"<

(1)total(CBank,BBank);//friendvoid

};

(2)getbalance()//voidCBank:

:

{cout<<"InputthesavingsinChinaBank:

";cin>>balance;}

classBBank

{intbalance;

public:

BBank(intb=0){balance=b;}

voidgetbalance()

{cout<<"InputthesavingsinBusinessBank:

";cin>>balance;}

voiddisp()

{cout<<"ThesavingsinBusinessBankis:

"<

(3)voidtotal(CBank,BBank);//friend

};

voidtotal(CBankA,BBankB)

{cout<<"Totalsavingsis:

"<

voidmain()

{CBankX(100);BBankY;

X.disp();Y.disp();

(4);//Y.getbalance()

total(X,Y);

}

2.程序运行效果如图4所示。

重载运算符

#include

classCRect

{intlength,width;

public:

CRect(){};

CRect(intl,intw){length=l;width=w;}

voiddisp(){cout<<"Theareais:

"<

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;

}

};

voidmain()

{CRectr1(3,3),r2(5,8),r3(2,4);

r1.disp();r2.disp();r3.disp();

r1=r2=r2+r3;r1.disp();

}

3.程序运行效果如图5所示。

//虚继承

#include

classBase

{public:

Base(inti){cout<<"Baseconstructorcalled"<

};

classBase1:

(8)Base//virtualpublic

{public:

Base1(inti,intj):

Base(j)

{cout<<"Base1constructorcalled"<

};

classBase2:

(9)//publicBase

{public:

Base2(inti,intj):

Base(j)

{cout<<"Base2constructorcalled"<

};

classDerived:

publicBase2,publicBase1

{public:

Derived(inti,inta,intb,intc)

:

(10)Base1(i,a),Base2(i,b),Base(c)

{cout<<"Derivedconstructorcalled"<

};

intmain()

{DerivedobjD(1,2,3,4);return0;}

 

4.程序运行效果如图6所示。

//静态数据

#include

classcounter

{(10);//staticintnum

intt;

public:

counter(inta){t=a;}

voidshow()const

{cout<

};

(10)=5;//intcounter:

:

num

voidmain()

{countera

(1),b

(2),c(3);

cout<<"a:

";a.show();

cout<<"b:

";b.show();

cout<<"c:

";c.show();

}

5.程序运行效果如图7所示。

//继承

#include

#include

classTwoDShape//二维图形类

{doublewidth,height;

public:

TwoDShape((11)){width=w;height=h;}//doublew=0,doubleh=0

voidshowDim(){cout<<"Widthandheightare"<

doublegetWidth(){returnwidth;}

doublegetHeight(){returnheight;}

voidsetWidth(doublew){width=w;}

voidsetHeight(doubleh){height=h;}

};

classTriangle:

publicTwoDShape

{charstyle[20];

public:

Triangle(12){strcpy(style,str);}

//(char*str,doublew=0,doubleh=0):

TwoDShape(w,h)

doublearea(){returngetWidth()*getHeight()/2;}

voidshowStyle(){cout<<"Triangleis"<

};

intmain()

{Trianglect1("right",7.0,12.0),ct2("right");

ct1.showStyle();ct1.showDim();

cout<<"Areais"<

ct2.showStyle();ct2.showDim();

cout<<"Areais"<

return0;

}

五、完成程序。

(第1小题4分,第2小题8分,共12分)

1.根据程序输出(如图8所示),以最小形式补充A类的成员函数。

#include

classA

{inta,b,c;

//成员函数

};

voidmain()

{Aa1(11,22,33);

Aa2(a1);

cout<<"a1:

";a1.out();

cout<<"a2:

";a2.out();

}

public:

A(intaa,intbb,intcc)

{cout<<"Initialobject\n";a=aa;b=bb;c=cc;}

A(A&obj)

{cout<<"Copyinitialobject\n";a=obj.a;b=obj.b;c=obj.c;}

voidout(){cout<

2.本程序包含了time类和Date类的声明。

请设计一个Birthtime类,它继承了time类和Date类,并且还有一项学生的名字Studentname。

并设计主函数显示一个学生的名字和出生日期、时间。

输出效果如图9所示。

#include

#include

classTime

{publi

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2