- 相關(guān)推薦
計(jì)算機(jī)二級(jí)《C++》上級(jí)考試試題及答案2017
一、基本操作題
1請(qǐng)使用VC6或使用【答題】菜單打開考生文件夾projl下的工程projl。程序中位于每個(gè)“//ERROR****found料****之后的一行語句有錯(cuò)誤,請(qǐng)加以改正。改正后程序的輸出結(jié)果應(yīng)為:
Name:Smith Age:21 ID:99999 CourseNum:12
Record:970
注意:只修改每個(gè)“//ERROR****found****”下的那一行,不要改動(dòng)程序中的其他內(nèi)容。
#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)
二、簡(jiǎn)單應(yīng)用題
2請(qǐng)使用VC6或使用【答題】菜單打開考生文件夾proj2下的工程proj2,其中定義了vehiele類,并派生出motorcar類和bicycle類。然后以motorcar和bicycle作為基類,再派生出motorcycle類。要求將Vehicle作為虛基類,避免二義性問題。請(qǐng)?jiān)诔绦蛑械臋M線處填寫適當(dāng)?shù)拇a并刪除橫線,以實(shí)現(xiàn)上述類定義。此程序的正確輸出結(jié)果應(yīng)為:
80
150
100
1
注意:只能在橫線處填寫適當(dāng)?shù)拇a,不要改動(dòng)程序中的其他內(nèi)容,也不要?jiǎng)h除或移動(dòng)“//****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
更多計(jì)算機(jī)二級(jí)C++相關(guān)試題分享:
1.2017年3月計(jì)算機(jī)二級(jí)C++考前練習(xí)題及答案
2.2017計(jì)算機(jī)二級(jí)C++模擬題及答案
4.2017年3月計(jì)算機(jī)二級(jí)C++練習(xí)題及答案
5.2017計(jì)算機(jī)二級(jí)C++基礎(chǔ)習(xí)題及答案
6.2017計(jì)算機(jī)二級(jí)C++考點(diǎn)習(xí)題及答案
7.2017年12月計(jì)算機(jī)二級(jí)C++考前練習(xí)題及答案
8.2017計(jì)算機(jī)二級(jí)C++強(qiáng)化習(xí)題及答案
9.2017計(jì)算機(jī)二級(jí)C++強(qiáng)化習(xí)題及答案
10.計(jì)算機(jī)二級(jí)考試C++練習(xí)及答案
三、綜合應(yīng)用題
3請(qǐng)使用VC6或使用【答題】菜單打開考生文件夾proj3下的工程文件proj3。本題創(chuàng)建一個(gè)小型字符串類,字符串長(zhǎng)度不超過l00。程序文件包括pmj3.h、proj3.cpp、writeToFile.obj。補(bǔ)充完成重載賦值運(yùn)算符函數(shù),完成深復(fù)制功能。
屏幕上輸出的正確結(jié)果應(yīng)該是:
Hello!
Happy new year!
補(bǔ)充編制的`內(nèi)容寫在“//**********333**********”與“//**********666**********”兩行之間。
不得修改程序的其他部分。
注意:
程序最后調(diào)用writeToFil。函數(shù),使用另一組不同的測(cè)試數(shù)據(jù),將不同的運(yùn)行結(jié)果輸出到文件0ut•dat中。
輸出函數(shù)writeToFile已經(jīng)編譯為obj文件。
//proj3.h
#include
#include
using namespace std;
class MiniString
{
pubhc:
friend。8tream&operator<<(oatream&output,const MiniString&s)//重載流插入運(yùn)算符
{ output<
friend istream&operator>>(istream&input,MiniString&8)//重載流提取運(yùn)算符
{ char temp[100];//用于輸入的臨時(shí)數(shù)組
temp[0]=’\0’://初始為空字符串
input>>setw(100)>>temp;
int inLen=strlen(temp);//輸入字符串長(zhǎng)度
if(inLen!=0)
}
s.1ength=inLen;//賦長(zhǎng)度
if(s.sPtr!=0)delete[]s.sPtr;//避免內(nèi)存泄漏
s.sPtr=Hew char[s.1ength+1];
strcpy(s.sPtr,temp);//如果8不是空指針,則復(fù)制內(nèi)容
}
else s.sPtr[0]=’\0’;//如果s是空指針,則為空字符串
retum input;
}
void modString(const char}string2)//更改字符串內(nèi)容
{
if(strin92 1=0)//如果strin92不是空指針,則復(fù)制內(nèi)容
{
if(strlen(strin92)!=length)
{
length=strlen(strin92);
delete[]sPtr;
sPtr=new char[1ength+1];//分配內(nèi)存
}
strcpy(sPtr,strin92);
}
else sPtr[0]=’\0’;//如果string2是空指針,則為空字符串
}
MiniString&operator=f const MiniString&otherString);
MiniString(corot char*s=""):lengtll((s!=0)?strlen(s):0)//構(gòu)造函數(shù)
{
sPtT=0:
if(1ength[=0)
setString(S);
}
~MiniString()//析構(gòu)函數(shù)
{delete[]sPtr;}
private:
int length;//字符串長(zhǎng)度
char*sPtr;//指向字符串起始位置
void setString(const char*string2)//輔助函數(shù)
{
sPtr=Dew char[str|en(string2)+1];//分配內(nèi)存
if(stnIl92!=0)strcpy(sPtr,string2);//如果string2不是空指針,則復(fù)制內(nèi)容
else slur[0]='\0';//如果string2是空指針,則為空字符串
}
};
//pwj3.cpp
#'include
#include
using namespace std;
#include”proj3.h”
MiniString&MiniStrin9::0perator=(const MiniString&otherString)
{//重載賦值運(yùn)算符函數(shù)。提示:可以調(diào)用輔助函數(shù)setString
//*************333**********
//*************666**********
}
int main()
{
MiniSu'ing strl(”Heuo!”),str2;
void writeToFile(const char*);
sir2=strl;//使用重載的賦值運(yùn)算符
str2.modString("Happy new year!");
cout<
cout<
writeToFile("");
return 0;
}
參考解析:
length=otherString.1ength; //把對(duì)象字符串0therString的長(zhǎng)度賦值給變量length setString(otherString.sPtr); //調(diào)用函數(shù)setstring,實(shí)現(xiàn)給類變量sptr分配空間,以及逐個(gè)把對(duì)象0therstring字符串的值復(fù)制到sptr中 return}this: //返回被賦值的對(duì)象
更多計(jì)算機(jī)二級(jí)C++相關(guān)試題分享:
1.2017年3月計(jì)算機(jī)二級(jí)C++考前練習(xí)題及答案
2.2017計(jì)算機(jī)二級(jí)C++模擬題及答案
4.2017年3月計(jì)算機(jī)二級(jí)C++練習(xí)題及答案
5.2017計(jì)算機(jī)二級(jí)C++基礎(chǔ)習(xí)題及答案
6.2017計(jì)算機(jī)二級(jí)C++考點(diǎn)習(xí)題及答案
7.2017年12月計(jì)算機(jī)二級(jí)C++考前練習(xí)題及答案
8.2017計(jì)算機(jī)二級(jí)C++強(qiáng)化習(xí)題及答案
9.2017計(jì)算機(jī)二級(jí)C++強(qiáng)化習(xí)題及答案
10.計(jì)算機(jī)二級(jí)考試C++練習(xí)及答案
【計(jì)算機(jī)二級(jí)《C++》上級(jí)考試試題及答案】相關(guān)文章:
計(jì)算機(jī)二級(jí)考試C++試題及答案09-15
計(jì)算機(jī)二級(jí)C++考試試題及答案10-29
計(jì)算機(jī)二級(jí)c++試題及答案08-27
全國(guó)計(jì)算機(jī)二級(jí)考試C++精選試題及答案08-17
2016計(jì)算機(jī)二級(jí)《C++》考試試題及答案09-02
計(jì)算機(jī)二級(jí)考試C++上機(jī)考試試題及答案11-09
2016計(jì)算機(jī)二級(jí)考試C++備考試題及答案07-16