国家二级C++机试(操作题)模拟试卷374
基本操作题
1.请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1。该工程中包含程序文件main.cpp,其中有类CDate(“日期”)和主函数main的定义。程序中位于每个“//ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应为:
原日期:2005—9—25
更新后的日期:2006—4—1
注意:只修改每个“//ERROR****found****”下的那一行.不要改动程序中的其他内容。
#include
#include
USing namespace std;
class CDate//日期类
{
//ERROR*********found*********
protected:
CDaLe(){);
CDate(int d,int m,int y)
{
//ERROR*********found*********
SefiDafie(int day=d,int month=m,
int year=y);
};
void Display()://显示日期
void SetDate(int day,int month,
int year)
//设置日期
{m_nDay=day;m_nNonfih=month;m_
nYear=year;}
private:
int m_nDay;//日
int m nNonth;//月
int m nYear;//年
},
void CDate::Display()//显示日期
{
//ERROR*********found*********
cout<<m_nDay<<”一”<<m—nNonth
<<”一”<<m-nYear;
cout<<endl;
}
int main()
{
CDate d(2 5,9,2 0 05);
//调用构造函数初始化日期
cout<<“原日期:”;
d.Display();
d.SefiDate(1,4,2 0 0 6);
//N用成员函数重新设置日期
cout<<\\
(1)public:
(2)SetDate(d,m,y);
(3)cout<<m_nYear<<”一”<<m_nMonth<<”一”
<<m_nDay;
解析:(1)通过主函数中成员函数的调用可知这里应该为公有成员,而且构造函数必须为公有继承。
(2)主要考查考生对成员函数的掌握,程序在这里调用成员函数SetDate,直接把形参代入即可。
(3)题目要求输出原日期:2005—9—25。可以知道输出顺序为:先输出年,其次月,最后是日。
简单应用题
2.请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2。此工程中包含一个源程序文件main.cpp,其中有“房间”类Room及其派生出的“办公室”类Office的定义,还有主函数main的定义。请在程序中“//****found****”下的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
办公室房间号:308
办公室长度:5.6
办公室宽度:4.8
办公室面积:26.88
办公室所属部门:会计科
注意:只能在横线处填写适当的代码,不要改动程序中
的其他内容,也不要删除或移动“//****found****”。
#include
using namespace std;
class Room{ //“房间”类
int room no; //房间号
double length; //房间长度(m)
double width; //房间宽度(m)
public:
Room(int the room_no,double the_
length,double the_width):room_no
(the_room_no),length(the_length),
width(the_width){)
int theRoomNo()const{return room_
no;}
//返回房间号
double theLength()const{return
length;) //返回房间长度
double theWidth()const{return
width;} //返回房间宽度
//********** found**********
double theArea()const{______}
//返回房间面积(矩形面积)
};
class Office:public Room{//“办公室”类
char*depart; //所属部门
public:
Office(int the room no,double the
length,double the width,const
char*thedepart)
//********** found**********
:_________{
depart=new char | strlen(the depart)+1 j;
//**********found**********
strcpy(________);
}
一Office(){delete[]depart;)
const char*theDepartment()const {
return depart;}//返回所属部门
};
int main(){
//**********found**********
Office__________;
cout<<”办公室房间号:\\
(1)return length*width:
(2)Room(the_room_no,the_length,the_width)
(3)depart,the_depart
(4)an_office(308,5.6,4.8,”会计科”)
解析:(1)主要考查考生对成员函数的掌握,题目要求返回房间面积(矩形面积)。由此可知空格部分要填写的是一个return语句,返回房间面积,也就是length*width,因此可以得出程序return length*width;。
本文档预览:3600字符,共6934字符,源文件无水印,下载后包含无答案版和有答案版,查看完整word版点下载