- 相關推薦
計算機二級《C++》上級考試試題及答案2017
一、基本操作題
1請使用VC6或使用【答題】菜單打開考生文件夾projl下的工程projl。程序中位于每個“//ERROR****found料****之后的一行語句有錯誤,請加以改正。改正后程序的輸出結果應為:
Name:Smith Age:21 ID:99999 CourseNum:12
Record:970
注意:只修改每個“//ERROR****found****”下的那一行,不要改動程序中的其他內容。
#include
using namespace std;
class Studentlnfo
{
protected:
//ERROR********************found****************
char Name;
int Age;
int ID:
int CourseNum;
float Record;
public:
Studentlnfo(char*name,int Age,int ID,int coumeNum,float record);
//ERROR ********************found********************
void~Studentlnfo(){}
float AverageRecord(){
return Record/CourseNum;
}
void show()const{
cout<<”Name:”<
<<”CourseNum:”<
}
};
//ERROR ******************found**************
StudentInfo StudentInfo(char*Name,int Age,int ID,int CourseNum,float Record)
{
Name=name;
Age=age;
this一>ID=ID:
CourseNum=courseNum:
Record=record;
}
int main()
{
Studentlnfo st(’’Smith”,21,99999,12,970);
st.show();
return 0;
}
參考解析:
(1)char*Name;
(2)~Studentlnfo(){}
(3)Studentlnf0::Studentlnfo(char*name,int age,,int ID,int eourseNum,float record)
二、簡單應用題
2請使用VC6或使用【答題】菜單打開考生文件夾proj2下的工程proj2,其中定義了vehiele類,并派生出motorcar類和bicycle類。然后以motorcar和bicycle作為基類,再派生出motorcycle類。要求將Vehicle作為虛基類,避免二義性問題。請在程序中的橫線處填寫適當的代碼并刪除橫線,以實現上述類定義。此程序的正確輸出結果應為:
80
150
100
1
注意:只能在橫線處填寫適當的代碼,不要改動程序中的其他內容,也不要刪除或移動“//****found****”。
#include
class vehicle
{
private:
int MaxSpeed;
int Weight;
public:
//*************found************
vehicle(int maxspeed,int weight):——
~vehicle(){};
int getMaxSpeed(){return MaxSpeed;}
int getWeight(){retum Weight;}
};
//****************found************
class bicycle:——public vehicle
{
private:
int Height;
public:
bicycle(int maxspeed,int weight,int height):vehicle(maxspeed,weight),Height(height){}
int getHeight(){retum Height;};
};
//*******************found**************
class motorcar:——public vehicle
{
private:
int SeatNum;
public:
motorcar(int maxspeed。int weight,int seatnum):vehicle(maxspeed,weight),SeatNum(seatnum){}
int getSeatNum(){return SeatNum;};
};
//*****************found***************
class motorcycle:——
{
public:
motorcycle(int maxspeed,int weight,int height):vehicle(maxspeed,weight),bicycle(maxspeed,weight,
height),motorcar(maxspeed,weight,1){}
};
void main()
{
motorcycle a(80,150,100);
cout<
cout<
cout<
cout<
}
參考解析:
(1)MaxSpeed(maxspeed),Weight(weight){f;
(2)virtual
(3)virtua1
(4)public bicycle,public motorcar
更多計算機二級C++相關試題分享:
三、綜合應用題
3請使用VC6或使用【答題】菜單打開考生文件夾proj3下的工程文件proj3。本題創建一個小型字符串類,字符串長度不超過l00。程序文件包括pmj3.h、proj3.cpp、writeToFile.obj。補充完成重載賦值運算符函數,完成深復制功能。
屏幕上輸出的正確結果應該是:
Hello!
Happy new year!
補充編制的內容寫在“//**********333**********”與“//**********666**********”兩行之間。
不得修改程序的其他部分。
注意:
程序最后調用writeToFil。函數,使用另一組不同的測試數據,將不同的運行結果輸出到文件0ut•dat中。
輸出函數writeToFile已經編譯為obj文件。
//proj3.h
#include
#include
using namespace std;
class MiniString
{
pubhc:
friend。8tream&operator<<(oatream&output,const MiniString&s)//重載流插入運算符
{ output<
friend istream&operator>>(istream&input,MiniString&8)//重載流提取運算符
{ char temp[100];//用于輸入的臨時數組
temp[0]=’\0’://初始為空字符串
input>>setw(100)>>temp;
int inLen=strlen(temp);//輸入字符串長度
if(inLen!=0)
}
s.1ength=inLen;//賦長度
if(s.sPtr!=0)delete[]s.sPtr;//避免內存泄漏
s.sPtr=Hew char[s.1ength+1];
strcpy(s.sPtr,temp);//如果8不是空指針,則復制內容
}
else s.sPtr[0]=’\0’;//如果s是空指針,則為空字符串
retum input;
}
void modString(const char}string2)//更改字符串內容
{
if(strin92 1=0)//如果strin92不是空指針,則復制內容
{
if(strlen(strin92)!=length)
{
length=strlen(strin92);
delete[]sPtr;
sPtr=new char[1ength+1];//分配內存
}
strcpy(sPtr,strin92);
}
else sPtr[0]=’\0’;//如果string2是空指針,則為空字符串
}
MiniString&operator=f const MiniString&otherString);
MiniString(corot char*s=""):lengtll((s!=0)?strlen(s):0)//構造函數
{
sPtT=0:
if(1ength[=0)
setString(S);
}
~MiniString()//析構函數
{delete[]sPtr;}
private:
int length;//字符串長度
char*sPtr;//指向字符串起始位置
void setString(const char*string2)//輔助函數
{
sPtr=Dew char[str|en(string2)+1];//分配內存
if(stnIl92!=0)strcpy(sPtr,string2);//如果string2不是空指針,則復制內容
else slur[0]='\0';//如果string2是空指針,則為空字符串
}
};
//pwj3.cpp
#'include
#include
using namespace std;
#include”proj3.h”
MiniString&MiniStrin9::0perator=(const MiniString&otherString)
{//重載賦值運算符函數。提示:可以調用輔助函數setString
//*************333**********
//*************666**********
}
int main()
{
MiniSu'ing strl(”Heuo!”),str2;
void writeToFile(const char*);
sir2=strl;//使用重載的`賦值運算符
str2.modString("Happy new year!");
cout<
cout<
writeToFile("");
return 0;
}
參考解析:
length=otherString.1ength; //把對象字符串0therString的長度賦值給變量length setString(otherString.sPtr); //調用函數setstring,實現給類變量sptr分配空間,以及逐個把對象0therstring字符串的值復制到sptr中 return}this: //返回被賦值的對象
更多計算機二級C++相關試題分享:
【計算機二級《C++》上級考試試題及答案】相關文章:
全國計算機二級考試C++鞏固試題與答案10-03
計算機二級C++模擬試題及答案09-22
全國計算機二級《C++》上機試題及答案08-15
計算機二級考試《Java》試題及答案09-04
計算機二級考試MySQL試題及答案10-23
計算機二級考試word試題及答案09-10
計算機二級考試WEB試題及答案10-22
計算機二級考試Java試題及答案10-24