文档详情

C程序设计与应用基础第五章 多态性习题答案.doc

发布:2017-12-03约9.38千字共14页下载文档
文本预览下载声明
第五章 多态性 1、填空题 1)在一个成员函数内调用一个虚函数时,对该虚函数的调用进行___动态______联编。 2)动态联编是在__虚函数___的支持下实现的,它通过___指针和引用__来调用该函数操作。 3)下列程序的运行结果如下: Bases cons. Deriveds cons. Deriveds des. Bases des. 根据结果将程序补充完整。 #incude iostream.h class Base { public: Base(){coutBases cons.endl;} ___varitual ~Base()___{coutBases des.endl;} }; class Derived:public Base { public: Derived(){coutDeriveds cons.endl;} ~Derived(){coutDeriveds des.endl;} }; void main() { Base *Ptr=___new Derived;__ delete ptr; } 4)C++中__不支持___虚构造函数,但__支持___虚析构函数 5)带有__纯虚函数___的类称为抽象类,它只能作为___基类___来使用。 6)下列程序的运行结果如下: Derive1s Print() called. Derive2s Print() caIIed. 根据结果将程序补充完整。 #include iostream.h class Base { public: Base(int I){b=I;} _____vitual void print ()______ protected: int b; } class Derive1:public Base { public: _____Derivel1(int I):Base(I){}______ void Print() { coutDerive1s Print() called.endl; } }; class Derive2:public Base { ______Derivel2(int I):Base(I){} void Print() { coutDerive1s Print() called.endl; }______ }; void fun(______Base* Obj______) { obj-Print(); }; void main() { ______Dervel1* d1=new Dervel1(1); Dervel2* d1=new Dervel2(2);______ fun(d1); fun(d2); } 2、编程题 1)设计一个小型公司的人员信息管理系统。该公司主要有四类人员:经理、兼职技术人员、销售经理和兼职推销员。要求存储这些人员的姓名、编号、级别、当月薪水总额并显示全部信息,具体要求如下所述。 ___①___人员编号基数为1000,每输入一个人员信息,编号顺序加1。 ___②___程序具有对所有人员提升级别的功能。经理为4级,兼职技术人员和销售经理为3级,推销员为1级。 ___③___月薪计算方法是:经理拿固定月薪8000元;兼职技术人员按每小时100元领取月薪;兼职推销员的月薪按该推销员当月销售额的4%提成;销售经理既拿固定月薪也领取销售提成,固定月薪为5000元,销售提成为所管辖部门当月销售额的5‰。 答案: #include iostream.h #include string.h class employee { public: employee(); ~employee(){delete[] name;} virtual void pay()=0; virtual void promote(int increment=0){grade+=increment;} virtual void display()=0; protected: char *name; int no; int grade; double Pay; static int MaxNo; }; class technician:public employee { public: technician(){hourlyRate=100;} void promote(int){employee::promote(2);} void pay(); void display(); private: double hourlyRate; int workHours; }; class salesman:virtual public employee { public: salesman(){Com
显示全部
相似文档