首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请打开考生文件夹下的解决方案文件proj2,此工程中包含一个程序文件main.cpp,其中有“班级”类Class和“学生”类Student的定义,还有主函数main的定义。在主函数中定义了两个“学生”对象,他们属于同一班级。程序展示,当该班级换教室后,这两
请打开考生文件夹下的解决方案文件proj2,此工程中包含一个程序文件main.cpp,其中有“班级”类Class和“学生”类Student的定义,还有主函数main的定义。在主函数中定义了两个“学生”对象,他们属于同一班级。程序展示,当该班级换教室后,这两
admin
2020-04-07
50
问题
请打开考生文件夹下的解决方案文件proj2,此工程中包含一个程序文件main.cpp,其中有“班级”类Class和“学生”类Student的定义,还有主函数main的定义。在主函数中定义了两个“学生”对象,他们属于同一班级。程序展示,当该班级换教室后,这两个人的教室也同时得到改变。请在横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为:
改换教室前:
学号:0789姓名:张三班级:062113教室:521
学号:0513姓名:李四班级:062113教室:521
改换教室后:
学号:0789姓名:张三班级:062113教室:311
学号:0513姓名:李四班级:062113教室:311
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容。
#include<iostream>
using namespace std;
class Class{ //“班级”类
public:
Class(const char * id,const char * Eoom){
strcpy(class id,id);
//**********found**********
_____________
}
const char * getClassTD()const { return class_id; } //返回班号
//**********found**********
const char * getClassEoom()const{________} //返回所在教室房号
void changeRoomTo(const char* new_Eoom) { //改换到另一个指定房号的教室
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_name);
}
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() <<endl;
}
int main(){
ClasS cla("062113","521");
Student Zhang("0789","张三",cla),Li("0513","李四",cla);
cout<<"改换教室前:"<<endl;
showStudent(&Zhang);
showStudent(&Li);
//062113班的教室由521改换到311
//********** found **********
________
cout<<"改换教室后:"<<endl;
showStudent(&Zhang);
showStudent(&Li);
return 0;
}
选项
答案
(1)strcpy(classroom,room); (2)return 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)主要考查考生对成员函数调用的掌握,程序要求052113班的教室由521改换到311。在类Class中已经定义了函数:void changeRoomTo(const char * new_room),因此直接调用函数changeRoomTo即可。
转载请注明原文地址:https://kaotiyun.com/show/Aw8p777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
假定动态分配一个类型为Worker的具有n个元素的数组,并由P指向这个动态数组,如果要释放这个数组,则使用的语句为【】。
执行这个程序的输出结果是();#include<iostream>usingnamespacestd;classTestClass1{public:voidfun1(){cout<<"
关系数据模型由关系数据结构、关系操作集合和【】三大要素组成。
要建立文件流并打开当前目录下的文件file.dat用于输入,下列语句中错误的是
C++中运算符优先级由高到低排列正确的是
下列关于getline()函数的描述中,不正确的是()。
如果一个类至少有一个纯虚函数,那么就称该类为()。
在C++中既可以用于文件输入又可以用于文件输出的流类是
下列类模板的定义中语法格式错误的是
随机试题
急性肾炎镜下血尿可持续
A.恶性纤维组织细胞瘤B.横纹肌肉瘤C.滑膜肉瘤D.平滑肌肉瘤E.脂肪肉瘤最常见于腹腔和腹膜后区的软组织肉瘤是
急性有机磷农药中毒最主要的死因
维生素C注射液【处方】维生素C104g依地酸二钠0.05g碳酸氢钠49g亚硫酸氢钠2g注射用水加至1000ml处方中依地酸二钠的作用是()
某货运公司2009年拥有载货汽车25辆、挂车10辆,自重吨位均为20吨;3辆四门六座客货两用车,载货自重吨位为3吨;小轿车2辆。该公司所在省规定载货汽车年纳税额每吨30元,9座以下乘人汽车年纳税额每辆520元。该公司2009年应缴的车船税为()元。
人伤感时,会见花落泪,对月伤怀,这种情绪状态属于激情。()
Rescuershavefoundthebodiesofover130peoplekilledintwoferrydisastersinBangladesh.Theaccidentshappenedduringas
据2011年第二季度中央银行货币政策报告称:“6月末,基础货币余额为20.3万亿元(2010年6月末为15.4亿元),比年初增加2.1万亿元,同比增长32.7%;货币乘数为3.847,比上年同期低0.53。”请根据上述资料回答:(对外经济贸
【26】【34】
Readingisthoughttobeakindofconversationbetweenthereaderandthetext.Thereaderputsquestions,asitwere,tothet
最新回复
(
0
)