请使用VC6或使用【答题】菜单打开考生文件夹prog2下的工程prog2,此工程中包含一个程序文件mmn.cpp,其中有“班级”类Class和“学生”类Student的定义,还有主函数main的定义。在主函数中定义了两个“学生”对象,他们属于同一班级。程序

admin2015-06-27  42

问题 请使用VC6或使用【答题】菜单打开考生文件夹prog2下的工程prog2,此工程中包含一个程序文件mmn.cpp,其中有“班级”类Class和“学生”类Student的定义,还有主函数main的定义。在主函数中定义了两个“学生”对象,他们属于同一班级。程序展示,当该班级换教室后,这两个人的教室也同时得到改变。请在横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为:
改换教室前:
学号:0789姓名:张三班级:062113教室:521
学号:0513姓名:李四班级:062113教室:521
改换教室后:
学号:0789姓名:张三班级:062113教室:311
学号:0513姓名:李四班级:062113教室:311
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容。
#include
usingnamespacestd;
classClass(//“班级”类
public:
Class(constchar*id,constchar*
room){
strcpy(classid,id);
f}**********found**********
}
constchar*getClassID()const{returnclassid;}//返回班号
//**********found**********
constchar*getClassroom()const
{________}//返回所在教室房号
voidchangeRoomTo(constchar*new
room){//改换到另一个指定房号的教室
strcpy(classroom,new_room);
}
private:
charclassid[20];//班号
charclassroom[20];//所在教室房号
};
classStudent{//“学生”类
charmyid[10];//学号
charmyname[20];//姓名
Class&myclass;//所在教室
public:
//**********found**********
Student(constchar*theid,const
char*the_name,Class&the_class):
________;
strcpy(my_id,the_id);
strcpy(my_name,the_name);
}
constchar*getID()const{returnmy_id;}
constchar*getName()const{returnmy_name;}
ClassgetClass0const{returnmy_class;}
};
voidshowStudent(Student*stu){
cout<<"学号:"<getID()<<"";
cout<<"姓名:"<getName()<<"";
cout<<"班级:"<getClass().
getClassID()<<"";
cout<<"教室:"<getClass().
getClassroom()<}
intmain(){
ClasScla("062113","521");
StudentZhang("0789","张三",cla),
Li("0513","李四",cla);
cout<<"改换教室前:"<showStudent(&Zhang);
showStudent(&Li);
//062113班的教室由521改换到311
//**********found**********
cout<<--改换教室后:"<showStudent(&Zhang);
showStudent(&Li);
return0;
}

选项

答案(1)strcpy(classroom,room); (2)returnclassroom; (3)my_class(the_class) (4)cla.changeRoomTo("311");

解析 (1)主要考查考生对strcpy函数的掌握情况,根据上一条语句:strcpy(class_id,id);可知,这条语句要复制字符串room,因此使用strcpy函数复制,即strcpy(classroom,room);。
(2)主要考查考生对函数返回值的掌握情况,根据函数要求:返回所在教室房号及函数要求返回的类型为constchar*,可以得出这里直接使用return语句返回classroom即可。
(3)主要考查考生对构造函数的掌握情况,先看函数体中:
strcpy(my_id,the_id);
strcpy(my_name,the_name);
可知只有参数Class&the_class未使用,因此在这里使用成员列表初始化给my_class赋初始值。
(4)主要考查考生对成员函数调用的掌握,程序要求062113班的教室由521改换到311。在类Class中已经定义了函数:voidehangeRoomTo(constchar*new_room),因此直接调用函数changeRoomTo即可。
转载请注明原文地址:https://kaotiyun.com/show/21BD777K
0

随机试题
最新回复(0)