當前位置:才華齋>計算機>計算機二級>

2016計算機二級《C++》上機練習題及答案

計算機二級 閱讀(5.98K)

  一、基本操作題

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,”會汁科”)