题目内容:
请使用VC6或使用【答题】菜单打开考生文件夹pr092下的工程pros2。此工程中包含一个程序文件main.cpp,其中有“部门”类Department和“职工”类Staff的定义,还有主函数main的定义。在主函数中定义了两个“职工”对象,他们属于同一部门。程序展示,当该部门改换办公室后,这两个人的办公室也同时得到改变。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:改换办公室前:
职工号:0789姓名:张三部门:人事处办公室:521
职工号:0513姓名:李四部门:人事处办公室:521
改换办公室后:
职工号:0789姓名:张三部门:人事处办公室:311
职工号:0513姓名:李四部门:人事处办公室:311
注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include<iostream>
using namespace std;
class Department{ //“部门”类
public:
Department(const char*name,const char*office){
strcpy(this一>name,nanle);
//**********found**********
}
const char*getName()const{return name;}//返回部门名称
//**********found**********
const char*getOffice()const{________} //返回办公室房号
void changeOfficeTo(const char*office){ //改换为指定房号的另一个办公室
strcpy(this一>office,office);
}
private:
char name[20];//部门名称
char office[20];//部门所在办公室房号
};
class staff{//“职工”类
public:
//**********found**********
Staff(const char*my—id,const char木my_name,Department&my_dept):——{
strcpy(this一>staff id,my_id);
strcpy(this一>name,my_name);
}
const char*getlD()const{return staff_id;}
const char*getName()consl{return name;}
Department getDepartment()const{return dept;} char staff=id[10];//职工号
char name[20];//姓名
Department&dept;//所在部门
}; void showStaff(Staff&staff){
cout<<”职工号:”<<staff.getID()<<””;
cout<<”姓名:”<<staff.getName()<<" ";
cout<<”部门:”<<staff.getDepartment().getName()<<" ";
cout<<”办公室:”<<staff.getDepartment().getOffice()<<endl; }
int main(){
Department dept(”人事处”,”521”);
Staff Zhang(”0789”,”张三”,dept),Li(”0513”,”李四”,dept);
cout<<”改换办公室前:”<<endl;
showStaff(Zhang); showStaff(Li);
//人事处办公室由521搬到311 //**********found********** ———————————————————————一
cout<<”改换办公室后:”<<endl;
showStaff(Zhang); showStaff(Li);
return 0; }
参考答案:
答案解析: