1、二级 C+分类模拟 122 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“/ ERROR *found*”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: Constructor called. The value is 10 Copy constructor called. The value is 10 Destructor called. Destructor called. 注意:
2、只修改注释“/ERROR *found*”的下一行语句,不要改动程序中的其他内容。 / proj1.cpp #include iostream using namespace std; class MyClass public: / ERROR *found* MyClass(int i) value = i; cout “Constructor called.“ endl; / ERROR *found* MyClass(const MyClass p) value = p.value; cout “Copy constructor called.“ endl; void Print() c
3、out “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 0; (分数:30.00)_二、简单应用题(总题数:1,分数:30.00)2.请使用 VC6 或使用【答题】菜单打开 proj2 下的工程 proj2,其中有矩阵基类 MatrixBase、矩阵类Matrix
4、 和单位阵 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 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 in
5、t 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 =
6、 0; j cols; j+) / *found* 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+) vali = (m=NULL? 0.0:mi); Matrix() delete val; double getElement (int r, int c) constreturn v
7、alr * getCols() +c;) ; /单位阵(主对角线元素都是 1,其余元素都是 0 的方阵)类 class UnitMatrix: public MatrixBase public: UnitMatrix (int rows): MatrixBase(rows, rows) /单位阵行数列数相同 double getElement (int r, int c) const / *found* if(_) return 1.0; return 0.0; ; int main() MatrixBase * m; double d 5 = 1,2, 3, 4,5, 2,3,4,5,6,
8、3,4,5,6,7; m = new Matrix (3, 5, (double *)d); m - show(); delete m; cout endl ; m = new UnitMatrix (6); m - show(); delete m; return 0; (分数:30.00)_三、综合应用题(总题数:1,分数:40.00)3.请使用 VC6 或使用【答题】菜单打开 proj3 下的工程 proj3,其中声明的 DataList 类,是一个用于表示数据表的类。DataList 的重载运算符函数 operator+,其功能是求当前数据表与另一个相同长度的数据表之和;即它返回一个数
9、据表,其每个元素等于相应两个数据表对应元素之和。请编写这个 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
10、len; double * d; public: DataList(int len, double data=NULL); DataList(DataList int length() const return len; double getElement (int i) const return di; DataList operator + (const DataList /两个数据表求和 void show( const; /显示数据表 ; void writeToFile (char *, const DataList /main.cpp #include “DataList.h“ D
11、ataList:DataList (int len, double data):len(len) d=new doublelen; for(int i = 0; i len; i +) di = (data = NULL? 0.0:datai); DataList:DataList (DataList for(int i = 0; i len; i+) di = data.di; DataList DataList:operator + (const DataList / *333* / *666* return DataList (list.length(),dd); void DataLi
12、st:show() const /显示数据表 for (int i = 0; i len-1; i+) cout di “,“; cout dlen-1 endl; int main() double s1 = 1,2,3,4,5,6; double s2 = 3,4,5,6,7,8; DataList list1(6, s1), list2(6, s2); /定义两个数据表对象 cout “两个数据表:“ endl; list1.show(); list2.show(); cout endl “两个数据表只和:“ endl; (list1 + list2).show(); writeToFi
13、le (“ “, list1 + list2); return 0; (分数:40.00)_二级 C+分类模拟 122 答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“/ ERROR *found*”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: Constructor called. The value is 10 Copy constructor called. The value is
14、 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 “Constructor called.“ endl; / ERROR *found* MyClass(const MyClass p) value = p.value; cout “C
15、opy constructor called.“ 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 0; (分数:30.00)_正确答案:()解析:(1)MyClass(int i=10) (2)MyClass(con
16、st MyClass /矩阵基础类,一个抽象类 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
17、 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 (int rows, int cols, double m = NULL):_ / *found* val =_; for(int i = 0; i rows * cols; i+) vali = (m=NULL? 0.0:mi); Matrix() delete val; dou
18、ble getElement (int r, int c) constreturn valr * getCols() +c;) ; /单位阵(主对角线元素都是 1,其余元素都是 0 的方阵)类 class UnitMatrix: public MatrixBase public: UnitMatrix (int rows): MatrixBase(rows, rows) /单位阵行数列数相同 double getElement (int r, int c) const / *found* if(_) return 1.0; return 0.0; ; int main() MatrixBase
19、 * m; double d 5 = 1,2, 3, 4,5, 2,3,4,5,6, 3,4,5,6,7; m = new Matrix (3, 5, (double *)d); m - show(); delete m; cout endl ; m = new UnitMatrix (6); m - show(); delete m; return 0; (分数:30.00)_正确答案:()解析:(1)getElement(i,j) (2)MatrixBase(rows,cols) (3)new doublerows*cols (4)r=c 答案考生文件夹 考点 本题考查 MatrixBas
20、e 类及其派生类 Matrix 和 UnitMatrix,其中涉及构造函数、const 函数、纯虚函数、动态数组和析构函数。派生类的构造函数要涉及基类的初始化,因此必须使用成员初始化列表。动态数组要先使用 new 语句分配空间,再赋值。 解析 (1)主要考查考生对纯虚函数的掌握,函数功能是分行显示矩阵中所有元素。因此在这里要输出行为 i、列为 j 的元素,使用纯虚函数 getElement(i,j)实现,输出语句为 coutgetElement(i,j)“ “;。 (2)主要考查考生对派生类的构造函数的掌握,派生类的构造函数使用成员列表初始化法,先对基类初始化。 (3)主要考查考生对动态数组的
21、掌握,val 是 double 型指针,要给 val 赋值,就要先给它分配空间,应使用 new 来完成。 (4)主要考查考生对成员函数的掌握,因为要输出单位矩阵,只有满足条件 r=c 的元素为 1.0,所以填写语句 if(r=c) return 1.0;。三、综合应用题(总题数:1,分数:40.00)3.请使用 VC6 或使用【答题】菜单打开 proj3 下的工程 proj3,其中声明的 DataList 类,是一个用于表示数据表的类。DataList 的重载运算符函数 operator+,其功能是求当前数据表与另一个相同长度的数据表之和;即它返回一个数据表,其每个元素等于相应两个数据表对应元
22、素之和。请编写这个 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; pub
23、lic: DataList(int len, double data=NULL); DataList(DataList int length() const return len; double getElement (int i) const return di; DataList operator + (const DataList /两个数据表求和 void show( const; /显示数据表 ; void writeToFile (char *, const DataList /main.cpp #include “DataList.h“ DataList:DataList (in
24、t len, double data):len(len) d=new doublelen; for(int i = 0; i len; i +) di = (data = NULL? 0.0:datai); DataList:DataList (DataList for(int i = 0; i len; i+) di = data.di; DataList DataList:operator + (const DataList / *333* / *666* return DataList (list.length(),dd); void DataList:show() const /显示数
25、据表 for (int i = 0; i len-1; i+) cout di “,“; cout dlen-1 endl; int main() double s1 = 1,2,3,4,5,6; double s2 = 3,4,5,6,7,8; DataList list1(6, s1), list2(6, s2); /定义两个数据表对象 cout “两个数据表:“ endl; list1.show(); list2.show(); cout endl “两个数据表只和:“ endl; (list1 + list2).show(); writeToFile (“ “, list1 + list2); return 0; (分数:40.00)_正确答案:()解析:for(int i=0; ilen; +i) /遍历对象 list 中的数组和 d 数组,把对应的值相加后放到数组 dd中。 ddi = di + list.di; 答案考生文件夹 考点 本题考查 DataList 类,其中涉及构造函数、动态数组、复制构造函数、const函数和运算符重载。 解析 主要考查考生对重载运算符的掌握,题目要求对两个数据表求和。程序已经定义了动态数组 dd,并已经分配好了空间,接下来只要运用循环语句完成元素相加并进行赋值即可。