1、二级 C+分类模拟 117 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“/ ERROR *found*”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: The value is:10 注意:只修改注释“/ ERROR *found*”的下一行语句,不要改动程序中的其他内容。 / proj1.cpp #include iostream using namespace std; class
2、 Member / ERROR *found* private: Member(int val):value(val) int value; ; class MyClass Member _m; public: / ERROR *found* MyClass(int val) int GetValue() const return _m.value; ; int main() MyClass *obj = new MyClass (10); / ERROR *found* 下面语句输出 obj 指向类中的 value 值 cout “The value is:“ obj.GetValue()
3、endl; delete obj; return 0; (分数:30.00)_二、简单应用题(总题数:1,分数:30.00)2.请使用 VC6 或使用【答题】菜单打开 proj2 下的工程 proj2,该工程中含有一个源程序文件proj2.cpp,其中定义了 CharShape 类、Triangle 类和 Rectangle 类。 CharShape 是一个抽象基类,它表示由字符组成的图形(简称字符图形),纯虚函数 Show 用于显示不同字符图形的相同操作接口。Triangle 和 Rectangle 是 CharShape 的派生类,它们分别用于表示字符三角形和字符矩形,并且都定义了成员函数
4、 Show,用于实现各自的显示操作。程序的正确输出结果应为: * * * * # # # 请阅读程序,分析输出结果,然后根据以下要求在横线处填写适当的代码并删除横线。 (1)将 Triangle 类的成员函数 Show 补充完整,使字符三角形的显示符合输出结果。 (2)将 Rectangle 类的成员函数 Show 补充完整,使字符矩形的显示符合输出结果。 (3)为类外函数 fun 添加合适的形参。 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“/ *found*”。 / proj2.cpp #include iostream using namespace st
5、d; class CharShape public: CharShape(char ch) : _ch(ch) ; virtual void Show() = 0; protected: char _ch; /组成图形的字符 ; class Triangle : public CharShape public: Triangle (char ch, int r) : CharShape(ch), _rows(r) void Show(); private: int _rows; /行数 ; class Rectangle: public CharShape public: Rectangle
6、(char ch, int r, int c) : CharShape (ch), _rows(r), _cols(c) void Show (); private : int _rows, _cols; /行数和列数 ; void Triangle:Show() /输出字符组成的三角形 for (int i = 1; i = _rows; i +) / *found* for (int j = 1; j =_; j +) cout _ch; cout endl; void Rectangle:Show() /输出字符组成的矩形 / *found* for (int i = 1; i =_;
7、i+) / *found* for (int j = 1; j =_; j+) cout _ch; cout endl; / *found* 为 fun 函数添加形参 void fun (_) cs. Show(); int main() Triangle tri (“*“, 4); Rectangle rect(“#“, 3, 8); fun(tri); fun(rect); return 0; (分数:30.00)_三、综合应用题(总题数:1,分数:40.00)3.请使用 VC6 或使用【答题】菜单打开 proj3 下的工程文件 proj3,此工程中包含一个源程序文件proj3.cpp,其
8、功能是从文本文件 in.dat 中读取全部整数,将整数序列存放到 intArray 类的对象中,然后建立另一对象 myArray,将对象内容赋值给 myArray。类 intArray 重载了“=”运算符。程序中给出了一个测试数据文件 input,不超过 300 个的整数。程序的输出是: 10 11 13 16 20 要求: 补充编制的内容写在“/ *333*”与“/ *666*”之间。实现重载赋值运算符函数,并将赋值结果在屏幕输出。格式不限。不得修改程序的其他部分。 注意:程序最后将结果输出到文件 out.dat 中。输出函数 writeToFile 已经编译为 obj 文件,并且在本程序中
9、调用。 /intArray.h class intArray private: int * array; int length; public: intArray(char * filename); intArray(); intArray intArray(); void show(); ; void writeToFile(const char * path); /main.cpp #include iostream #include fstream #include cstring #include “intArray.h“ using namespace std; intArray:i
10、ntArray() length = 10; array = new intlength; intArray:intArray (char * filename) ifstream myFile(filename); array = new int300; length = 0; while(myFile arraylength +) length -; myFile.close(); intArray length = src.length; array = new intlength; / *333* / *666* return * this; intArray:intArray() d
11、elete array; void intArray:show() int step=0; for(int i=0; ilength; i=i+step) cout arrayi endl; step +; void main() intArray * arrayP = new intArray (“input.dat“); intArray myArray; myArray = * arrayP; (* arrayP).show(); delete arrayP; writeToFile(“ “); (分数:40.00)_二级 C+分类模拟 117 答案解析(总分:100.00,做题时间:9
12、0 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1,此工程中含有一个源程序文件proj1.cpp。其中位于每个注释“/ ERROR *found*”之后的一行语句存在错误。请改正这些错误,使程序的输出结果为: The value is:10 注意:只修改注释“/ ERROR *found*”的下一行语句,不要改动程序中的其他内容。 / proj1.cpp #include iostream using namespace std; class Member / ERROR *found* private: Mem
13、ber(int val):value(val) int value; ; class MyClass Member _m; public: / ERROR *found* MyClass(int val) int GetValue() const return _m.value; ; int main() MyClass *obj = new MyClass (10); / ERROR *found* 下面语句输出 obj 指向类中的 value 值 cout “The value is:“ obj.GetValue() endl; delete obj; return 0; (分数:30.0
14、0)_正确答案:()解析:(1)public: (2)MyClass(int val): _m(val)或 MyClass(int val) _m=val (3)cout“The value is:“obj-GetValue()endl; 答案考生文件夹 考点 本题主要考查的是 Member 类和 MyClass 类,其中涉及构造函数、成员函数和类的指针。类的指针调用类的成员函数时要使用标识符“-”,而不是“.”,这是最容易出错的地方。私有成员使用 private,公有成员使用 public,保护成员使用 protected,类的构造函数一定是公有成员函数。 解析 (1)主要考查考生对私有成员
15、和公有成员的掌握,先看改错语句的下一条语句:Member(int val):value(val) ,该语句是一个构造函数,因此我们可以得出此处为公有成员,因为构造函数不可能是私有成员。 (2)主要考查构造函数,构造函数要对类的成员进行初始化,因此在这里使用成员列表初始化,即MyClass(int val):_m(val)或 MyClass(int val)_m=val。 (3)指针调用类的成员函数时,应使用标识符“-”。 判断该成员是公有成员还是私有成员的方法很多,不必一一死记,只要记住公有成员可以被类外函数调用,私有成员则不可以,类的构造函数一定是公有成员函数就可以了。二、简单应用题(总题数
16、:1,分数:30.00)2.请使用 VC6 或使用【答题】菜单打开 proj2 下的工程 proj2,该工程中含有一个源程序文件proj2.cpp,其中定义了 CharShape 类、Triangle 类和 Rectangle 类。 CharShape 是一个抽象基类,它表示由字符组成的图形(简称字符图形),纯虚函数 Show 用于显示不同字符图形的相同操作接口。Triangle 和 Rectangle 是 CharShape 的派生类,它们分别用于表示字符三角形和字符矩形,并且都定义了成员函数 Show,用于实现各自的显示操作。程序的正确输出结果应为: * * * * # # # 请阅读程序
17、,分析输出结果,然后根据以下要求在横线处填写适当的代码并删除横线。 (1)将 Triangle 类的成员函数 Show 补充完整,使字符三角形的显示符合输出结果。 (2)将 Rectangle 类的成员函数 Show 补充完整,使字符矩形的显示符合输出结果。 (3)为类外函数 fun 添加合适的形参。 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“/ *found*”。 / proj2.cpp #include iostream using namespace std; class CharShape public: CharShape(char ch) : _c
18、h(ch) ; virtual void Show() = 0; protected: char _ch; /组成图形的字符 ; class Triangle : public CharShape public: Triangle (char ch, int r) : CharShape(ch), _rows(r) void Show(); private: int _rows; /行数 ; class Rectangle: public CharShape public: Rectangle (char ch, int r, int c) : CharShape (ch), _rows(r)
19、, _cols(c) void Show (); private : int _rows, _cols; /行数和列数 ; void Triangle:Show() /输出字符组成的三角形 for (int i = 1; i = _rows; i +) / *found* for (int j = 1; j =_; j +) cout _ch; cout endl; void Rectangle:Show() /输出字符组成的矩形 / *found* for (int i = 1; i =_; i+) / *found* for (int j = 1; j =_; j+) cout _ch;
20、cout endl; / *found* 为 fun 函数添加形参 void fun (_) cs. Show(); int main() Triangle tri (“*“, 4); Rectangle rect(“#“, 3, 8); fun(tri); fun(rect); return 0; (分数:30.00)_正确答案:()解析:(1)i*2-1 (2)_rows (3)_cols (4)CharShape int length; public: intArray(char * filename); intArray(); intArray intArray(); void sho
21、w(); ; void writeToFile(const char * path); /main.cpp #include iostream #include fstream #include cstring #include “intArray.h“ using namespace std; intArray:intArray() length = 10; array = new intlength; intArray:intArray (char * filename) ifstream myFile(filename); array = new int300; length = 0;
22、while(myFile arraylength +) length -; myFile.close(); intArray length = src.length; array = new intlength; / *333* / *666* return * this; intArray:intArray() delete array; void intArray:show() int step=0; for(int i=0; ilength; i=i+step) cout arrayi endl; step +; void main() intArray * arrayP = new i
23、ntArray (“input.dat“); intArray myArray; myArray = * arrayP; (* arrayP).show(); delete arrayP; writeToFile(“ “); (分数:40.00)_正确答案:()解析:for (int i = 0; i length; i+) /遍历对象 src 中的数组 array,然后依次把值放进数组array 中 arrayi = src.arrayi; 答案考生文件夹 考点 本题主要考查 intArray 类,其中涉及动态数组、构造函数、运算符重载、析构函数及其他成员函数。 解析 主要考查考生对运算符重载的掌握,该函数要重载运算符“=”,该运算符的意思是赋值。先看该函数的其他语句: if(array! = NULL) delete array; length = src.length; array = new int length; 第一条语句是把原来动态数组释放,第二条语句是把形参 src 的成员 length 赋值给变量 length,第三条语句是给指针 array 分配内存。接下来要把动态数组中的值逐个赋给 array 数组,在这里使用 for 循环语句,循环变量 i 的范围是 0length,并进行赋值操作。