2016计算机二级C++上机考前冲刺试题

  基本操作题(18分)

2016计算机二级C++上机考前冲刺试题

41、请使用VC6或使用【答题】菜单打开考生文件夹projl下的工程projl,此工程中含有一个源程碍文件 。其中位于每个注释“//ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:

ConstruCtor Called. The value is 10

Copy ConstruCtor Called. The value is 10

DestruCtor Called. DestruCtor Called. 注意:只修改注释“//ERROR ****found****”的下一行语句,不要改动程序中的其他内容。

//

#inClude ’using namespaCe std; Class MyClass{

publiC:

//ERROR**********found**********

MyClass(int i)

{value=i;Cout<<”ConstruCtor Called.” < //ERROR**********found********** MyClass(eonst MyClass P)

{

value = e;

eout<<”Copy ConstruCtor Called.”< }

void Print()

{Cout<<”The value is” < //ERROR**********found********* void-MyClass()

{Cout<<”DestruCtor Called.”< private:

int value;

}; int main()

{ MyChas objl

t();

MyClmss obj2(owl); t();

retum 0;

  简单应用题(24分)

42、请使用VC6或使用【答题】菜单打开考生文件夹pr092下的工程pros2。此工程中包含一个程序文件,其中有“部门”类Department和“职工”类Staff的定义,还有主函数main的定义。在主函数中定义了两个“职工”对象,他们属于同一部门。程序展示,当该部门改换办公室后,这两个人的办公室也同时得到改变。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:

改换办公室前:

职工号:0789姓名:张三部门:人事处办公室:521

职工号:0513姓名:李四部门:人事处办公室:521

改换办公室后:

职工号:0789姓名:张三部门:人事处办公室:311

职工号:0513姓名:李四部门:人事处办公室:311

注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。

#include

using namespace std;

class Department{ //“部门”类

public:

Department(const char*name,const char*office){

strcpy(this一>name,nanle);

//**********found**********

}

const char*getName()const{return name;}//返回部门名称

//**********found**********

const char*getOffice()const{________} //返回办公室房号

void changeOfficeTo(const char*office){ //改换为指定房号的另一个办公室

strcpy(this一>office,office);

}

private:

char name[20];//部门名称

char office[20];//部门所在办公室房号

};

class staff{//“职工”类

public:

//**********found**********

Staff(const char*my—id,const char木my_name,Department&my_dept):——{

strcpy(this一>staff id,my_id);

strcpy(this一>name,my_name);

}

const char*getlD()const{return staff_id;}

const char*getName()consl{return name;}

Department getDepartment()const{return dept;} char staff=id[10];//职工号

char name[20];//姓名

Department&dept;//所在部门

}; void showStaff(Staff&staff){

cout<<”职工号:”< cout<<”姓名:”< cout<<”部门:”< cout<<”办公室:”< int main(){

Department dept(”人事处”,”521”);

Staff Zhang(”0789”,”张三”,dept),Li(”0513”,”李四”,dept);

cout<<”改换办公室前:”< showStaff(Zhang); showStaff(Li);

//人事处办公室由521搬到311 //**********found********** ———————————————————————一

cout<<”改换办公室后:”< showStaff(Zhang); showStaff(Li);

return 0; }

  综合应用题(18分)

43、

请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中包含了类IntegerSet和主函数main的定义。一个IntegerSet对象就是一个整数的集合,其中包含0个或多个无重复的整数;为了便于进行集合操作,这些整数按升序存放在成员数组elem的前若干单元中。成员函数add的作用是将一个元素添加到集合中(如果集合中不存在该元素),成员函数remove从集合中删除指定的元素(如果集合中存在该元素)。请编写成员函数remove。在main函数中给出了一组测试数据,此时程序的正确输出结果应为:

2 3 4 5 27 28 31 66 75

2 3 4 5 6 27 28 31 56 75