请打开考生文件夹下的解决方案文件proj2,此工程中包含一个源程序文件main.cpp,其中有“房间”类Room及其派生出的“办公室”类Office的定义,还有主函数main的定义。请在程序中“//****found****”下的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
办公室房间号:308
办公室长度:5.6
办公室宽度:4.8
办公室面积:26.88
办公室所属部门:会计科
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include<iostream>
using namespace std;
class Room( //“房间”类
int room no; //房间号
double length; //房间长度(m)
double width; //房间宽度(m)
public:
Room(int the_room no,double the_length,double the_ width):room no(th_room_no),
length(the length),width(the width)()
int theRoomNo()const{return room_no;}
//返回房间号
double theLength()const{return length;}//返回房间长度
double theWidth()const{return width;}//返回房间宽度
//**************found**************
double theArea()const{_____________
}//返回房间面积(矩形面积)
};
class Office:public Roomt//“办公室”类
char*depart; //所属部门
public:
Office(int the _room _no,double the _length,double the_width,const char *the _depart)
//**************found**************
:_____________{
depart=new char[ strlen(the depart)+1];
//**************found**************
strcpy(____________);
}
~Office(){delete[]
depart;}
const char*theDepartment()
const { return depart;}//返回所属部门
};
int main(){
//**************found**************
Office___________;
cout<<"办公室房间号:"<<an office.theRoomNo()<<endl
<<"办公室长度:"<<an office.theLength()<<endl
<<"办公室宽度:"<<an_office.theWidth()<<endl
<<”办公室面积:"<<an_office.theArea()<<endl
<<"办公室所属部门:"<<an_office.theDepartmenI()<<endl;
return 0;
}
【正确答案】(1)return length*width;
(2)Room(the_room_no,the_length,the_width)
(3)depart,the_depart
(4)an_office(308,5.6,4.8,"会计科")
【答案解析】(1)主要考查考生对成员函数的掌握,题目要求返回房间面积(矩形面积)。由此可知空格部分要填写的是一个return语句,返回房间面积,也就是length*width,因此可以得出程序return length*width;。
(2)主要考查考生对派生类的构造函数的掌握,派生类的构造函数要使用成员列表初始法先对基类进行初始化。
(3)考查strepy函数,由前一条语句depart=newchar[strlen(the_delmn)+1];可知,程序给depart分配了长度为the_depart串长加1的空问,因此要复制字符串the_deparl串到depart,直接填写strepy(depart,the_depart)即可。
(4)主要考查考生对类的掌握,题目要求输出的结果为:
办公室房间号:308
办公室长度:5.6
办公室宽度:4.8
办公室面积:26.88
办公室所属部门:会计科
由Office类的构造函数可知要定义的一个office类的对象为an_office(308,5.6,4.8,"会计科")。