国家二级C++机试(操作题)模拟试卷361
基本操作题
1.请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程pmj1,其中有点类Point和线段类Line和主函数main的定义,程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出应为:
p1=(8,4)p2=(3,5)
注意:只修改两个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。
#include
#include
using namespace std;
clasS Point{
double x,Y;
public:
Point(double x=0.0,double Y=0.0)
//ERROR**********found**********
{X=x;Y=Y;)
double getX()const(return X;)
double getY()const{return y;}
//ERROR**********found**********
void show()const{cout<<’(’<<X<
<’,’<<Y<<’)’)
};
clasS Line{
Point p1,p2;
public:
Line(Point pt1,Point pt2)
//ERROR**********found**********
{pt1=p1;pt2=p2;}
Point getP1()const{return p1;}
Point getP2()const{return p2;)
};
int main(){
Line line(Point(8,4),Point(3,5));
cout<<”p1=”;
line.getP1().show();
cout<<”p2=”;
line.getP2().show();
cout<<endl;
return 0;
}
(1):x(x),y(y){}或{this一>x=x,this一>y=y;}
(2)void show()const{cout<<’(’<<x<<’,’<<y<<’)’;}
(3):p1(pt1),p2(pt2){}或{p1=pt1;p2=pt2}
解析:(1)主要考查考生对构造函数的掌握,因为形参名和私有成员名称一样,因此不能直接赋值,在这里使用成员列表初始化,也可以使用this指针赋值。
(2)主要考查考生对语句基本语法的掌握,根据语句:
void show()eonst{eout<<’(’<<x<<’,’<<y<<’)’}。可看出函数体内并没有“;”作为eout语句的结束符,因此程序错误。
(3)主要考查考生对构造函数的掌握,形参是pt1和pt2,这里写反了,也可以使用成员列表初始化法,可以避免这种错误。
简单应用题
2.请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中有矩阵基类MatrixBase、矩阵类Matrix和单位阵UnitMatrix的定义,还有main函数的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
1 0 0 0 0 0
0 1 0 0 0 0
O 0 1 0 0 O
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include
using namespace std;
//矩阵基础类,一个抽象类
class MatrixBase{
int rows,cols;
public:
MatrixBase(int rows, int cols):
rows(rows),cols(cols){)
int getRows()const {return rows;}
//矩阵行数
int getCols()const{return cols;}
//矩阵列数
virtual double getElement(int r,
int C)const=0;//取第i个元素的值
void show()const{
//分行显示矩阵中所有元素
for(int i=0;i<rows;i++){
cout<<endl;
for(int j=0;j<cols;j++)
//**********found**WW******
cout<<_____________<<””;
}
}
}
//矩阵类
class Matrix:public MatrixBase{
double*val;
public:
//**********found**********
Matrix(int rOWS,int cols,double m
[]=NULL):________{
//**********found**********
val=________;
for(int i=0;i<rows*cols;i++)
val[i]=(m==NULL?0.0:m[i]);
}
一Matrix(){delete[]val;)
double getElement(int r,int C)const
{return val[r*getCols()+C];)
};
//单位阵(主对角线元素都是1,其余元素都是
0的方阵)类
class UnitMatrix:public MatrixBase{
public:
UnitMatrix(int rows):MatrixBase
(rows,rows){)
//单位阵行数列数相同.
double getElement(int r,int C)
const{
//********** found**********
if(________)re
本文档预览:3600字符,共9174字符,源文件无水印,下载后包含无答案版和有答案版,查看完整word版点下载