【计算机类职业资格】二级C++分类模拟134及答案解析.doc

上传人:figureissue185 文档编号:1324211 上传时间:2019-10-17 格式:DOC 页数:9 大小:44.50KB
下载 相关 举报
【计算机类职业资格】二级C++分类模拟134及答案解析.doc_第1页
第1页 / 共9页
【计算机类职业资格】二级C++分类模拟134及答案解析.doc_第2页
第2页 / 共9页
【计算机类职业资格】二级C++分类模拟134及答案解析.doc_第3页
第3页 / 共9页
【计算机类职业资格】二级C++分类模拟134及答案解析.doc_第4页
第4页 / 共9页
【计算机类职业资格】二级C++分类模拟134及答案解析.doc_第5页
第5页 / 共9页
点击查看更多>>
资源描述

1、二级 C+分类模拟 134 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1,其中在编辑窗口内显示的主程序文件中定义有类 ABC 和主函数 main。程序文本中位于每行“/ ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是: 21 23 注意:只修改每个“/ ERROR *found*”下面的一行,不要改动程序中的其他任何内容。 #include iostream using namespace std; class ABC p

2、ublic: / ERROR *found* ABC() a=0; b=0; c=0; ABC(int aa, int bb, int cc); void Setab() +a, +b; int Sum() return a+b+c; private: int a,b; const int c; ; ABC:ABC (int aa, int bb, int cc):c(cc) a=aa; b=bb; int main() ABC x(1,2,3), y(4,5,6); ABC z, *w= w-Setab(); / ERROR *found* int s1 =x.Sum() + y - Sum

3、); cout s1 “; / ERROR *found* int s2 = s1 + w.Sum(); cout s2 endl; return 0; (分数:30.00)_二、简单应用题(总题数:1,分数:30.00)2.请使用 VC6 或使用【答题】菜单打开 proj2 下的工程 proj2,其中在编辑窗口内显示的主程序文件中定义有类 Base 和 Derived,以及主函数 main。程序文本中位于每行“/ *found*”下面的一行内有一处或多处下画线标记,请在每个下画线标记处填写合适的内容,并删除下画线标记。经修改后运行程序,得到的输出应为: sum=55。 注意:只在横线处填写

4、适当的代码,不要改动程序中的其他内容。 #include iostream using namespace std; class Base public: Base(int m1,int m2) mem1 = m1; mem2 = m2; int sum() return mem1 + mem2; private: int mem1, mem2; /基类的数据成员 ; /派生类 Derived 从基类 Base 公有继承 / *found* class Derived:_ public: Derived (int m1, int m2, int m3); /sum 函数定义,要求返回 mem1

5、mem2 和 mem3 之和 / *found* int sum() return_ + mem3; private: int mem3; /派生类本身的数据成员 ; /构造函数的类外定义,要求由 m1 和 m2 分别初始化 mem1 和 mem2,由 m3 初始化 mem3 / *found* _Derived (int m1, int m2, int m3): , mem3 (m3) / *found* int main() Base a(4,6); Derived b (10,15,20); int sum=a.sum()+b.sum(); cout “sum =“ sum endl;

6、 return 0; (分数:30.00)_三、综合应用题(总题数:1,分数:40.00)3.请使用 VC6 或使用【答题】菜单打开 proj3 下的工程 proj3,其中包含主程序文件 main.cpp 和用户定义的头文件 Array.h,整个程序包含有类 Array 的定义和主函数 main 的定义。请把主程序文件中的 Array类的成员函数 Contrary()的定义补充完整,经补充后运行程序,得到的输出结果应该是: 5 8 5,4,3,2,1 0,0,8.4,5.6,4.5,3.4,2.3,1.2 注意:只允许在“/ *333*”和“/ *666*”之间填写内容,不允许修改其他任何地方

7、的内容。 /Array.h #include iostream using namespace std; templateclass Type, int m class Array /数组类 public: Array(Type b, int mm) /构造函数 for(int i=0; im; i+) if(imm) ai=bi; else ai=0; void Contrary(); /交换数组 a 中前后位置对称的元素的值 int Length() const return m; /返回数组长度 Type operator (int i) const /下标运算符重载为成员函数 if(i

8、0 | i=m) cout “下标越界!“ endl; exit(1); return ai; private: Type am; ; void writeToFile(const char *); /不用考虑此语句的作用 /main.cpp #include “Array.h“ /交换数组 a 中前后位置对称的元素的值 templateclass Type, int m void Array Type,m:Contrary() /补充函数体 / *333* / *666* int main() int s15 = 1,2,3,4,5; double s26 = 1.2,2.3,3.4,4.5

9、5.6,8.4; Array int,5 d1 (s1,5); Array double,8 d2 (s2,6); int i; d1.Contrary(); d2.Contrary(); cout d1.Length() “ d2.Length() endl; for(i=0;i4;i+) cout d1i “,“; cout d14 endl; for(i=0;i7;i +) cout d2i “,“; cout d27 endl; writeToFile(“ “); /不用考虑此语句的作用 return 0; (分数:40.00)_二级 C+分类模拟 134 答案解析(总分:100.0

10、0,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1,其中在编辑窗口内显示的主程序文件中定义有类 ABC 和主函数 main。程序文本中位于每行“/ ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是: 21 23 注意:只修改每个“/ ERROR *found*”下面的一行,不要改动程序中的其他任何内容。 #include iostream using namespace std; class ABC public: / ERROR *found* ABC()

11、 a=0; b=0; c=0; ABC(int aa, int bb, int cc); void Setab() +a, +b; int Sum() return a+b+c; private: int a,b; const int c; ; ABC:ABC (int aa, int bb, int cc):c(cc) a=aa; b=bb; int main() ABC x(1,2,3), y(4,5,6); ABC z, *w= w-Setab(); / ERROR *found* int s1 =x.Sum() + y - Sum(); cout s1 “; / ERROR *foun

12、d* int s2 = s1 + w.Sum(); cout s2 endl; return 0; (分数:30.00)_正确答案:()解析:(1)ABC():a(0),b(0),c(0) (2)int s1=x.Sum()+y.Sum(); (3)int s2=s1+w-Sum(); 答案考生文件夹 考点 本题考查的是 ABC 类,其中涉及构造函数、成员函数和常变量私有成员。构造函数中因为要给常变量私有成员初始化,所以必须使用成员列表初始化来赋初值。只有类的指针在调用成员函数时才使用标识符“-”。 解析 (1)主要考查考生对构造函数的掌握情况,根据私有成员的定义:const int c;可知

13、c 为常变量,因此构造函数必须使用成员列表初始化来给 c 赋初始值。 (2)主要考查考生对类的指针的掌握情况,根据主函数的第一条语句:ABC x(1,2,3),y(4,5,6);可知,x 和 y 都是 ABC 类,但不是指针,因此它们调用 ABC 类的成员函数要使用标识符“.”,而不是“-”。 (3)主要考查考生对类的指针的掌握情况,根据主函数的第二条语句:ABC z,*w= class Base public: Base(int m1,int m2) mem1 = m1; mem2 = m2; int sum() return mem1 + mem2; private: int mem1,

14、 mem2; /基类的数据成员 ; /派生类 Derived 从基类 Base 公有继承 / *found* class Derived:_ public: Derived (int m1, int m2, int m3); /sum 函数定义,要求返回 mem1、mem2 和 mem3 之和 / *found* int sum() return_ + mem3; private: int mem3; /派生类本身的数据成员 ; /构造函数的类外定义,要求由 m1 和 m2 分别初始化 mem1 和 mem2,由 m3 初始化 mem3 / *found* _Derived (int m1,

15、int m2, int m3): , mem3 (m3) / *found* int main() Base a(4,6); Derived b (10,15,20); int sum=a.sum()+b.sum(); cout “sum =“ sum endl; return 0; (分数:30.00)_正确答案:()解析:(1)public Base (2)Base:sum() (3)Derived: (4)Base(m1,m2) 答案考生文件夹 考点 本题考查的是 Base 类及其派生类 Derived 类,其中涉及构造函数和成员函数。构造函数在类外定义时,因为构造函数是属于类的函数,所

16、以函数前也要加上类名和作用域符,派生类的构造函数要先给基类初始化,使用成员列表初始化。 解析 (1)主要考查考生对公有继承的掌握情况,根据题目要求:派生类 Derived 从基类 Base 公有继承,因此这里使用 public 来公有继承。 (2)主要考查考生对成员函数的掌握情况,根据题目对 sum 函数的要求:sum 函数定义,要求返回mem1、mem2 和 mem3 之和,因此这里直接调用基类的 sum 函数,再加上 mem3 就满足题目要求。 (3)主要考查考生对构造函数的掌握情况,由于 Derived 的构造函数在类外定义,因此要加上类名和作用域符,即 Derived:。 (4)主要考

17、查考生对构造函数的掌握情况,因为 Derived 是 Base 类的派生类,所以其构造函数要使用成员列表初始化先给 Base 初始化。 主要考查考生对公有继承、成员函数和构造函数的掌握程度,在派生类中直接调用基类的函数,要在前面加上基类名和作用域符,从而防止派生类中也有相同函数时产生的二义性。三、综合应用题(总题数:1,分数:40.00)3.请使用 VC6 或使用【答题】菜单打开 proj3 下的工程 proj3,其中包含主程序文件 main.cpp 和用户定义的头文件 Array.h,整个程序包含有类 Array 的定义和主函数 main 的定义。请把主程序文件中的 Array类的成员函数

18、Contrary()的定义补充完整,经补充后运行程序,得到的输出结果应该是: 5 8 5,4,3,2,1 0,0,8.4,5.6,4.5,3.4,2.3,1.2 注意:只允许在“/ *333*”和“/ *666*”之间填写内容,不允许修改其他任何地方的内容。 /Array.h #include iostream using namespace std; templateclass Type, int m class Array /数组类 public: Array(Type b, int mm) /构造函数 for(int i=0; im; i+) if(imm) ai=bi; else ai

19、0; void Contrary(); /交换数组 a 中前后位置对称的元素的值 int Length() const return m; /返回数组长度 Type operator (int i) const /下标运算符重载为成员函数 if(i0 | i=m) cout “下标越界!“ endl; exit(1); return ai; private: Type am; ; void writeToFile(const char *); /不用考虑此语句的作用 /main.cpp #include “Array.h“ /交换数组 a 中前后位置对称的元素的值 templateclass

20、 Type, int m void Array Type,m:Contrary() /补充函数体 / *333* / *666* int main() int s15 = 1,2,3,4,5; double s26 = 1.2,2.3,3.4,4.5,5.6,8.4; Array int,5 d1 (s1,5); Array double,8 d2 (s2,6); int i; d1.Contrary(); d2.Contrary(); cout d1.Length() “ d2.Length() endl; for(i=0;i4;i+) cout d1i “,“; cout d14 endl

21、 for(i=0;i7;i +) cout d2i “,“; cout d27 endl; writeToFile(“ “); /不用考虑此语句的作用 return 0; (分数:40.00)_正确答案:()解析:for (int i = 0, j = m - 1; i j; i+, j-) /i 从 0 开始自加,j 从数组最一位开始自减,条件是 ij,开始遍历数组 a Type temp = ai; /把 ai赋值给变量 temp ai = aj; /把 aj赋值给j aj = temp; /把 temp 赋值给 aj,最终使 ai与 aj值的互换 答案考生文件夹 考点 本题考查的是 A

22、rray 类,其中涉及构造函数、const 函数和运算符重载。交换数组中前后对称的元素的值,要使用两个下标 i 和 j,一个代表第一个元素,一个代表最后一个元素,交换后 i+,j-即可。 解析 主要考查考生对交换算法的掌握情况,根据题目对要完成的函数 Contrary 的要求:交换数组 a 中前后位置对称的元素的值。这里取下标 i=0,即为数组中的第一个元素,j=m-1,即为数组中的最后一个元素,利用中间值 temp 交换元素的值,然后 i 逐次递增的同时 j 逐次递减,再交换,循环到 ij 时停止交换即可。 主要考查考生对数组中元素交换的掌握情况,交换算法要使得两个下标 i 和 j 移动的范围能覆盖全部元素,同时要确定 for 循环的终止条件。

展开阅读全文
相关资源
猜你喜欢
  • DIN EN ISO 7287-2003 Graphical symbols for thermal cutting equipment (ISO 7287 2002) German version EN ISO 7287 2002《热切割设备图形符号》.pdf DIN EN ISO 7287-2003 Graphical symbols for thermal cutting equipment (ISO 7287 2002) German version EN ISO 7287 2002《热切割设备图形符号》.pdf
  • DIN EN ISO 7291-2015 Gas welding equipment - Pressure regulators for manifold systems used in welding cutting and allied processes up to 30 MPa (300 bar) (ISO 7291 2010 + Amd 1 201.pdf DIN EN ISO 7291-2015 Gas welding equipment - Pressure regulators for manifold systems used in welding cutting and allied processes up to 30 MPa (300 bar) (ISO 7291 2010 + Amd 1 201.pdf
  • DIN EN ISO 7326-2017 Rubber and plastics hoses - Assessment of ozone resistance under static conditions (ISO 7326 2016) German version EN ISO 7326 2016《橡胶管和塑料管 静态条件下抗臭氧能力的评估(ISO 73.pdf DIN EN ISO 7326-2017 Rubber and plastics hoses - Assessment of ozone resistance under static conditions (ISO 7326 2016) German version EN ISO 7326 2016《橡胶管和塑料管 静态条件下抗臭氧能力的评估(ISO 73.pdf
  • DIN EN ISO 7327-1997 Plastics - Hardeners and accelerators for epoxide resins - Determination of free acid in acid anhydride (ISO 7327 1994) German version EN ISO 7327 1997《塑料 环氧树脂.pdf DIN EN ISO 7327-1997 Plastics - Hardeners and accelerators for epoxide resins - Determination of free acid in acid anhydride (ISO 7327 1994) German version EN ISO 7327 1997《塑料 环氧树脂.pdf
  • DIN EN ISO 7328-2009 Milk-based edible ices and ice mixes - Determination of fat content - Gravimetric method (Reference method) (ISO 7328 2008) English version of DIN EN ISO 7328 .pdf DIN EN ISO 7328-2009 Milk-based edible ices and ice mixes - Determination of fat content - Gravimetric method (Reference method) (ISO 7328 2008) English version of DIN EN ISO 7328 .pdf
  • DIN EN ISO 734-2016 Oilseed meals - Determination of oil content - Extraction method with hexane (or light petroleum) (ISO 734 2015) German version EN ISO 734 2015《油料种子残渣 测定油含量 已烷萃.pdf DIN EN ISO 734-2016 Oilseed meals - Determination of oil content - Extraction method with hexane (or light petroleum) (ISO 734 2015) German version EN ISO 734 2015《油料种子残渣 测定油含量 已烷萃.pdf
  • DIN EN ISO 7346-1-1998 Water quality - Determination of the acute lethal toxicity of substances to a freshwater fish [Brachydanio rerio Hamilton-Buchanan (Teleostei Cyprinidae)] - .pdf DIN EN ISO 7346-1-1998 Water quality - Determination of the acute lethal toxicity of substances to a freshwater fish [Brachydanio rerio Hamilton-Buchanan (Teleostei Cyprinidae)] - .pdf
  • DIN EN ISO 7346-2-1998 Water quality - Determination of the acute lethal toxicity of substances to a freshwater fish [Brachydanio rerio Hamilton-Buchanan (Teleostei Cyprinidae)] - .pdf DIN EN ISO 7346-2-1998 Water quality - Determination of the acute lethal toxicity of substances to a freshwater fish [Brachydanio rerio Hamilton-Buchanan (Teleostei Cyprinidae)] - .pdf
  • DIN EN ISO 7346-3-1998 Water quality - Determination of the acute lethal toxicity of substances to a freshwater fish [Brachydanio rerio Hamilton-Buchanan (Teleostei Cyprinidae)] - .pdf DIN EN ISO 7346-3-1998 Water quality - Determination of the acute lethal toxicity of substances to a freshwater fish [Brachydanio rerio Hamilton-Buchanan (Teleostei Cyprinidae)] - .pdf
  • 相关搜索

    当前位置:首页 > 考试资料 > 职业资格

    copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
    备案/许可证编号:苏ICP备17064731号-1