首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,该工程中包含一个程序文件main.cpp,其中有日期类Date、人员类Person及排序函数sortByName和主函数main的定义。请在程序中的横线处填写适当的代码并删除横线,以
请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,该工程中包含一个程序文件main.cpp,其中有日期类Date、人员类Person及排序函数sortByName和主函数main的定义。请在程序中的横线处填写适当的代码并删除横线,以
admin
2021-06-10
28
问题
请使用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 getBirthdate()const{return birth_date;}
//利用strcmp()函数比较姓名,返回一个正数、0或负数,分别表示大于、等于、小于
int compareName(const Person &p)const{
//**********found**********
______________}
void show(){
cout<<endl:
cout<<name<<"<<(is_male?"男":"女")<<"<<"出生日期:"<<birth_date.getYear()<<"年"//显示出生年
//**********found**********
______________//显示出生月
<<birth_date.getDay()<<"日";//显示出生日
}
};
void sortByName(Person ps[],int size){
//将人员数组按姓名排列为升序
for(int i=0;i<size-1;i++){
//采用选择排序算法
int m=i;
for(int j=i+1;j<size;j++)
if(ps[j].compareName(ps[m])<0)
m=j;
if(m>i){
Person p=ps[m];
ps[m]=ps
;
ps
=p;
}
}
}
int main(){
Person staff[]={
Person("张三",true,Date(1978,4,20)),
Person("王五",false,Date(1965,8,3)),
Person("杨六",false,Date(1965,9,5)),
Person("李四",true,Date(1973,5,30))
};
const int size=sizeof(staff)/sizeof(staff[0]);
int i;
cout<<endl<<"按姓名排序";
cout<<endl<<"排序前:";
for(i=0;i<size;i++)staff
.show();
sortByName(staff,size);
cout<<endl<<endl<<"排序后:";
for(i=0;i<size;i++)staff
.show();
cout<<endl;
return0;
}
选项
答案
(1)is_male(is_male),birth_date(birth_date) (2)return strcmp(name,p.getName()); (3)<<birth_date.getMonth()<<"月"
解析
本题考查Date类和Person类,其中涉及构造函数、const函数、bool型私有成员及成员函数,以及strcmp()函数。
(1)主要考查考生对构造函数的掌握,由函数体内strcpy(this->name,name);可知,要使用成员列表初始化的成员为is_male和birth_date。
(2)主要考查考生对strcmp()函数的掌握,先看程序对该函数的功能要求:利用strcmp()函数比较姓名,返回一个正数、0或负数,分别表示大于、等于、小于。因为strcmp()函数的功能是比较字符串大小,因此可以直接被return语句调用:return strcmp(name,p.getName());。
(3)主要考查考生对成员函数的掌握,程序的注释为:显示出生月,由此可以知道这里要输出出生月份,直接调用函数getMonth()即可。
strcmp()函数、strcpy()函数、stden()函数等是经常会用到的系统函数,要了解各个函数的功能:stralt(连接)、strcly(复制)、stmmp(比较)、strlen(求长度)。
转载请注明原文地址:https://kaotiyun.com/show/eAfp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
已知类IMS中两个成员函数的声明为“voidlisten()const;”与“voidspeak();”,另有两个对象的定义为“IMSobj1,”与“constIMSobj2;”,则下列语句中,产生编译错误的是()。
下列关于派生类构造函数和析构函数的说法中,错误的是()。
下列语句中,与语句“n=(a>b?co>c?1:0):0);”的功能等价的是()。
下列关于函数参数的叙述中,正确的是()。
下列是重载乘法运算符的函数原型声明,其中错误的是()。
某二叉树中共有935个结点,其中叶子结点有435个,则该二叉树中度为2的结点个数为
一棵二叉树共有25个结点,其中5个是叶子结点,则度为1的结点数为
结构化程序设计的基本原则不包括()。
数据流图中带有箭头的线段表示的是( )。
下列选项中,正确的C++标识符是
随机试题
使用乙炔气瓶时,环境温度不应超过()。
A.短效巴比妥类B.苯巴比妥C.水杨酸类D.甲醇E.锂血液灌流可清除
淋巴瘤最重要的临床特征是
在护理健康教育中,下列哪一种不属于知识灌输方法()。
[2010年,第99题]将二进制小数0.1010101111转换成相应的八进制数,其正确结果是()。
下列关于多功能桥式起重机安全措施的说法,错误的是()。
青海玉树地震期间。干警王伟同志不顾亲人的遇难而去救别人。你是怎么看待的?
[2016年]已知函数f(x,y)=,则().
(2014下项管)在编制项目沟通计划的过程中,对项目干系人分析的目的不包括______。
上网查询资料时,目前最好用的搜索引擎是
最新回复
(
0
)