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);。