标签:
领导类(Leader)和工程师类(Engineer)都是人类(Person)的派生类。请将下面代码中空缺的部分填上,使main函数中能通过指向Person类的指针,调用Leader和Engineer类中各自的成员函数show,输出相关的信息。
Input
第一行输入是某领导的信息,如“Wang 40 Manager Sales”表示40岁的王(Wang)领导是销售部(Sales)的经理(Manager)。
第二行输入某工程师的信息,如“Zhang 30 Software”表示30岁的张(Zhang)是工程师,他的专业是软件(Software)
Output
规定的格式输出信息,请注意空格及最后的标点。
Sample Input
Wang 40 Manager Sales
Zhang 30 Software
Sample Output
Wang is Manager in Sales department.
Zhang is an engineer of Software major.
#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;} }; //************* 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; }
virtual void show() =0;
标签:
原文地址:http://blog.csdn.net/mayuko2012/article/details/46628033