exercise15

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

12. Person为基类:虚函数为 dailywork() ~Person()
定义三个子类:Student Doctor(char* label) Driver(char* label)
主函数:定义基类指针数组,动态创建子类对象,调用成员函数,删除创建的对象。
#include
class Person
{
public:
Person(int a,char* b):id(a),name(b){}
virtual void dailywork(){cout<<"身份证 "<virtual ~Person(){cout<<"Person destructor\n";}
protected:
int id;
char* name;
};
class Student:public Person
{
public:
Student(int a,char* b):Person(a,b){}
virtual void dailywork(){Person::dailywork();cout<<" 任务 每天上学.\n";}
virtual ~Student(){cout<<"Student destructor\n";}
};
class Doctor:public Person
{
public:
Doctor(int a,char *b,char* c):Person(a,b),laber(c){}
virtual void dailywork(){Person::dailywork();cout<<" 职务 "<virtual ~Doctor(){cout<<"Doctor destructor\n";}
private:
char* laber;
};
class Driver:public Person
{
public:
Driver(int a,char *b,char* c):Person(a,b),laber(c){}
virtual void dailywork(){Person::dailywork();cout<<" 车牌号 "<virtual ~Driver(){cout<<"Driver destructor\n";}
private:
char *laber;
};
void main()
{
Student* t=new Student(100,"xiaoming");
Doctor* x=new Doctor(101,"lihua","Medical professor");
Driver * m=new Driver(102,"wangtian","1234");
Person* array[3];
array[0]=t;
array[1]=x;
array[2]=m;
for(int i=0;i<3;i++) array[i]->dailywork();
delete array[0];
delete array[1];
delete array[2];
return ;
}

相关文档
最新文档