想要实现两个类,分别表示雇员和经理两类人员,并满足以下程序给出的运行结果,求指点

公司中有两类人员:一般雇员(Employee)和经理(Manager)。每位雇员都有各自的基本信息,包括姓名、年龄、工龄、部门名称。每位经理除了具有这些基本信息外,还具有另外两个信息:职务级别、他所管辖的雇员信息。
int main()
{
Employee e1(“Jack”, 24, 2, “Development”);
Employee e2(“Susan”, 28, 4, “Development”);
Manager m(“Tom”, 30, 2, “Development”, 2);
m.addMember(&e1).addMember(&e2);
e1.printOn(); // display all data member information of Jack
e2.printOn(); // display all data member information of Susam
m.printOn(); // display all data member information of Tom 
cout << “Total” << Employee::count();
cout<< “employee’s object created.” << endl;
return 0;
}
程序运行结果:
This is Jack:
age: 24
Year: 2
Dept: Development
This is Susan:
age: 28
Year: 4
Dept: Development
This is Tom:
age: 30
Year: 2
Dept: Development
Level: 2
Members: Jack Susan 
Total 3 employee’s object created.
1.main函数体中一个字也不要更改

慕村9548890
浏览 179回答 2
2回答

烙印99

#include<iostream>#include<cstdlib>#include<string>using namespace std;class Employee{public:Employee(char *,int,int,char *);void printOn();static int count(); //调用静态成员的函数也必须是静态函数char *name;int age;int worktime;char *department;static int n;/*~Employee(){delete name;delete department;}*/ //使用该析构函数会抱内存错};int Employee::n=0;Employee::Employee(char *name,int age,int worktime,char *department){this->name=name;&nbsp;this->age=age;this->worktime=worktime;&nbsp;this->department=department;n++;}void Employee::printOn(){cout<<"This is "<<name<<endl<<"age "<<age<<endl<<"year "<<worktime<<endl<<"Dept "<<department<<endl;}&nbsp;&nbsp;int Employee::count(){return n;};class Manager:public Employee{public:Manager(char *,int,int,char *,int);Manager& addMember(Employee *);void printOn();private:static int m;int level;char *member[10];};int Manager::m=0;Manager::Manager(char *name,int age,int worktime,char *department,int level):Employee(name,age,worktime,department){this->level=level;}&nbsp;&nbsp;Manager& Manager::addMember(Employee *e){member[m++]=e->name;&nbsp;&nbsp;return *this;}void Manager::printOn(){Employee::printOn();cout<<"Level: "<<level<<endl<<"Members: ";for(int i=0;i<n-1;i++)cout<<member[i]<<" ";cout<<endl;}&nbsp;&nbsp;int main(){Employee e1("Jack", 24, 2, "Development");Employee e2("Susan", 28, 4, "Development");Manager m("Tom", 30, 2, "Development", 2);m.addMember(&e1).addMember(&e2);e1.printOn(); // display all data member information of Jacke2.printOn(); // display all data member information of Susamm.printOn(); // display all data member information of Tom&nbsp;cout << "Total " << Employee::count();cout<< " employee's object created." << endl;system("pause");return 0;}

HUWWW

#include<iostream>using namespace std;/*公司中有两类人员:一般雇员(Employee)和经理(Manager)。每位雇员都有各自的基本信息,包括姓名、年龄、工龄、部门名称。每位经理除了具有这些基本信息外,还具有另外两个信息:职务级别、他所管辖的雇员信息。请设计并实现两个类,分别表示雇员和经理两类人员,并满足以下程序给出的运行结果(注意:经理也是雇员)。&nbsp;*/class Employee{char* _xm;int _nl;int _gl;char* _bm;static int _n;public:void getXm(char*&);&nbsp;Employee(char*, int, int, char*);virtual ~Employee();virtual void printOn();static int count();};void Employee::getXm(char*& xm){strcpy(xm,_xm);}int Employee::_n=0;Employee::Employee(char* xm,int nl, int gl, char* bm){++_n;_xm=new char[strlen(xm)+1];strcpy(_xm, xm);_nl=nl;_gl=gl;_bm=new char[strlen(bm)+1];strcpy(_bm, bm);}Employee::~Employee(){if(_xm!=NULL)delete [] _xm;if(_bm!=NULL)delete [] _bm;}void Employee::printOn(){cout<<"This is "<<_xm<<": "<<endl;cout<<"age: "<<_nl<<endl;cout<<"Year: "<<_gl<<endl;cout<<"Dept: "<<_bm<<endl;}int Employee::count(){return _n;}class Manager : public Employee{int _dj;char* _gyxx;public:Manager(char*, int, int, char*, int);virtual ~Manager();void printOn();Manager& addMember(Employee*);};Manager::Manager(char* xm, int nl, int gl, char* bm,int dj):Employee(xm, nl, gl, bm){_gyxx=NULL;_dj=dj;}Manager::~Manager(){if(_gyxx!=NULL)delete [] _gyxx;}&nbsp;void Manager::printOn(){Employee::printOn();cout<<"Level: "<<_dj<<endl;cout<<"Members: "<<_gyxx<<endl;}Manager& Manager::addMember(Employee* e){char* a=new char[50];e->getXm(a);if(_gyxx!=NULL){char* t=new char[strlen(_gyxx)+1];strcpy(t, _gyxx);delete [] _gyxx;_gyxx=new char[strlen(t)+strlen(a)+6];strcpy(_gyxx, t);strcat(_gyxx, " ");strcat(_gyxx, a);delete [] t;}else{_gyxx= new char[strlen(a)+1];strcpy(_gyxx, a);}delete [] a;return *this;}int main(){Employee e1("Jack", 24, 2, "Development");Employee e2("Susan", 28, 4, "Development");Manager m("Tom", 30, 2, "Development", 2);m.addMember(&e1).addMember(&e2);e1.printOn(); // display all data member information of Jacke2.printOn(); // display all data member information of Susamm.printOn(); // display all data member information of Tomcout << "Total " << Employee::count();cout<< " employee’s object created." << endl;return 0;}
打开App,查看更多内容
随时随地看视频慕课网APP