计算机二级《C++》上级考试试题及答案2017
一、基本操作题

1请使用VC6或使用【答题】菜单打开考生文件夹projl下的工程projl。程序中位于每个“/pic/p>
Name:Smith Age:21 ID:99999 CourseNum:12
Record:970
注意:只修改每个“/pic/p>
#include
using namespace std;
class Studentlnfo
{
protected:
/pic/p>
char Name;
int Age;
int ID:
int CourseNum;
float Record;
public:
Studentlnfo(char*name,int Age,int ID,int coumeNum,float record);
/pic/p>
void~Studentlnfo(){}
float AverageRecord(){
return Record/CourseNum;
}
void show()const{
cout<<”Name:”<
<<”CourseNum:”<
}
};
/pic/p>
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
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“/pic/p>
#include
class vehicle
{
private:
int MaxSpeed;
int Weight;
public:
/pic/p>
vehicle(int maxspeed,int weight):——
~vehicle(){};
int getMaxSpeed(){return MaxSpeed;}
int getWeight(){retum Weight;}
};
/pic/p>
class bicycle:——public vehicle
{
private:
int Height;
public:
bicycle(int maxspeed,int weight,int height):vehicle(maxspeed,weight),Height(height){}
int getHeight(){retum Height;};
};
/pic/p>
class motorcar:——public vehicle
{
private:
int SeatNum;
public:
motorcar(int maxspeed。int weight,int seatnum):vehicle(maxspeed,weight),SeatNum(seatnum){}
int getSeatNum(){return SeatNum;};
};
/pic/p>
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!
补充编制的`内容写在“/pic/pic/p>
不得修改程序的其他部分。
注意:
程序最后调用writeToFil。函数,使用另一组不同的测试数据,将不同的运行结果输出到文件0ut•dat中。
输出函数writeToFile已经编译为obj文件。
/pic/p>
#include
#include
using namespace std;
class MiniString
{
pubhc:
friend。8tream&operator<<(oatream&output,const MiniString&s)/pic/p>
{ output<
friend istream&operator>>(istream&input,MiniString&8)/pic/p>
{ char temp[100];/pic/p>
temp[0]=’\0’:/pic/p>
input>>setw(100)>>temp;
int inLen=strlen(temp);/pic/p>
if(inLen!=0)
}
s.1ength=inLen;/pic/p>
if(s.sPtr!=0)delete[]s.sPtr;/pic/p>
s.sPtr=Hew char[s.1ength+1];
strcpy(s.sPtr,temp);/pic/p>
}
else s.sPtr[0]=’\0’;/pic/p>
retum input;
}
void modString(const char}string2)/pic/p>
{
if(strin92 1=0)/pic/p>
{
if(strlen(strin92)!=length)
{
length=strlen(strin92);
delete[]sPtr;
sPtr=new char[1ength+1];/pic/p>
}
strcpy(sPtr,strin92);
}
else sPtr[0]=’\0’;/pic/p>
}
MiniString&operator=f const MiniString&otherString);
MiniString(corot char*s=""):lengtll((s!=0)?strlen(s):0)/pic/p>
{
sPtT=0:
if(1ength[=0)
setString(S);
}
~MiniString()/pic/p>
{delete[]sPtr;}
private:
int length;/pic/p>
char*sPtr;/pic/p>
void setString(const char*string2)/pic/p>
{
sPtr=Dew char[str|en(string2)+1];/pic/p>
if(stnIl92!=0)strcpy(sPtr,string2);/pic/p>
else slur[0]='\0';/pic/p>
}
};
/pic/p>
#'include
#include
using namespace std;
#include”proj3.h”
MiniString&MiniStrin9::0perator=(const MiniString&otherString)
{/pic/p>
/pic/p>
/pic/p>
}
int main()
{
MiniSu'ing strl(”Heuo!”),str2;
void writeToFile(const char*);
sir2=strl;/pic/p>
str2.modString("Happy new year!");
cout<
cout<
writeToFile("");
return 0;
}
参考解析:
length=otherString.1ength; /pic/pic/pic/p>
更多计算机二级C++相关试题分享:
【计算机二级《C++》上级考试试题及答案】相关文章:
计算机二级C++考试试题及答案10-13
全国计算机二级考试C++精选试题及答案12-28
计算机二级c++试题及答案12-24
计算机二级考试C++上机考试试题及答案02-08
2016年计算机二级考试C++试题及答案03-17