国家二级C++机试(操作题)模拟试卷625
基本操作题
1.请打开考生文件夹下的解决方案文件proj1,此工程包含一个源程序文件proj1.cpp。文件中将表示数组元素个数的常量Size定义为4,并用int类型对类模板进行了实例化。文件中位于每个注释“//ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
1 2 3 4
注意:模板参数名用T。只修改注释“//ERROR ********found********”的下—行语句,不要改动程序中的其他内容。
//proj1.cpp
#include<iostream>
using namespace std;
//将数组元素个数Size定义为4
//ERROR *******found*******
const int Size;
template<typename T>
class MyClass
{
public:
MyClass(T*p)
{
for(int i=0;i<Size;i++)
array[i]=p[i];
}
void Print();
private:
T array[Size];
};
template<typename T>
//ERROR ******* found *******
void MyClass::Print()
{
for(int i=0;i<Size;i++)
cout<<array[i]<<’\t’;
}
int main()
{
int intArray[Size]={1,2,3,4};
//ERROR ******* found *******
MyClass<double>obj ( intArray);
obj.Print();
cout<<endl;
return 0;
}
(1)const int size=4;
(2)void MyClass<T>::Print()
(3)MyClass<int>obj(intArray);
解析:(1)主要考查考生对const变量的掌握,因为const变量不能修改,所以在定义的同时必须初始化。
(2)主要考查考生对模板类的成员函数定义的掌握,因为MyClass类是模板类,所以在定义该函数时要加上模板标识符“<T>”,即语句void MyClass<T>::Print()。
(3)主要考查考生对模板类构造函数的调用的理解,从上一条语句int intArray[Size]={1,2,3,4};中可以知道intArray为int型,因此定义obj时要使用<int>,即MyClass<int>obj(intArray);。
简单应用题
2.请打开考生文件夹下的解决方案文件proj2,其中有类Point(“点”)、Rectangle(“矩形”)和Circle(“圆”)的定义。在程序所使用的平面坐标系统中,x轴的正方向是水平向右的,y轴的正方向是竖直向下的。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应该是:
–圆形———-
圆心=(3,2)
半径=1
面积=3.14159
–外切矩形——
左上角=(2,1)
右下角=(4,3)
面积 =4
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include <iostream>
#include<cmath>
using namespace std;
//平面坐标中的点
//本题坐标系统中,x轴的正方向水平向右,y轴的正方向竖直向下。
class Point{
public:
Point(double x=0.0,double y=0.0):x_(x),y_(y){}
double getX()const{return x_;}
double getY()const{return y_;}
vood setX(double x){x_ =x;}
void setY(double y){y_ =y;}
private:
double x_; //x坐标
double y_; //y坐标
};
//矩形
class Rectangle{
public:
Rectangle(Point p,int w,int h)
:poLnt(p),width(w),height(h){}
double area()const//矩形面积
{
return width * height;
}
Point topLeft() const//左上角顶点
{
return point;
}
Point bottomRight()const
//右下角顶点(注:y轴正方向竖直向下)
{
//**********found**********
return Point(__________);
}
private:
Point point;//左上角顶点
double width;//水平边长度
double he~ght;//垂直边长度
};
//圆形
class Cirole{
public:
Circle(Point P,double r):center(p),radius(r){}
Rectangle boundingBox ( )const;
//外切矩形
double area()const//圆形面积
本文档预览:3600字符,共5713字符,源文件无水印,下载后包含无答案版和有答案版,查看完整word版点下载