三、简单应用题(24分)
42、请使用VC6或使用【答题】菜单打开考生文件夹pr092下的工程pros2。此工程中包含一个程序文件main.cpp,其中有“部门”类Department和“职工”类Staff的定义,还有主函数main的定义。在主函数中定义了两个“职工”对象,他们属于同一部门。程序展示,当该部门改换办公室后,这两个人的办公室也同时得到改变。请在程序中的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
改换办公室前:
职工号:0789姓名:张三部门:人事处办公室:521
职工号:0513姓名:李四部门:人事处办公室:521
改换办公室后:
职工号:0789姓名:张三部门:人事处办公室:311
职工号:0513姓名:李四部门:人事处办公室:311
注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include
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<<”职工号:”< cout<<”姓名:”< cout<<”部门:”< cout<<”办公室:”< int main(){
Department dept(”人事处”,”521”);
Staff Zhang(”0789”,”张三”,dept),Li(”0513”,”李四”,dept);
cout<<”改换办公室前:”< showStaff(Zhang); showStaff(Li);
//人事处办公室由521搬到311 //**********found********** ———————————————————————一
cout<<”改换办公室后:”< showStaff(Zhang); showStaff(Li);
return 0; }
34、 有如下程序:
#inClude using namespaCe std; Class Sample{
friend long fun(Sample S); publiC:
Sample(10ng A.{x=a;} private:
long X;
}; long fun(Sample S){
if(S.x<2)return l;
return S.X*fun(Sample(s.x-1)); }
int main( ) {
int sum=0;
for(int i=0;i<6;i++) {sum+=fun(Sample(i));}
Cout< }
执行这个程序的输出结果是( )。
A.120
B.16
C.154
D.34
42、
请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,该工程中包含一个程序文件 main.cpp,其中有类CPolygon(“多边形”)、CRectangle(“矩形”)、CTriangle(“三角形”)的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。该程序的正确输出结果应为: 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。 #include
{tout<<——< int area(void){return(width *height);}
class CTriangle:public CPolygon{
int length;//三角形一边长 int height;//该边上的高
public:
CTriangle(int l,int h):length(1),height(h){}
//*********found*********
int area(void){return(——)/2;}
};
int main(){
CRectangle rect(4,5); CTriangle trgl(4,5);
//*********found********* ______ *ppolyl,* ppoly2; ppolyl=▭
ppoly2=&trgl;
ppolyl->printarea(); ppoly2->printarea(); retun 0;
相关推荐:
北京 | 天津 | 上海 | 江苏 | 山东 |
安徽 | 浙江 | 江西 | 福建 | 深圳 |
广东 | 河北 | 湖南 | 广西 | 河南 |
海南 | 湖北 | 四川 | 重庆 | 云南 |
贵州 | 西藏 | 新疆 | 陕西 | 山西 |
宁夏 | 甘肃 | 青海 | 辽宁 | 吉林 |
黑龙江 | 内蒙古 |