TXT

exercise15

By Jill Gonzales,2014-04-12 16:25
10 views 0
exercise15

    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<<" 职务 "<工作 天医治病人.\n";}

     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<<" 车牌号 "<工作 每天开车.\n";}

     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 ;

    }

Report this document

For any questions or suggestions please email
cust-service@docsford.com