C程序设计与应用基础第五章多态性习题答案.doc_第1页
C程序设计与应用基础第五章多态性习题答案.doc_第2页
C程序设计与应用基础第五章多态性习题答案.doc_第3页
C程序设计与应用基础第五章多态性习题答案.doc_第4页
C程序设计与应用基础第五章多态性习题答案.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第五章 多态性1、填空题1)在一个成员函数内调用一个虚函数时,对该虚函数的调用进行_动态_联编。2)动态联编是在_虚函数_的支持下实现的,它通过_指针和引用_来调用该函数操作。3)下列程序的运行结果如下:Bases cons.Deriveds cons.Deriveds des.Bases des.根据结果将程序补充完整。#incude class Basepublic:Base()coutBases cons.endl;_varitual Base()_coutBases des.endl;class Derived:public Basepublic: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 class Basepublic:Base(int I)b=I;_vitual void print ()_protected:int b;class Derive1:public Basepublic:_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.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 #include class employeepublic: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 employeepublic:technician()hourlyRate=100;void promote(int)employee:promote(2);void pay();void display();private:double hourlyRate;int workHours;class salesman:virtual public employeepublic:salesman()CommRate=0.04;void promote(int)employee:promote(2);void pay();void display();protected:double CommRate;double sales;class manager:virtual public employeepublic:manager()monthlyPay=8000;void promote(int)employee:promote(3);void pay();void display();protected:double monthlyPay;class salesmanager:public manager,public salesmanpublic:salesmanager()monthlyPay=5000;CommRate=0.005;void promote(int)employee:promote(2);void pay();void display();int employee:MaxNo=1000;employee:employee()char namestr50;coutnamestr;name=new charstrlen(namestr)+1;strcpy(name,namestr);no=MaxNo+;grade=1;Pay=0.0;void technician:pay()cout请输入nameworkHours;Pay=hourlyRate*workHours;cout兼职技术人员name的编号为:not本月工资为:Payendl;void technician:display()cout兼职技术人员name的编号为:.not级别为:grade级,己付本月工资:Payendl;void salesman:pay()cout请输入.namesales;Pay=sales*CommRate;cout推销员name的编号为:not本月工资为:Payendl;void salesman:display()cout推销员name的编号为:not级别为:grade级,己付本月工资:Payendl;void manager:pay()Pay=monthlyPay;cout经理name的编号为:not本月工资为:Payendl;void manager:display()cout经理name的编号为:not级别为:grade级,己付本月工资:Payendl;void salesmanager:pay()cout请输入employee:namesales;Pay=monthlyPay+CommRate*sales;cout销售经理name的编号为:not本月工资为:Payendl;void salesmanager:display()cout销售经理name的编号为:not级别为:grade级,己付本月工资:Payendl;void main()manager m1;technician t1;salesmanager sm1;salesman s1;employee *emp4=&m1,&t1,&sm1,&s1;int i;for(i=0;ipromote();empi-pay();empi-display();2)编写一个程序,建立两种类型的表:队列与堆钱,使它们可以共用一个接口访问。答案:#include #include #include class Listpublic:List()head=tail=next=NULL;virtual void Store(int i)=0;virtual int Retrieve()=0;List *head, *tail, *next;int num;class Quene:public Listpublic:void Store(int i);int Retrieve();void Quene:Store(int i)List *item;item=new Quene;if(!item)coutAllocation error.num=i;if(tail)tail-next=item;tail=item;item-next=NULL;if(!head)head=tail;int Quene:Retrieve()int i;List *p;if(!head)coutQuene empty.num;p=head;head=head-next;delete p;return i;class Stack :public Listpublic:void Store(int i);int Retrieve();void Stack:Store(int i)List *item;item=new Stack;if(!item)coutAllocation error.num=i;if(head)item-next=head;head=item;if(!tail)tail=head;int Stack:Retrieve()int i;List *p;if(!head)coutStack empty.num;p=head;head=head-next;delete p;return i;void main()List *p;Quene qobj;p=&qobj;for(int i=0;iStore(i+1);coutQuene.;for(i=0;i3;i+)coutRetrieve();Stack sobj;p=&sobj;for(i=0;iStore(i+1);coutendlStack:;for(i=0;i3;i+)coutRetrieve();3)编写一个程序,先设计一个整数链表List类,然后从此链表派生出一个整数集合类set,在集合举中增加一个元素个数的数据项。集合类的插入操作与链表相似(只是不插入重复元素),并且插入后,表示元素个数的数据成员需增值。集合类的删除操作是在链表删除操作的基础上对元素个数做减1操作:而查找和显示操作是相同的。答案:#include enum boolean False,True;struct Nodeint val;Node *next;class Listpublic:List()nodes=0;List()Node *temp=nodes;for(Node *n=nodes;nodes!=0;)temp=nodes;nodes=nodes-next;delete temp;virtual boolean Insert(int val)Node *temp=new Node;if(temp!=0)temp-val=val;temp-next=nodes;nodes=temp;return True;elsereturn False;virtual boolean Delete(int val)if(nodes=0)return False;Node *temp=nodes;if(nodes-val=val)nodes=nodes-next;delete temp;return True;elsefor(Node *n=nodes;n-next!=0;n=n-next)if(n-next-val=val)temp=n-next;n-next=temp-next;delete temp;return True;return False;boolean IsContain(int val)if(nodes=0)return False;if(nodes-val=val)return True;elsefor(Node *n=nodes;n-next!=0;n=n-next)if(n-next-val=val)return True;return False;void Print()if(nodes=0)return;for(Node *n=nodes;n!=0;n=n-next)coutval ;coutInsert(10);p-Insert(20);p-Insert(30);p-Insert(40);p-Print();p-Delete(30);p-Print();p=&set1;p-Insert(12);p-Insert(22);p-Insert(32);p-Print();p-Delete(22);p-Print();4)某人喜欢饲养宠物。假定他拥有的放置宠物的窝的数目是固定的。请设计一个程序,使得某人可以饲养任意种类任意数目的宠物。要求能够知道现在饲养了多少只宠物, 每只宠物所在的位置及其种类和姓名。答案:#include #include #include #include class Animalpublic:Animal()name=NULL;Animal(char *n)name=strdup(n);virtual Animal()delete name;virtual void WhoAmI()coutGeneric Animal.endl;protected:char *name;class Cat: public Animal public:Cat()Cat(char *n):Animal(n)virtual void WhoAmI()coutI am a cat namednameendl;class Pig: public Animal public:Pig()Pig(char *n):Animal(n)virtual void WhoAmI()coutI am a pig namednameendl;class Dog: public Animalpublic:Dog()Dog(char *n):Animal(n)virtual void WhoAmI()coutI am a dog namednameendl;class Kennelpublic:Kennel(int max);virtual Kennel()delete Residents;int Accept(Animal *d);Animal *Release(int pen);void ListAnimals();private:int MaxAnimals,NumAnimals;Animal

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论