北京大学程序设计实习期末考试AWord文档格式.docx
《北京大学程序设计实习期末考试AWord文档格式.docx》由会员分享,可在线阅读,更多相关《北京大学程序设计实习期末考试AWord文档格式.docx(20页珍藏版)》请在冰点文库上搜索。
()7.const成员函数内部不能修改成员变量的值,但是可以调用非const成员函数。
()8.当基类指针指向派生类对象时,可以通过该基类指针访问该派生类对象的全部成员
()9.将一个对象放入STL中的容器里时,实际上被放入的是该对象的一个拷贝
()10.只有定义时用了virtual关键字的成员函数才是虚函数
三、单项选择题(16分,每题2分)请在题号边上的括号中作答
()1.以下类定义哪个是不正确的?
A)
classA{
public:
voidFunc(int){};
intFunc(int){};
};
C)
voidFunc(intx,inty){};
voidFunc(intx){};
B)
voidFunc(int){};
voidFunc(double){};
D)
intFunc(int)const{};
()2.假定A是一个类的名字,下面哪个语句不会引发类A构造函数的调用?
A)A*p=newA;
B)Aa;
C)Aa[10];
D)A*a[10];
()3.如果将运算符“()”重载为某个类的成员运算符(也即成员函数),则该成员函数的参数个数是:
A)0个B)1个C)2个D)任意数目均可
()4.下面哪种容器,其元素在容器中的位置与元素的值没有关系?
A)set
B)map
C)multimap
D)list
()5.以下4个程序片断,哪个是正确的?
假设main之上都有且只有以下代码:
#include<
set>
vector>
list>
stack>
algorithm>
usingnamespacestd;
A)
intmain(){
set<
int>
s;
:
iterator
p=s.begin();
p+=5;
return0;
}
vector<
v(5);
p=v.end();
p--;
B)
stack<
D)
inta[5]={1,2,3,4,5};
list<
L(a,a+5);
sort(L.begin(),L.end());
()6.有如下程序:
classBASE{
charc;
public:
BASE(charn):
c(n){}
virtual~BASE(){cout<
<
c;
}
};
classDERIVED:
publicBASE{
DERIVED(charn):
BASE(n+1),c(n){}
~DERIVED(){cout<
intmain()
{ DERIVED('
X'
);
return0;
}
执行上面的程序将输出:
A)XYB)YXC)XD)Y
()7.下列函数模板中定义正确的是:
A)template<
classT1,classT2>
T1fun(T1,T2){returnT1+T2;
}
B)template<
classT>
Tfun(Ta){returnT+a;
C)tempmlate<
classT1,classT2>
T1fun(T1,T2){returnT1+T2;
D)template<
classT>
Tfun(Ta,Tb){returna+b;
()8.下面程序对一维点坐标类Point进行运算符重载
iostream>
classPoint{
Point(intval){x=val;
Point&
operator++(){x++;
return*this;
Pointoperator++(int){Pointold=*this;
++(*this);
returnold;
intGetX()const{returnx;
private:
intx;
intmain(){
Pointa(10);
cout<
(++a).GetX();
a++.GetX();
return0;
编译和运行情况是
A)运行时输出1011;
B)运行时输出1111;
C)运行时输出1112;
D)编译有错。
四、看程序写结果。
下面的程序编译、链接都能通过,请写出运行时输出的结果。
你认为没有输出的,就写"
无输出"
(每题5分,共25分)
1.
classCSample{
intx;
CSample(){};
CSample(intn):
x(n){};
CSample(CSample&
d)
{x=2+d.x;
CSamplea(10);
CSampleb=a;
cout<
a.x<
endl<
b.x<
endl;
2.
stdio.h>
classCBase{
intvalA;
intvalB;
CBase(intarg){
valA=arg;
valB=2*arg;
voidprintA(){
printf("
CBase:
%d\n"
valA);
classCDerived:
publicCBase{
CDerived(intarg1,intarg2)
CBase(arg2)
{valA=arg1;
voidprintA(){
printf("
CDerived:
valA);
CBasebObj(5);
CDeriveddObj(4,6);
dObj.printA();
bObj=dObj;
bObj.printA();
3.
template<
classsample
{
sample(){};
sample(Tx){n=x;
sample<
T>
&
operator+
(constsample<
s)
staticsample<
temp;
temp.n=n+s.n;
returntemp;
constsample<
operator=
(constsample<
&
{
n=s.n;
voiddisp(){
cout<
"
n="
n<
endl;
protected:
Tn;
intmain()
s1(10),s2(20),s3;
s3=s1+s2;
s1.disp();
s3.disp();
4.
classA
intn;
virtualvoidPrint(){
"
PrintA:
n="
<
n
A(inti):
n(i){
Print();
voidFunc(){
cout<
A:
Func"
Print();
classB:
publicA
intn;
B(inti):
n(i),A(i-1){
}
voidPrint(){
PrintB:
<
B:
A*p=newB(9);
p->
Print();
Func();
deletep;
5.
inta[]={1,3,5,4,4,6,3};
vector<
v(a,a+7);
iteratorp=find(v.begin(),v.end()-2,10);
*p<
getchar();
五、根据程序的输出结果,填写程序中空缺的代码。
假定你所需要的C++标准库头文件都已经包含。
(22分)请直接在卷子上填
1)(4分)下面程序输出是:
4
9
请填空
classArray
________________;
Array(T*a,intn){
__________________;
for(inti=0;
i<
n;
i++)
p[i]=a[i];
~Array(){
deletep;
_____________________(inti)
{
return___________;
inta[]={1,2,3,4,5};
Array<
MyArray(a,4);
MyArray[3]<
MyArray[3]=9;
2)(4分)下面程序输出是:
10,9,8,6,3,2
inta[]={2,3,10,9,8,6};
v(a,a+6);
_____________________________;
for(inti=0;
v.size();
cout<
v[i]<
"
;
3)(4分)下面程序输出:
Bob,Jack,Mary,Tom,
classMyContainer:
____________________
voidPrintAll(){
______________________;
for(i=begin();
i!
=end();
cout<
*i<
strings[]={"
Tom"
Jack"
Mary"
Bob"
"
________________________________;
5;
i++)
m.insert(s[i]);
m.PrintAll();
4)(4分)下面程序输出:
1
classA
A(intv):
n(v){}
_________________________(intk){
n-=k;
Aa(5);
a-=4;
a.n;
5)(6分)下面程序输出是:
6,5,4,1,
____________________________________;
intGet(){returnn;
};
A(inti):
n(i){};
boolMyCompare(________________________________)
return_______________________;
_________________;
v.push_back(newA(5));
v.push_back(newA(6));
v.push_back(newA(4));
v.push_back(newA
(1));
sort(v.begin(),v.end(),MyCompare);
i++)
v[i]->
Get()<
;
六、编写程序(19分)
1.(6分)编写行为类似于标准库中find算法的函数模板findElem。
findElem模板可用于在vector<
和vector<
string>
中查找给定值。
你写的模板中不能使用任何C++标准库中的函数和模板。
除此模板之外你不能再写其他任何东西。
intia[]={1,2,3,4,5,6,7};
stringsa[]={"
this"
"
is"
test"
example"
ivec(ia,ia+7);
svec(sa,sa+5);
iteratoriit;
if((iit=findElem(ivec.begin(),ivec.end(),6))!
=ivec.end())
foundthiselement:
*iit<
else
nosuchelement."
iteratorsit;
if((sit=findElem(svec.begin(),svec.end(),"
))!
=svec.end())
*sit<
提示:
STL中copy模板的源码:
template<
class_II,class_OI>
inline
_OIcopy(_II_F,_II_L,_OI_X){
for(;
_F!
=_L;
++_X,++_F)
*_X=*_F;
return(_X);
2.(7分)请写一个单词排序程序。
输入若干单词,将其排序后输出。
输入数据:
第一行为整数N,代表单词数目。
0<
N<
10000
以下N行,每行都是一个单词。
每个单词不超过50个字符,全部由小写字母组成。
输出数据:
将单词按词典序从大到小输出。
如果有重复单词,重复单词只输出一个。
输入样例:
6
about
take
call
me
dog
输出样例:
特别要求:
必须使用set容器来实现。
3.(6分)
为了表示一组对象,已经定义了一个类MyClass,并已对这个类重载了逻辑小于运算符“<
”。
这些对象被预先存储在一棵二叉树上,其中每个节点是一个类型为Node的变量,存储一个对象的值。
请编写一个函数,统计这组对象中有多少对象x位于区间[a,b]内,其中a和b是MyClass的两个对象。
Node的定义如下。
structNode{
MyClassval;
Node*lChild,*rChild;
所编写函数的接口定义如下:
intcount(Node*root,MyClass&
a,MyClass&
b)
返回值:
在root指向的二叉树上,有多少个节点的值位于区间[a,b]内