荟萃馆

位置:首页 > 计算机 > 计算机二级

2016计算机二级《C++》上机练习题及答案

  一、基本操作题

ALt="2016计算机二级《C++》上机练习题及答案" title="2016计算机二级《C++》上机练习题及答案">

1请使用VC6或使用【答题】菜单打开考生文件夹projl下的工程projl,其中有枚举DOGCOLOR、狗类Dog和主函数main的定义。程序中位于每个“//ERROR ****found****”下的语句行有错误,请加以改正。改正后程序的输出结果应该是:

There is a white dog named Hoh0. There is a blaCk dog named Haha. There is a motley dog named Hihi. 注意:只修改每个//ERROR****found****女料下的那一行,不要改动程序中的其他内容。

#inClude using namespaCe std; //狗的颜色:黑、白、黄、褐、花、其他

enum DOGCOLOR{BLACK,WHITE,YELLOW,BROWN,PIEBALD,OTHER}; Class Dog{//狗类

DOGCOLOR Color; Char name[20]; statiC int Count; publiC:

Dog(Char nalTle[],DOGCOLOR Color){ strCpy(this一>name,name);

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

strCpy(this一>Color,Color);

} 1 DOGCOLOR getColor()Const{retum Color;}

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

Const Char * 9etName()Const{return*finin9;} Const Char * 9etColorString()Const{

switCh(Color){

Case BLACK:return”blaCk“; Case WHITE:return”white”:

Case YELLOW:return’"yellow”: Case BROWN:return”brown”; ease PIEBALD:return”piebald”:

}

return”motley”;

}

void show()Const{ . Cout<<”There is a”<

} }; int main(){

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

Dog dogl(”Hoh0”,WHITE),dog2(”Haha”,BLACK);d0g3(”Hihi”,0THER); ();

(); (); return 0;

}

  参考解析:

(1)this->color=color:

(2)consL char getName()eonst{return*name;}

(3)Dog dogl(”Hoho”,WHITE)。dog2(”Haha”,

BLACK),dog3(”Hihi”,OTHER);

  二、简单应用题

2请使用VC6或使用【答题】菜单打开考生文件夹proj2下的'工程proj2。此工程中包含一个源程序文件,其中有“房间”类Room及其派生出的“办公室”类0ffice的定义,还有主函数main的定义。请在程序中“//****found****”下的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:

办公室房间号:308

办公室长度:5.6

办公室宽度:4.8

办公室面积:26.88

办公室所属部门:会计

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

#include

using namespaee std;

class Room{ //“房间”类

int room_n0;//房间号

double length;//房间长度(m)

double width;//房间宽度(n1)

public:

Room(int the—room—n0,double the~length,double the—width):loom—no(the—room—no),length(the—

length),width(the—width){}

int theRoomNo()eonst{return iDonl—n0;}

//返回房间号

double theLength()const{return length;}//返回房间长度

double theWidth()const{return width;}//返回房间宽度

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

double theArea()const{_______________}//返回房间面积(矩形面积)

};

class Office:public Room{//“办公室”类

char*depart;//所属部门

public:

Office(int the_room_n0,double the_length,double the_width,eonst char木the_depart)

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

:_______________{

depart=new char[strlen(the depart)+1];

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

strcpy(_______________);

}

~Office(){delete[]depart;}

const char*theDepartment()const{return depart;}//返回所属部门

};

int main(){

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

Office_______________;

cout<<”办公室房间号:”<

<<”办公室长度:”<

<<”办公室宽度:”<

<<”办公室面积:”<

<<”办公室所属部门:”<

return 0;

}

  参考解析:

(1)returtl length*width;

(2)Room(the_room_no,the_lengih,the_width)

(3)depart,the_depart

(4)an_office(308,5.6,4.8,”会汁科”)