首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请使用VC6或使用【答题】菜单打开考生文件夹prog2下的工程prog2,此工程中包含一个程序文件main.cpp,其中有“班级”类Class和“学生”类Student的定义,还有主函数main的定义。在主函数中定义了两个“学生”对象,他们属于同一班级。程
请使用VC6或使用【答题】菜单打开考生文件夹prog2下的工程prog2,此工程中包含一个程序文件main.cpp,其中有“班级”类Class和“学生”类Student的定义,还有主函数main的定义。在主函数中定义了两个“学生”对象,他们属于同一班级。程
admin
2017-02-21
23
问题
请使用VC6或使用【答题】菜单打开考生文件夹prog2下的工程prog2,此工程中包含一个程序文件main.cpp,其中有“班级”类Class和“学生”类Student的定义,还有主函数main的定义。在主函数中定义了两个“学生”对象,他们属于同一班级。程序展示,当该班级换教室后,这两个人的教室也同时得到改变。请在横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为:
改换教室前:
学号:0789姓名:张三班级:062113教室:521
学号:0513姓名:李四班级:062113教室:521
改换教室后:
学号:0789姓名:张三班级:062113教室:311
学号:0513姓名:李四班级:062113教室:311
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容。
#include<lostream>
using namespace std;
class class{ //“班级”类
public:
C1ass(const char * id,const char * room){
strcpy(class_id,id);
//**********found**********
}
const char * getClasSID() const { return class_id;}//
返回班号
//**********found**********
const char * getClass room()
const{_______} //返回所在教室房号
void changeRoomTo(const char
* new_room) { //改换到另一个指定房号的教室
strcpy(classroom,new_room);
}
private:
char class_id[20];//班号
char classroom[20]; //所在教室房号
};
class Student{ //“学生”类
char my_id[10];//学号
char my_name[20];//姓名
Class &my_class; //所在教室
public:
//**********found**********
Student(const char * the_id,const char * the_name,Class&the_class):_______{
strcpy(my_id,the_id);
strcpy(my_name,the_ame);
}
const char * getID()const{return my_id;}
const char *getName()const{return my_name;}
Class getClass()const{return my_class;}
};
void showStudent (Student * stu){
cout<<"学号:"<<stu->getID()
<<"";
cout<<"姓名:"<<stu->getName()
<<"";
cout<<"班级:"<<stu->getClass().getClassID()<<"";
cout<<"教室:"<<stu->getClass().getClassroom()<<
end1;
}
int main(){
Class cla("062113","521");
Student Zhang("0789","张三",cla),Li("0513","李四",cla);
cout<<"改换教室前:"<<end1;
showStudent(&Zhang);
showStudent(&Li);
//0 62113班的教室由521改换到311
//**********found**********
cout<<"改换教室后"<<end1;
showStudent(&Zhang);
showStudent(&Li);
return 0;
}
选项
答案
(1)strcpy(classroom,room); (2)retum classroom; (3)my_class(the_class) (4)cla.changeRoomTo("311");
解析
(1)主要考查考生对strcpy函数的掌握情况,根据上一条语句:strcpy(class_id,id);可知,这条语句要复制字符串room,因此使用strcpy函数复制,即strcpy(classroom,room);。
(2)主要考查考生对函数返回值的掌握情况,根据函数要求:返回所在教室房号及函数要求返回的类型为const char *,可以得出这里直接使用return语句返回classroom即可。
(3)主要考查考生对构造函数的掌握情况,先看函数体中:
strcpy(my_id,the_id);
strcpy(my_name,the_name);
可知只有参数Class &the_class未使用,因此在这里使用成员列表初始化给my_class赋初始值。
(4)主要考查考生对成员函数调用的掌握,程序要求062113班的教室由521改换到311。在类Class中已经定义了函数:void changeRoomTo(const char * new_room),因此直接调用函数changeRoomTo即可。
转载请注明原文地址:https://kaotiyun.com/show/LBAp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
在C++中,打开一个文件,就是将这个文件与一个()建立关联,关闭一个文件,就是取消这种关联。
对于拷贝初始化构造函数,正确的描述是()。
一个()允许用户为类定义一种模式,使得类中的某些数据成员、某些成员函数的参数和返回值能取任意数据类型。
有以下程序#include<iostream.h>voidmain(){inta=5,b=0,c=0;if(a=b+c)cout<<"***"<<endl;elsecout<<"$$$"<<<endl;}
如果表达式++x/y中,++是作为友元函数重载的,/是作为成员函数重载的,则该表达式还可为()。
下列函数的运行结果是#include<iostream.h>intf(inta,intB){intc;if(a>b)c=1elseif(a=b)c=0;
软件开发离不开系统环境资源的支持,其中必要的测试数据属于
将x+y*z中的“+”用成员函数重载,“*”用友元函数重载应写为【】。
软件危机出现于60年代末,为了解决软件危机,入们提出了【】的原理来设计软件,这就是软件工程诞生的基础。
软件工程研究的内容主要包括【】技术和软件工程管理。
随机试题
主要经消化道传播的肝炎病毒是
患者男性,31岁。咳嗽,呈刺激性,干咳,偶有咳少量黏稠痰,受寒冷刺激加重,伴气促,每天晚间、清晨均有剧咳而影响睡眠,用过青霉素、氨苄西林、头孢菌素和多种祛痰止咳剂未能缓解。查体:双肺散在哮鸣音,心脏(-)此病人最可能的诊断是
抗凝血类灭鼠药对机体血液系统的毒性表现是
A.5%B.20%C.40%D.50%E.80%下颌中切牙唇舌向双根管者约为
水箱中的水从一扩散短管流入大气(图3-37)。若喉部直径d1=100mm,该处绝对压强p1=49035Pa,出口直径d2=150mm,水头H为()m。假定水头损失可忽略不计。
机床的具体技术经济指标包括()等。
根据税收征收管理法律制度的规定,下列各项中,不属于征税主体权利的是()。(2015年)
以下不属于劳动法基本原则特点的是()。
事业单位现行的(),对应管理岗位十级职员岗位。
在标准模块的变量定义区域中,用___________关键字说明的变量属于全局范围的变量。
最新回复
(
0
)