B3 指向基类的指针访问派生类的成员函数

B3 指向基类的指针访问派生类的成员函数

时间: 1ms        内存:128M

描述:

领导类(Leader)和工程师类(Engineer)都是人类(Person)的派生类。请将下面代码中空缺的部分填上,使main函数中能通过指向Person类的指针,调用Leader和Engineer类中各自的成员函数show,输出相关的信息
//************* begin *****************
#include <iostream>
using namespace std;
class Person
{
public:
    Person(string na, int a):name(na),age(a) {}
    ___(1)_____ show() ___(2)___;
protected:
    string name;  // 姓名
    int age;      // 年龄
};
class Leader: ___(3)_____ Person
{
private:
    string job;     // 职务
    string dep;     // 部门
public:
    Leader(string na, int a, string j, string d): ____(4)______{ }
    void show(){____(5)____}  
};
//************* end *****************
class Engineer: public Person
{
    string major;     // 专业
public:
    Engineer (string na, int a, string m): Person(na, a),major(m){ }
    void show(){cout<<name<<" is an engineer of "<<major<<" major."<<endl;}
};
int main()
{
    Person *p;
    string name, job, dept,major;
    int age;
    cin>>name>>age>>job>>dept;
    p=new Leader(name,age,job,dept);
    p->show();
    cin>>name>>age>>major;
    p=new Engineer(name,age,major);
    p->show();
    return 0;
}

输入:

第一行输入是某领导的信息,如“Wang 40 Manager Sales”表示40岁的王(Wang)领导是销售部(Sales)的经理(Manager)。
第二行输入某工程师的信息,如“Zhang 30 Software”表示30岁的张(Zhang)是工程师,他的专业是软件(Software)

输出:

规定的格式输出信息,请注意空格及最后的标点。

示例输入:

Wang 40 Manager Sales
Zhang 30 Software

示例输出:

Wang is Manager in Sales department.
Zhang is an engineer of Software major.

提示:

参考答案(内存最优[0]):

#include <iostream>
using namespace std;
class Person
{
public:
    Person(string na, int a):name(na),age(a) {}
    virtual void show()=0;
protected:
    string name;  // 姓名
    int age;      // 年龄
};
class Leader: public Person
{
private:
    string job;     // 职务
    string dep;     // 部门
public:
    Leader(string na, int a, string j, string d):Person(na,a){job=j;dep=d;}
    void show(){cout<<name<<" is "<<job<<" in "<<dep<<" department."<<endl;}
};


class Engineer: public Person
{
    string major;    // 专业
public:
    Engineer (string na, int a, string m): Person(na, a),major(m){ }
    void show(){cout<<name<<" is an engineer of "<<major<<" major."<<endl;}
};
int main()
{
    Person *p;
    string name, job, dept,major;
    int age;
    cin>>name>>age>>job>>dept;
    p=new Leader(name,age,job,dept);
    p->show();
    cin>>name>>age>>major;
    p=new Engineer(name,age,major);
    p->show();
    return 0;
}

参考答案(时间最优[0]):

//************* begin *****************
#include <iostream>
using namespace std;
class Person
{
public:
    Person(string na, int a):name(na),age(a) {}
    virtual void show() = 0;
protected:
    string name;  // 姓名
    int age;      // 年龄
};

class Leader: public Person    // (1)
{
private:
    string job;     // 职务
    string dep;     // 部门
public:
    Leader(string na, int a, string j, string d): Person(na, a),job(j), dep(d){ }
    void show(){cout<<name<<" is "<<job<<" in "<<dep<<" department."<<endl;}  //(2)
};
//************* end *****************
class Engineer: public Person
{
    string major;     // 专业
public:
    Engineer (string na, int a, string m): Person(na, a),major(m){ }
    void show(){cout<<name<<" is an engineer of "<<major<<" major."<<endl;}
};

int main()
{
    Person *p;
    string name, job, dept,major;
    int age;

    cin>>name>>age>>job>>dept;
    p=new Leader(name,age,job,dept);
    p->show();

    cin>>name>>age>>major;
    p=new Engineer(name,age,major);
    p->show();
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注