国家二级C++机试(操作题)模拟试卷519
基本操作题
1.请打开考生文件夹下的解决方案文件proj1,程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
value=63
number=1
注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。
#include<iostream>
using namespace std;
class MyClass{
int*p;
const int N;
public:
//ERROR*******found*******
MyClass(int val):N=1
{
p=new int;
*p=val;
}
//ERROR*******found*******
~MyClasS( ){delete*p;}
friend void print(MyClass&obj);
};
//ERROR*******found*******
void MyClass::print(MyClass&obj)
{
cout<<\\
(1)MyClass(int val):N(1)
(2)~MyClass( ){delete[ ]p;}
(3)void print(MyClass&obj)
解析:(1)主要考查考生对构造函数的掌握,在这里不能使用赋值语句。
(2)主要考查考生对析构函数的掌握,析构函数的delete语句要使用标识符“[ ]”,即delete[ ]p;。
(3)主要考查考生对友元函数的掌握,友元函数并不属于类,因此定义时前面不用加类名和作用域符号。
简单应用题
2.请打开考生文件夹下的解决方案文件proj2,其中定义了vehicle类,并派生出motorcar类和bicycle类。然后以motorcar和bicycle作为基类,再派生出motorcycle类。要求将vehicle作为虚基类,避免二义性问题。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
80
150
100
1
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include<iostream.h>
class vehicle
{
private:
int MaxSpeed;
int Weight;
public:
//**********found**********
vehicle(int maxspeed,int
weight):_______
~vehicle( ){};
int getMaxSpeed( ){returnMaxSpeed;}
int getWeight( ){returnWeight;}
};
//**********found**********
class bicycle:_______publicvehicle
{
private:
int Height;
public:
bicycle(int maxspeed,intweight,int height):vehicle(maxspeed,weight),Height(height){}
int getHeight( ){returnHeight};
};
//**********found**********
class motorcar:_______public vehicle
{
private:
int SeatNum;
publiC:
motorcar(intmaxspeed,int weight,in tseatnum):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<<a.getMaxSpeed( )<<endl;
cout<<a.getWeight( )<<endl;
cout<<a.getHeight( )<<endl;
cout<<a.getSeatNum( )<<
endl;
}
(1)MaxSpeed(maxspeed),Weight(weight){};
(2)virtual
(3)virtual
(4)public bicycle,public motorcar
解析:(1)主要考查考生对构造函数的掌握,构造函数使用初始化列表来对私有成员MaxSpeed和Weight初始化。
(2)主要考查考生对派生类的掌握,题目要求将vehicle作为虚基类,避免二义性问题。因此在这里添加virtual使vehicle成为虚基类。
(3)主要考查考生对派生类的掌握,题目要求以motorcar和bicycle作为基类,再派生出motorcycle类。在主函数中可以看到motorcycle类的实例a调用getHeight函数和getSeatNum函数,由此可知这两个基类都是公有继承,因此得出语句:public bicycle,public motorcar。
综合应用题
3.请打开考生文件夹下的解决方案文件proj3,其中声明的DataList类,是一个用于表示数据表的类。DataList的重载运算符函数operator+,其功能是求当前数据表与另一个相同长度的数据表之和;即它返回一个数据表,其每个元素等于相应两个数据表对应元素之和。请编写这个operator十函数。程序的正确输出应该是:
两个数据表:
1,2,3,4,5,6
3,4,5,6,7,8
两个数据表之和:
4,6,8,10,12,14
要求:
补充编制的内容写在“//********333********”与“//********666********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//DataList.h
#include<iostream>
using namespa
本文档预览:3600字符,共4256字符,源文件无水印,下载后包含无答案版和有答案版,查看完整word版点下载