[YTU]_2636 ( B3 指向基类的指针访问派生类的成员函数)

#include <iostream>

#include <string>

using namespace std;

class Person

{

public:

    Person(stringna,int a):name(na),age(a){}

    virtual voidshow()=0;

protected:

    string name;

    int age;

};

class Leader:public Person

{

private:

    string job;

    string dep;

public:

    Leader(stringna,int a,string j,string d):Person(na,a),job(j),dep(d){}

    voidshow(){cout<<name<<" is "<<job<<" in"<<dep<<" department."<<endl;}

};

class Engineer: public Person

{

    stringmajor1;    //专业

public:

    Engineer (stringna, int a, string m): Person(na, a),major1(m) { }

    voidshow(){cout<<name<<" is an engineer of"<<major1<<" major."<<endl;}

};

int main()

{

    Person *p;

    string name, job,dept,major;

    int age;

    cin>>name>>age>>job>>dept;

    p=newLeader(name,age,job,dept);

    p->show();

    cin>>name>>age>>major;

    p=new Engineer(name,age,major);

    p->show();

    return 0;

}

posted @ 2017-06-09 10:08  衣带渐宽、为伊憔悴  阅读(101)  评论(0编辑  收藏  举报