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

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

1、二级 C+分类模拟 137 及答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1。该工程中包含程序文件 main.cpp,其中有类 CDate(“日期”)和主函数 main 的定义。程序中位于每个“/ ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: 原日期:2005-9-25 更新后的日期:2006-4-1 注意:只修改每个“/ ERROR *found*”下的那一行,不要改动程序中的其他内容。 #include iostream

2、 #include cstdlib using namespace std; class CDate /日期类 / ERROR *found* protected: CDate() ; CDate(int d, int m, int y) / ERROR *found* SetDate (int day = d, int month=m, int year=y); ; void Display(); /显示日期 void SetDate (int day, int month, int year) /设置日期 m_nDay = day; m_nMonth = month; m_nYear =

3、year; private: int m_nDay; /日 int m_nMonth; /月 int m_nYear; /年 ; voidCDate:Display() /显示日期 / ERROR *found* cout m_nDay “-“ m_nMonth “-“ m_nYear; cout endl; int main () CDate d (25,9,2005); /调用构造函数初始化日期 cout “原日期:“; d.Display(); d.SetDate(1,4,2006); /调用成员函数重新设置日期 cout “更新后的日期:“; d. Display(); return

4、0; (分数:30.00)_二、简单应用题(总题数:1,分数:30.00)2.请使用 VC6 或使用【答题】菜单打开 proj2 下的工程 proj2,其中定义了 Employee 类和 Manager 类。Employee 用于表示某公司的雇员,其属性包括姓名(name)和工作部分(dept)。Manager 是 Employee 的公有派生类,用于表示雇员中的经理。除了姓名和工作部分之外,Manager 的属性还包括级别(level)。Employee 类的成员函数 print 用于输出雇员的信息;Manager 类的成员函数 print 负责输出经理的信息。请在横线处填写适当的代码,然后

5、删除横线,以实现上述类定义。此程序的正确输出结果应为: Name:Sally Smith Dept:Sales Level:2 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“/ *found*”。 #include iostream #include string using namespace std; class Employee public: Employee (string name, string dept): / *found* _ virtual void print() const; string dept() const /返回部门名称 /

6、*found* _ virtual Employee() private: string name_; string dept_; ; class Manager: public Employee public: Manager (string name, string dept, int level): / *found* _ virtual void print() const; private: int level; ; void Employee:print() const cout “Name:“ name_ endl; cout “Dept:“ dept_ endl; void M

7、anager:print() const / *found* cout “Level:“ level_ endl; int main() Employee * emp = new Manager(“Sally Smith“, “Sales“, 2); emp - print(); delete emp; return 0; (分数:30.00)_三、综合应用题(总题数:1,分数:40.00)3.请使用 VC6 或使用【答题】菜单打开 proj3 下的工程文件 proj3,其中该工程中包含定义了用于表示姓名的抽象类 Name、表示“先名后姓”的姓名类 Name1(名、姓之间用空格隔开)和表示“先

8、姓后名”的姓名类 Name2(姓、名之间用逗号隔开);程序应当显示: John Smith Smith,John 但程序中有缺失部分,请按照以下提示,把缺失部分补充完整: (1)在“/ *1* *found*”的下方是函数 show 中的一个语句,它按先名后姓的格式输出姓名。 (2)在“/ *2* *found*”的下方是函数 getWord 中的一个语句,它把一个字符序列复制到 head所指向的字符空间中,复制从 start 所指向的字符开始,共复制 end-start 个字符。 (3)在“/ *3* *found*”的下方是函数 createName 中的语句,它根据指针 p 的值决定返回

9、何种对象:如果 p 为空,直接返回一个 Name1 对象,否则直接返回一个 Name2 对象。注意:返回的 Name1 或Name2 对象必须是动态对象,返回的实际是指向它的指针。 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“*found*”。填写的内容必须在一行中完成,否则评分将产生错误。 /proj3.cpp #include iostream using namespace std; class Name protected: char * surname; /姓 char * firstname; /名 public: Name() delete sur

10、name; delete firstname; virtual void show() = 0; ; class Name1:public Name public: Name1(const char * name); / *1* *found* void show() _; ; class Name2:public Name public: Name2(const char * name); void show() cout surname “,“ firstname; ; char * getWord (const char * start,const char * end) char *

11、head = new charend - start +1; / *2* *found* for (int i = 0; i end - start; i +)_; headend-start=“/0“; return head; Name1:Name1(const char * name) char * p = strchr(name,“); firstname = getWord(name,p); surname = new char strlen (p); strcpy(surname,p+1); Name2:Name2(const char * name) char * p = str

12、chr(name,“,“); surname = getWord(name,p); firstname = new charstrlen(p); strcpy(firstname,p+1); Name * createName(const char * s) char * p = strchr(s,“,“); / *3* *found* if(p)_; int main() Name * n; n = createName(“John Smith“); n - show(); cout endl; delete n; n = createName(“Smith,John“); n - show

13、(); cout endl; delete n; return 0; (分数:40.00)_二级 C+分类模拟 137 答案解析(总分:100.00,做题时间:90 分钟)一、基本操作题(总题数:1,分数:30.00)1.请使用 VC6 或使用【答题】菜单打开 proj1 下的工程 proj1。该工程中包含程序文件 main.cpp,其中有类 CDate(“日期”)和主函数 main 的定义。程序中位于每个“/ ERROR *found*”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为: 原日期:2005-9-25 更新后的日期:2006-4-1 注意:只修改每个“/ ERROR

14、*found*”下的那一行,不要改动程序中的其他内容。 #include iostream #include cstdlib using namespace std; class CDate /日期类 / ERROR *found* protected: CDate() ; CDate(int d, int m, int y) / ERROR *found* SetDate (int day = d, int month=m, int year=y); ; void Display(); /显示日期 void SetDate (int day, int month, int year) /设置

15、日期 m_nDay = day; m_nMonth = month; m_nYear = year; private: int m_nDay; /日 int m_nMonth; /月 int m_nYear; /年 ; voidCDate:Display() /显示日期 / ERROR *found* cout m_nDay “-“ m_nMonth “-“ m_nYear; cout endl; int main () CDate d (25,9,2005); /调用构造函数初始化日期 cout “原日期:“; d.Display(); d.SetDate(1,4,2006); /调用成员函

16、数重新设置日期 cout “更新后的日期:“; d. Display(); return 0; (分数:30.00)_正确答案:()解析:(1)public: (2)SetDate(d,m,y); (3)cout m_nYear “-“ m_nMonth “-“ m_nDay; 答案考生文件夹 考点 本题考查 CDate 类,其中涉及构造函数和成员函数。判断函数是公有成员还是保护成员,主要通过在主函数中函数的调用来确定,如果函数在主函数中被调用则说明是公有成员,否则为私有成员或者保护成员。 解析 (1)通过主函数中成员函数的调用可知这里应该为公有成员,而且构造函数必须为公有继承。 (2)主要考

17、查考生对成员函数的掌握,程序在这里调用成员函数 SetDate,直接把形参代入即可。 (3)题目要求输出原日期:2005-9-25。可以知道输出顺序为:先输出年,其次月,最后是日。 主要考查考生对公有成员,成员函数的掌握,构造函数一般情况下肯定是公有成员,当涉及派生类时才会使用保护成员。二、简单应用题(总题数:1,分数:30.00)2.请使用 VC6 或使用【答题】菜单打开 proj2 下的工程 proj2,其中定义了 Employee 类和 Manager 类。Employee 用于表示某公司的雇员,其属性包括姓名(name)和工作部分(dept)。Manager 是 Employee 的公

18、有派生类,用于表示雇员中的经理。除了姓名和工作部分之外,Manager 的属性还包括级别(level)。Employee 类的成员函数 print 用于输出雇员的信息;Manager 类的成员函数 print 负责输出经理的信息。请在横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为: Name:Sally Smith Dept:Sales Level:2 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“/ *found*”。 #include iostream #include string using namespace std;

19、class Employee public: Employee (string name, string dept): / *found* _ virtual void print() const; string dept() const /返回部门名称 / *found* _ virtual Employee() private: string name_; string dept_; ; class Manager: public Employee public: Manager (string name, string dept, int level): / *found* _ virt

20、ual void print() const; private: int level; ; void Employee:print() const cout “Name:“ name_ endl; cout “Dept:“ dept_ endl; void Manager:print() const / *found* cout “Level:“ level_ endl; int main() Employee * emp = new Manager(“Sally Smith“, “Sales“, 2); emp - print(); delete emp; return 0; (分数:30.

21、00)_正确答案:()解析:(1)name_(name),dept_(dept) (2)return dept_; (3)Employee(name,dept),level_(level) (4)Employee:print(); 答案考生文件夹 考点 本题考查 Employee 类及其派生类 Manager,其中涉及构造函数、虚函数和 cosnt函数。构造函数使用成员列表初始化,特别是派生类的构造函数要先调用基类的构造函数。 解析 (1)主要考查考生对构造函数的掌握,这里使用成员列表初始化法对私有成员初始化。 (2)主要考查考生对成员函数的掌握,题目要求返回部门名称,因此这里是一条返回语句。

22、函数要求返回的类型为 string,因此直接返回 dept_即可。 (3)主要考查考生对构造函数的掌握,因为 Manager 类是 Employee 类的派生类,因此它的构造函数要先对基类初始化,应使用成员列表初始化。 (4)主要考查考生对虚函数的掌握,因为 Manager 类是 Employee 类的派生类,因此它的 print 函数可以先调用基类的 print 函数,再输出自身要输出的数据,故为 Employee:print();。三、综合应用题(总题数:1,分数:40.00)3.请使用 VC6 或使用【答题】菜单打开 proj3 下的工程文件 proj3,其中该工程中包含定义了用于表示姓

23、名的抽象类 Name、表示“先名后姓”的姓名类 Name1(名、姓之间用空格隔开)和表示“先姓后名”的姓名类 Name2(姓、名之间用逗号隔开);程序应当显示: John Smith Smith,John 但程序中有缺失部分,请按照以下提示,把缺失部分补充完整: (1)在“/ *1* *found*”的下方是函数 show 中的一个语句,它按先名后姓的格式输出姓名。 (2)在“/ *2* *found*”的下方是函数 getWord 中的一个语句,它把一个字符序列复制到 head所指向的字符空间中,复制从 start 所指向的字符开始,共复制 end-start 个字符。 (3)在“/ *3*

24、 *found*”的下方是函数 createName 中的语句,它根据指针 p 的值决定返回何种对象:如果 p 为空,直接返回一个 Name1 对象,否则直接返回一个 Name2 对象。注意:返回的 Name1 或Name2 对象必须是动态对象,返回的实际是指向它的指针。 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“*found*”。填写的内容必须在一行中完成,否则评分将产生错误。 /proj3.cpp #include iostream using namespace std; class Name protected: char * surname; /姓

25、char * firstname; /名 public: Name() delete surname; delete firstname; virtual void show() = 0; ; class Name1:public Name public: Name1(const char * name); / *1* *found* void show() _; ; class Name2:public Name public: Name2(const char * name); void show() cout surname “,“ firstname; ; char * getWord

26、 (const char * start,const char * end) char * head = new charend - start +1; / *2* *found* for (int i = 0; i end - start; i +)_; headend-start=“/0“; return head; Name1:Name1(const char * name) char * p = strchr(name,“); firstname = getWord(name,p); surname = new char strlen (p); strcpy(surname,p+1);

27、 Name2:Name2(const char * name) char * p = strchr(name,“,“); surname = getWord(name,p); firstname = new charstrlen(p); strcpy(firstname,p+1); Name * createName(const char * s) char * p = strchr(s,“,“); / *3* *found* if(p)_; int main() Name * n; n = createName(“John Smith“); n - show(); cout endl; de

28、lete n; n = createName(“Smith,John“); n - show(); cout endl; delete n; return 0; (分数:40.00)_正确答案:()解析:(1)cout firstname “ “ surname (2)headi = starti (3)return new Name2(s);else return new Name1(s) 答案考生文件夹 考点 本题考查 Name 类及其派生类 Name1 和 Name2,其中涉及动态数组、析构函数、纯虚函数和构造函数。 解析 (1)主要考查考生对成员函数的掌握,题目要求按先名后姓的格式输出姓名,因此输出语句的顺序应该是先输出 firstname,然后再输出 surname,注意它们之间还要输出个空格。 (2)主要考查考生对字符串复制的掌握,题目要求把一个字符序列复制到 head 所指向的字符空间中,复制从 start 所指向的字符开始,共复制 end-start 个字符。程序已经把 for 循环语句写好了,此处只要把复制语句完成即可,即 headi=starti。 (3)主要考查考生对动态分配的掌握,题目要求对象必须是动态对象,因此使用 new 来分配空间,建立动态对象:if(p)return new Name2(s);else return new Name1(s);。

展开阅读全文
相关资源
猜你喜欢
  • IEC 60384-3-1-2006 Fixed capacitors for use in electronic equipment - Part 3-1 Blank detail specification Surface mount fixed tantalum electrolytic capacitors with manganese dioxid.pdf IEC 60384-3-1-2006 Fixed capacitors for use in electronic equipment - Part 3-1 Blank detail specification Surface mount fixed tantalum electrolytic capacitors with manganese dioxid.pdf
  • IEC 60384-8-2015 Fixed capacitors for use in electronic equipment - Part 8 Sectional specification Fixed capacitors of ceramic dielectric Class 1《电子设备用固定电容器.第8部分 分规范 1级陶瓷介质固定电容器》.pdf IEC 60384-8-2015 Fixed capacitors for use in electronic equipment - Part 8 Sectional specification Fixed capacitors of ceramic dielectric Class 1《电子设备用固定电容器.第8部分 分规范 1级陶瓷介质固定电容器》.pdf
  • IEC 60384-9-2015 Fixed capacitors for use in electronic equipment - Part 9 Sectional specification Fixed capacitors of ceramic dielectric Class 2《电子设备用固定电容器.第9部分 分规范 2级陶瓷介质固定电容器》.pdf IEC 60384-9-2015 Fixed capacitors for use in electronic equipment - Part 9 Sectional specification Fixed capacitors of ceramic dielectric Class 2《电子设备用固定电容器.第9部分 分规范 2级陶瓷介质固定电容器》.pdf
  • IEC 60386-1972 Method of measurement of speed fluctuations in sound recording and reproducing equipment《录音和重放设备速度波动的测量方法》.pdf IEC 60386-1972 Method of measurement of speed fluctuations in sound recording and reproducing equipment《录音和重放设备速度波动的测量方法》.pdf
  • IEC 60392-1972 Guide for the drafting of specifications for microwave ferrites《微波铁氧体规范起草编写指南》.pdf IEC 60392-1972 Guide for the drafting of specifications for microwave ferrites《微波铁氧体规范起草编写指南》.pdf
  • IEC 60393-1-2008 Potentiometers for use in electronic equipment - Part 1 Generic specification《电子设备用电位器.第1部分 总规范》.pdf IEC 60393-1-2008 Potentiometers for use in electronic equipment - Part 1 Generic specification《电子设备用电位器.第1部分 总规范》.pdf
  • IEC 60393-3-1-1992 Potentiometers for use in electronic equipment part 3 blank detail specification rotary precision potentiometers assessment level E《电子设备用电位器 第3-1部分 空白详细规范 旋转精密电位器 评定水平E》.pdf IEC 60393-3-1-1992 Potentiometers for use in electronic equipment part 3 blank detail specification rotary precision potentiometers assessment level E《电子设备用电位器 第3-1部分 空白详细规范 旋转精密电位器 评定水平E》.pdf
  • IEC 60393-3-1992 Potentiometers for use in electronic equipment part 3 sectional specification rotary precision potentiometers《电子设备用电位器 第3部分 分规范 旋转精密电位器》.pdf IEC 60393-3-1992 Potentiometers for use in electronic equipment part 3 sectional specification rotary precision potentiometers《电子设备用电位器 第3部分 分规范 旋转精密电位器》.pdf
  • IEC 60393-4-1-1992 Potentiometers for use in electronic equipment part 4 blank detail specification singel-turn rotary power potentiometers assessment level E《电子设备用电位器 第4-1部分 空白详细规范 单圈旋转功率电位器 评定水平.pdf IEC 60393-4-1-1992 Potentiometers for use in electronic equipment part 4 blank detail specification singel-turn rotary power potentiometers assessment level E《电子设备用电位器 第4-1部分 空白详细规范 单圈旋转功率电位器 评定水平.pdf
  • 相关搜索

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

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