试卷详情
-
C++语言笔试-10
-
[简答题]请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,其中有矩阵基类MatrixBase、矩阵类Matnx和单位阵UnitMatrix的定义,还有mam函数的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
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
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include<iostream>
using namespace std;
//矩阵基础类,一个抽象类
class MatrixBase
int rows,cols;
public:
MatrixBase(int rows,int cols):rows(rows),cols(cols)
int getRows( )constreturn rows;
//矩阵行数
int gatCols( )constreturn 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**********
COUt<<______<<" ";
;
//矩阵类
class Matrix:public MatrixBase
double*val;
public:
//**********found**********
Matrix(i
-
[简答题]请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程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 namespace std;
class Datalist //数据表类
int len;
double*d;
public:
DataList(int len,double data[]=
DataList(Datalist &data);
int length ( ) constreturn len;
double getElement(int i)const return d[i];
Datalist operator+(const DataList&list)const; //两个数据表求和
void show ( ) const; //显示数据表void writeToFile(char*,constDataList&);
//main.cpp
#include"Datalist.h"
DataList::DataList(int len,doubledata[]):len(len)
d=new double[len];
for(int i=0;i<len;i++)
d[i]=(data==N
-
[简答题]请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“//ERROR ****found****”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为:
Constructor called.
The value is 10
Copy constructor called.
The value is 10
Destructor called.
Destructor called.
注意:只修改注释“//ERROR ****found****”的下一行语句,不要改动程序中的其他内容。
//proj1.cpp
#include <iostream>
using namespace std;
class MyClass
public:
//ERROR **********found**********
MyClass(int i)
value=i;cout<<"Construc-tor called."<<endl;
//ERROR **********found**********
MyClass(const MyClass p)
value=p.value;
cout<<"Copy constructorcalled."<<endl;
void Print ( )
cout<<"The value is"<<value<<endl;
//ERROR **********found**********
void ~MyClass( )
cout<<"Destructor called."
<<endl;
private:
int value;
;
int main( )
MyClass obj1;
obj1.Print( );
MyClass obj2(obj1);
obj2.Print( );
return