首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请使用VC6或使用【答题】菜单打开考生文件夹prog2下的工程prog2,此工程中包含一个程序文件main.cpp,其中有“班级”类Class和“学生”类Student的定义,还有主函数main的定义。在主函数中定义了两个“学生”对象,他们属于同一班级。程
请使用VC6或使用【答题】菜单打开考生文件夹prog2下的工程prog2,此工程中包含一个程序文件main.cpp,其中有“班级”类Class和“学生”类Student的定义,还有主函数main的定义。在主函数中定义了两个“学生”对象,他们属于同一班级。程
admin
2016-08-19
42
问题
请使用VC6或使用【答题】菜单打开考生文件夹prog2下的工程prog2,此工程中包含一个程序文件main.cpp,其中有“班级”类Class和“学生”类Student的定义,还有主函数main的定义。在主函数中定义了两个“学生”对象,他们属于同一班级。程序展示,当该班级换教室后,这两个人的教室也同时得到改变。请在横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为:
改换教室前:
学号:0789姓名:张三班级:062113教室:521
学号:0513姓名:李四班级:062113教室:521
改换教室后:
学号:0789姓名:张三班级:062113教室:311
学号:0513姓名:李四班级:062113教室:311
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容。
1 #include
2 using namespace std;
3 class Class{//"班级"类
4 public:
5 C1ass(const char*id,const char*room){
6 strcpy(class id,id);
7 //**********found**********
8 _____
9 }
10 const char*getClassID()const{re-turn class_id;}//返回班号
11 //**********found**********
12 const char*getClassroom()const{____}//返回所在教室房号
13 void changeRoomTo(const char *new_room) { //改换到另一个指定房号的教室
14 strcpy(classroom,new room);
15 }
16 private:
17 char class id[20]; //班号
18 char classroom[20]; //所在教室房号
19 };
20 class Student{ //“学生”类
21 char my id[1 0]; //学号
22 char my name[2 0]; //姓名
23 Class&my class; //所在教室
24 public:
25 //**********found**********
26 Student(const char*the_id,const char*the_name,Class&the_class):_______{:
27 strcpy(my_id,the_id);
28 strcpy(my_name,the_name);
29 }
30 const char*getID()const{return my id;}
31 const char*getName()const{re-turn my_name;}
32 Class getClass()const(return my_class;}
33 };
34 void showStudent(Student*stu){
35 cout<<"学号:"<
getID()<<…" ";
36 cout<<"姓名:"<<stu一>getName()<<" ";
37 cout<<"班级:"<
getClas s().
getClassID()<<" ";
38 cout<<"教室:"<
getClass().getClassroom()<
39 }
40 int main(){
41 Class cla("0 62113","521");
4 Student Zhang("0789","张三",cla),Li("0513","李四",cla);
43 cout<<”改换教室前:”<
44 showStudent(&Zhang);
45 showStudent(&Li);
46 //062113班的教室由521改换到311
47 //**********found**********
48 ______
49 cout<<"改换教室后:"<
50 showStudent(&Zhang);
51 showStudent(&Li);
52 return 0;
53 }
选项
答案
(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://jikaoti.com/ti/87E0FFFM
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
若有0~9之间不同的3个数构成一个三位数,下面程序将统计出共有多少种方法,请填空完成此程序。#include<iostream>usingnamespacestd;intmain(){i
下列叙述中,正确的是()。
一个类的友元函数能够访问该类的
数据库的设计通常可以分为这样4个步骤:需求分析、概念设计、【】和物理设计。
函数sstrcmp()的功能是对两个字符串进行比较。当s所指字符串和t所指字符串相等时,返回值为0:当s所指字符串大于t所指字符串时,返回值大于0;当s所指字符串大于t所指字符串时,返回值大于0(功能等同于strcmp())。请填空。intSs
下面描述中,符合结构化程序设计风格的是______.
当使用带参数的操作子时,程序中必须包含头文件______。
C++语言中关键字运算符有new,delete和______。
若要打开二进制文件binfile写方式,输出到文件尾则应为:ofstreambfile("binfile",______);
拷贝构造函数的参数一般是
随机试题
刀具在进给运动方向上相对工件的位移量称为背吃刀量。( )
油气田分为()油气田和碳酸盐岩油气田两大类。
下列作品中,依《诗经》之例,取首句的头几个字为题的作品是()
戴望舒曾因发表了著名的诗作《雨巷》而被称为______诗人。
具有溶血作用的苷类化合物为
冷沉淀的制备是指新鲜冰冻血浆在何种温度下制备的
Sally’sneverseenaplayintheShanghaiGrandTheatre,______?
①科学进步的简单模式是提出理论,并从中推导出假设,然后假设接受各种技术或方法的检验。如果假设通过了检验,该理论就得到了某种程度的确证;如果假设被实验证伪,这个理论就得做出某种程度的改变,或者被一个新理论所取代。②科学中有很多理论已经被充分证实并被
下列关于2007—2009年各产业、行业用人需求变化的说法与资料相符的是()。
(1+p)12-1
最新回复
(
0
)