问答题
请使用VC6或使用【答题】菜单打开
prog2下的工程prog2,此工程中包含一个程序文件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 * room) {
strcpy(class_id, id);
// *******found*******
______
}
const char * getClassID() const {return class_id;} //返回班号
// *******found*******
const char * getClassroom() 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_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;
}