国家二级C++机试(操作题)模拟试卷292
基本操作题
1.请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1,该工程中包含程序文件main.cpp,其中有类Door(“门”)和主函数main的定义。程序中位于每个“//ERROR****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
打开503号门…门是锁着的,打不开。
打开503号门的锁…锁开了。
打开503号门…门打开了。
打开503号门…门是开着的,无须再开门。
锁上503号门…先关门…门锁上了。
注意:只修改每个“//ERROR*********found*********”下的那一行,不要改动程序中的其他内容。
#include<iostream>
using namespace std;
class Door{
int num; //门号
bool closed; //true表示门关着
bool locked; //true表示门锁着
public:
Door(int num){
//ERROR *******found*******
num=this->num;
closed=locked=true;
}
bool isClosed()const { return closed;}
//门关着时返回true,否则返回false
bool isOpened()const{return!closed;}
//门开着时返回true,否则返回false
bool isLocked()const{return locked;}
//门锁着时返回true,否则返回false
bool i sUnlocked()const{return!locked;)
//门未锁时返回true,否则返回false
void open(){ //开门
cout<<end1<<\\
(1)this->num=num;
(2)if(!closed)
(3)void lock(){
解析:(1)主要考查考生对this指针的掌握,在构造函数中this指针指向的是当前类,因此要给num赋值使用语句this->num=num;完成。
(2)主要考查考生对if语句的掌握,先看类的私有成员中关于closed的定义:bool closed;//true表示门关着。再看下一条语句:cout<<\\
简单应用题
2.请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,该工程中包含一个程序文件main.cpp,其中有日期类Date、人员类Person及排序函数sortByName和主函数main的定义。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义和函数定义。此程序的正确输出结果应为:
按姓名排序
排序前
张三 男 出生日期:1978年4月20日
王五 女 出生日期:1965年8月3日
杨六 女 出生日期:1965年9月5日
李四 男 出生日期:1973年5月30日
排序后:
李四 男 出生日期:1973年5月30日
王五 女 出生日期:1965年8月3日
杨六 女 出生日期:1965年9月5日
张三 男 出生日期:1978年4月20日
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include<iostream>
using namespace std;
class Date{ //日期类
int year,month,day;//年、月、日
public:
Date(int year,int month,
int day):year(year),month(month),day(day){}
int getYear()const{return
year;}
int getMonth()const(return
month;}
int getDay()const{return
day;}
};
class Person{//人员类
char name[14];//姓名
bool is_male,//性别,为true时表示男性
Date birth_date;//出生日期
public:
Person(char *name,bool is
male,Date birth date)
//**********found**********
:_______
{
strcpy(this->name,name);
}
const char *getName()const{
return name;}
bool isMale()const { return is_male;}
Date getBi rthdate()const{return birth_date;}
//利用strcmp()函数比较姓名,返回一个正数、0或负数,分别表示大于、等于、小于
int compareName(const Person&p)const{
//**********found**********
_______}
void show(){
cout<<end1;
cout<<name<<\\
(1)is_male(is_male),birth_date(birth_date)
(2)return strcmp(name,p.getName());
(3)<<birth_date.getMonth()<<\\
解析:(1)主要考查考生对构造函数的掌握,由函数体内strcpy(this->name,name);可知,要使用成员列表初始化的成员为is_male和birth_date。
(2)主要考查考生对strcmp()函数的掌握,先看程序对该函数的功能要求:利用strcmp()函数比较姓名,返回一个正数、0或负数,分别表示大于、等于、小于。因为strcmp()函数的功能是比较字符串大小,因此可以直接被retum语句调用:return strcmp(name,p.getName());。
(3)主要考查考生对成员函数的掌握,程序的注释为:显示出生月,由此可以知道这里要输出出生月份,直接调用函数getMonth()即可。
综合应用题
3.请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中包含了类IntegerSet和主函数main的定义。一个Integer
本文档预览:3600字符,共5519字符,源文件无水印,下载后包含无答案版和有答案版,查看完整word版点下载