2016计算机二级C++上机考前冲刺试题
基本操作题(18分)

41、请使用VC6或使用【答题】菜单打开考生文件夹projl下的工程projl,此工程中含有一个源程碍文件 projl.epp。其中位于每个注释“//ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
ConstruCtor Called. The value is 10
Copy ConstruCtor Called. The value is 10
DestruCtor Called. DestruCtor Called. 注意:只修改注释“//ERROR ****found****”的下一行语句,不要改动程序中的其他内容。
//pwjl.Cpp
#inClude ’using namespaCe std; Class MyClass{
publiC:
//ERROR**********found**********
MyClass(int i)
{value=i;Cout<<”ConstruCtor Called.” < //ERROR**********found********** MyClass(eonst MyClass P)
{
value = P.value;
eout<<”Copy ConstruCtor Called.”< }
void Print()
{Cout<<”The value is” < //ERROR**********found********* void-MyClass()
{Cout<<”DestruCtor Called.”< private:
int value;
}; int main()
{ MyChas objl
owl.Print();
MyClmss obj2(owl); obj2.Print();
retum 0;
简单应用题(24分)
42、请使用VC6或使用【答题】菜单打开考生文件夹pr092下的工程pros2。此工程中包含一个程序文件main.cpp,其中有“部门”类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
2 3 4 5 6 19 27 28 31 66 75
3 4 5 6 19 27 28 31 66 75
3 4 5 6 19 27 28 31 66 75
要求:
补充编制的内容写在“//***********333***********”与“//***********666***********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//IntegorSet.h
#ifndef INTEGERSET
#define INTEGERSET
#include
using namespace std;
const int MAXELEMENTS=100;
//集合最多可拥有的元素个数
class IntegerSet{
int elem[MAXELEMENTS];
//用于存放集合元素的数组
int counter; //用于记录集合中元素个数的计数器
puhlic:
IntegerSet():counter(0){}
//创建一个空集合
IntegerSet(int data[],int size);
//利用数组提供的数据创建一个整数集合
void add(int element);
//添加一个元素到集合中
void remeve(int element);
//删除集合中指定的元素
int getCount()const{return counter;}
//返回集合中元素的个数
int getElement(int i)const{retum elem[i];}//返回集合中指定的元素
void show()const;
};
void WriteToFile(char*);
#endif
//main.cpp
#include”IntegerSet.h”
#include
IntegerSet::IntegerSet(int data[],int size):counter(0){
for(int i=0;i add(data[i]);
}
}
void IntegerSet::add(int element){
int j;
//从后往前寻找第一个小于等于element的元素
for(j=counter;j>0;j-)
if(element>=elem[j一1])break;
//如果找到的是等于element的元素,说明要添加的元素已经存在,直接返回
if(j>0)
if(element==elem[j-1])return;
//如果找到的是小于element的元素,j就是要添加的位置
//该元素及其后面的元素依次后移,腾出插入位置
for(int k=counter;k>j;k一)
elem[k]=elem[k一1];
elem[j]=element;//将element插入到该位置
counter++; //计数器加l
}
void IntegerSet::remove(int element){
//***************333***************
//***************666***************
void IntegerSet::show()const{
for(int i=0;i cout< cout< }
int main(){
int d[]={5,28,2,4,5,3,2,75,27,66,31};
IntegerSet S(d,11);S.show();
S.add(6); s.show();
S.add(19); S.show();
S.remove(2); s.show();
S.add(4); S.show();
writeToFile(””);
return 0;
}
【计算机二级C++上机考前冲刺试题】相关文章:
计算机二级《C++》上机考前冲刺试题06-25
计算机二级考试C++考前冲刺试题10-13
计算机二级《C++》上机试题及答案08-12
2016计算机二级《C++》上机冲刺题09-05
2016计算机二级C++上机试题及答案08-02
二级C++上机冲刺题及答案解析10-10
全国计算机二级《C++》上机试题及答案08-15