石河子大学信息学院c++期末考试复习题.docx

上传人:b****5 文档编号:12124118 上传时间:2023-04-17 格式:DOCX 页数:70 大小:35.82KB
下载 相关 举报
石河子大学信息学院c++期末考试复习题.docx_第1页
第1页 / 共70页
石河子大学信息学院c++期末考试复习题.docx_第2页
第2页 / 共70页
石河子大学信息学院c++期末考试复习题.docx_第3页
第3页 / 共70页
石河子大学信息学院c++期末考试复习题.docx_第4页
第4页 / 共70页
石河子大学信息学院c++期末考试复习题.docx_第5页
第5页 / 共70页
点击查看更多>>
下载资源
资源描述

石河子大学信息学院c++期末考试复习题.docx

《石河子大学信息学院c++期末考试复习题.docx》由会员分享,可在线阅读,更多相关《石河子大学信息学院c++期末考试复习题.docx(70页珍藏版)》请在冰豆网上搜索。

石河子大学信息学院c++期末考试复习题.docx

石河子大学信息学院c++期末考试复习题

(小题前面那个复习题里面有,大题注意一下)

试题一

一、单项选择题(共20分,每题2分,正确答案只有一项)

1.以下说法中正确的是

A.类A的private成员只能在类A的成员函数内部进行直接访问,其他任何地方都不能直接访问。

B.静态成员一定不能是private的。

C.在某种情况下,在派生类的成员函数内部也能对基类的私有成员进行直接访问。

D.只有成员变量才能是protected的,成员函数不能是protected的。

(C)

2.以下关于this指针的说法中不正确的是:

A.const成员函数内部不可以使用this指针

B.成员函数内的this指针,指向成员函数所作用的对象。

C.在构造函数内部可以使用this指针

D.在析构函数内部可以使用this指针

(A)

3.下列类模板中定义正确的是:

A.template

classA:

{

T1b;

intfun(inta){returnT1+T2;}

};

B.template

classA{

intT2;

T1fun(T2a){returna+T2;}

};

C.template

classA{

public:

T2b;T1a;

A(){}

T1fun(){returna;}

};

D.template

classA{

T2b;

T1fun(doublea){b=(T2)a;return(T1)a;}

};

(D)

4.假设p1,p2是STL中的list容器上的迭代器,那么以下语句哪个是不符合语法的

A.p1++;

B.p1--;

C.p1+=1;

D.intn=(p1==p2);

(C)

5.将一个对象放入STL中的容器里时:

A.实际上被放入的是该对象的一个拷贝(副本)

B.实际上被放入的是该对象的指针

C实际上被放入的是该对象的引用

D.实际上被放入的就是该对象自身

(A)

6.以下关于函数对象的说法正确的是:

A.函数对象所属的类将()运算符重载为一个成员函数

B.函数对象所属的类将[]运算符重载为一个成员函数

C.函数对象所属的类不能有成员变量

D.A和C都对

(A)

7.以下说法正确的是:

A.const成员函数内可以调用非const成员函数

B.在抽象类的某些成员函数中,可以出现调用纯虚函数的语句

C.静态成员函数也可以是虚函数

D.静态成员变量只能通过静态成员函数来访问

(B)

8.如果将运算符“*”重载为某个类的成员运算符(也即成员函数),则该成员函数的参数个数是:

A.0个B.1个C.2个D.根据实际作用不同,0个或1个都行

(D)

9.以下关于STL中stack类模板的正确说法是:

A.stack是关联容器

B.对于stack上的迭代器p,能够执行p++操作

C.stack可以用deque实现

D.可以用sort算法对stack进行排序

(C)

10.以下说法正确的是

A.在静态成员函数中调用虚函数是动态联编(多态)

B.在构造函数中调用虚函数,不是动态联编

C.抽象类的成员函数都是纯虚函数

D.构造函数和析构函数都不能是虚函数

(B)

二.以下程序编译、连接都能通过,请写出运行时输出的结果。

你认为没有输出的,就写"无输出"(共28分)。

1)4分

#include

#include

usingnamespacestd;

classA{

private:

intnId;

public:

A(intn){

nId=n;

cout<

};

~A(){

cout<

}

};

main()

{

vectorvp;

vp.push_back(newA

(1));

vp.push_back(newA

(2));

vp.clear();

Aa(4);

}

/*

1contructor

2contructor

4contructor

4destructor

*/

2)4分

#include

classApple{

private:

staticintnTotalNumber;

public:

Apple()

{nTotalNumber++;}

~Apple(){nTotalNumber--;}

staticvoidPrintTotal()

{cout<

};

intApple:

:

nTotalNumber=0;

intmain(){

Apple*p=newApple[4];

Apple:

:

PrintTotal();

Applep1,p2;

delete[]p;

Apple:

:

PrintTotal();

}

/*

4

2

*/

3)4分

#include

#include

usingnamespacestd;

classA{

public:

inti;

A(intn):

i(n){};

voidPrint(){cout<

};

A&Func(vector&v,A*p)

{

v.push_back(*p);

for(inti=0;i

if(v[i].i==2){

p=&(v[i]);

break;

}

return*p;

}

main()

{

constintSIZE=4;

Aarray[SIZE]={A

(1),A

(2),A(3),A(4)};

vectorv(array,array+SIZE);

A*p=newA(5);

Func(v,p)=10;

for(inti=0;i

v[i].Print();

cout<

cout<i;

}

/*

1,10,3,4,5,

5

*/

4)4分

#include

classA{

public:

A(){}

virtualvoidfunc()

{cout<<"A:

:

func"<

~A(){}

virtualvoidfund()

{cout<<"A:

:

fund"<

};

classB:

publicA{

public:

B(){func();}

voidfun(){func();}

virtual~B(){fund();}

};

classC:

publicB{

public:

C(){}

voidfunc()

{cout<<"C:

:

func"<

~C(){fund();}

voidfund()

{cout<<"C:

:

fund"<

};

main()

{

A*pa=newC();

deletepa;

B*pb=newC();

deletepb;

}

/*

A:

:

func

A:

:

func

C:

:

fund

A:

:

fund

*/

5)4分

#include

#include

#include

usingstd:

:

vector;

usingstd:

:

accumulate;

classComplex{

doublereal;

doubleimage;

public:

Complex(doubler=0,doublei=0):

real(r),image(i){}

friendComplexoperator+(constComplex&c,inti);

friendostream&operator<<(ostream&o,constComplex&c);

};

Complexoperator+(constComplex&c,inti)

{

Complextmp;

tmp.real=i*i+c.real;

tmp.image=c.image;

returntmp;

}

ostream&operator<<(ostream&o,constComplex&c)

{

o<

returno;

}

main()

{

inta[]={1,2,3,4};

vectorv(a,a+4);

cout<

}

/*

32+3i

*/

6)4分

#include

#include

usingnamespacestd;

classGt

{

public:

booloperator()(constint&n1,constint&n2)const{

return(n1%10)>(n2%10);

}

};

intmain(){

typedefmapmmid;

mmidMyMap;

cout<

MyMap.insert(mmid:

:

value_type(15,2.7));

MyMap.insert(mmid:

:

value_type(15,99.3));

cout<

MyMap.insert(mmid:

:

value_type(30,111.11));

MyMap.insert(mmid:

:

value_type(11,22.22));

cout<

for(mmid:

:

const_iteratori=MyMap.begin();

i!

=MyMap.end();i++)

cout<<"("<first<<","<second

<<")"<<",";

}

/*

0

1

0

(16,0),(15,2.7),(11,22.22),(30,111.11)

*/

7)4分

#include

classA1{

public:

inti;

A1(intn):

i(n){cout<<"A1_Con:

"<

~A1(){cout<<"A1_Des:

"<

};

classA2{

public:

inti;

A2(intn):

i(n){cout<<"A2_Con:

"<

~A2(){cout<<"A2_Des:

"<

};

classB{

public:

B(){cout<<"B_Con"<

~B(){cout<<"B_Des"<

};

classC:

publicB{

public:

A2a2;

A1a1;

C():

a1

(1),a2

(1){cout<<"C_Con"<

~C(){cout<<"C_Des"<

};

main(){

C*pc=newC;

}

/*

B_Con

A2_Con:

1

A1_Con:

1

C_Con

*/

三、程序填空(36分):

已知以下程序的输出结果,请填出缺少的部分。

您填写的内容里不能包含分号。

假设您需要的头文件前面都已经有#include语句包含进来了(即可以随意使用标准库中的类、模板等)

1)6分

voidOutputN(intn);

函数的功能是输出所有数正整数i,j,k的组合,该组合满足下列3个条件:

1)i,j,k都不大于n

2)i

3)i+j+k能够被3整除

比如,OutputN(5)的输出结果就是:

1,2,3

1,3,5

2,3,4

3,4,5

请补上丢失的部分。

#include

voidOutputN(intn)

{

inti,j,k;

for(_____;______;_____)

for(_____;______;______)

for(______;______;______)

if((i+j+k)%3==0)

cout<

}

/*

i=1

i<=n

i++

j=i+1

j<=n

j++

k=j+1

k<=n

k++

*/

2)6分

从1,2,3,4,5这5个整数中取一个或多个数(每个数最多只能取1次),使得取出的数总和能被3整除。

下面的这段程序的功能就是用枚举的办法,求出了所有满足条件的取法并输出。

输出结果如下:

1,2,

3,

1,2,3,

2,4,

2,3,4,

1,5,

1,3,5,

4,5,

1,2,4,5,

3,4,5,

1,2,3,4,5,

请填出缺少的部分

#include

#include

#include

usingnamespacestd;

intbit(intn,intb)

{

return____________;

}

main()

{

inti;

intsum;

vectorv;

for(i=1;i<32;i++){

sum=0;

____________;

for(intj=0;j<5;j++)

if(bit(__________)){

sum+=j+1;

v.push_back(j+1);

}

if((sum%3)==0){

for(intk=0;k

cout<

cout<

}

}

}

/*

n&(1<

v.clear();

i,j

*/

3)6分

输出结果:

Tom,Jack,Mary,John,

程序:

#include

#include

#include

usingnamespacestd;

template

classMyClass

{

vectorarray;

public:

MyClass_____________________

{

copy(begin,begin+n,array.begin());

}

voidList(){

______________________;

for(i=array.begin();i!

=array.end();i++)

cout<<*i<<",";

}

};

main()

{

stringarray[4]={"Tom","Jack","Mary","John"};

_________________________;

obj.List();

}

/*

(T*begin,intn):

array(n)

vector:

:

iteratori

MyClassobj(array,4)

*/

 

4)6分

输出结果:

A:

:

Print:

1

B:

:

Print:

2

B:

:

Print:

3

程序:

template

voidPrintAll(constT&c)

{

T:

:

const_iteratori;

for(i=c.begin();i!

=c.end();i++)

_________________;

};

classA{

protected:

intnVal;

public:

A(inti):

nVal(i){}

virtualvoidPrint(){cout<<"A:

:

Print:

"<

};

classB:

publicA{

public:

B(inti):

A(i){}

voidPrint(){cout<<"B:

:

Print:

"<

};

main()

{

__________________;

v.push_back(newA

(1));

v.push_back(newB

(2));

v.push_back(newB(3));

PrintAll(v);

}

/*

(*i)->Print()

vectorv

*/

 

5)6分

回文串指的是颠倒过来后还是与原串一样的字符串。

比如“abba”,“bcd232dcb”,都是回文串。

下面的程序运行后等待输入一串字符(字符个数小于4000),敲回车后程序判断输入的字符串是否是回文串,如果是,输出yes,否则,输出no。

比如输入

abba(回车)

则输出

yes

输入

abcd(回车)

则输出:

no

请填空。

#include

#include

charszWord[5000];

boolPalindrome(char*s,intnLen)

{

if(___________)

returntrue;

if(____________)

returnfalse;

returnPalindrome(___________);

}

main()

{

cin.getline(szWord,4990);

if(Palindrome(szWord,strlen(szWord)))

cout<<"yes";

else

cout<<"no";

}

/*

nLen<=1

s[0]!

=s[nLen-1]

s+1,nLen-2

*/

6)6分

输出结果是:

0

5

程序:

classA{

public:

intval;

A(____________){val=n;};//(a)

___________GetObj(){//(b)

return_________;//(c)

}

};

main(){

Aa;

cout<

a.GetObj()=5;

cout<

}

/*

intn

A&

*this

*/

 

四、编程题

1.(8分)程序员马克斯的程序风格和他的性格一样怪异。

很不幸他被开除后老板命令你接替他的工作。

马克斯走之前分愤然删除了他写的一个类模板MyMax中的代码,你只好将其补出来。

你只知道MyMax模板的作用与求数组或向量中的最大元素有关,而且下面程序的输出结果是:

5

136

请补出马克斯删掉的那部分代码。

该部分代码全部位于"//开头"和"//结尾"之间,别处一个字节也没有。

Bytheway,马克在空白处留下了以下三个条件:

1)不准使用除true和false以外的任何常量,并且不得假设true的值是1或任何值

2)不得使用任何库函数或库模板(包括容器和算法)

3)不得使用static关键字

你不想表现得不如马克斯,所以你只好遵守这三个条件。

#include

#include

#include

usingnamespacestd;

template

classMyMax

{

//开头

……

//结尾

};

classA{

public:

inti;

A(intn):

i(n){};

A(){};

};

booloperator<(constA&a1,constA&a2)

{

returna1.i

}

ostream&operator<<(ostream&o,constA&a)

{

o<

returno;

}

main()

{

Aa[5]={A

(1),A(5),A(3),A(4),A

(2)};

intb[9]={1,5,30,40,2,136,80,20,6};

in

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

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1