2 回答
TA贡献1829条经验 获得超13个赞
#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;
this->age=age;
this->worktime=worktime;
this->department=department;
n++;
}
void Employee::printOn(){
cout<<"This is "<<name<<endl
<<"age "<<age<<endl
<<"year "<<worktime<<endl
<<"Dept "<<department<<endl;
}
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;
}
Manager& Manager::addMember(Employee *e){
member[m++]=e->name;
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;
}
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;
system("pause");
return 0;
}
TA贡献1874条经验 获得超12个赞
#include<iostream>
using namespace std;
/*
公司中有两类人员:一般雇员(Employee)和经理(Manager)。
每位雇员都有各自的基本信息,包括姓名、年龄、工龄、部门名称。
每位经理除了具有这些基本信息外,还具有另外两个信息:
职务级别、他所管辖的雇员信息。请设计并实现两个类,分别表示雇
员和经理两类人员,并满足以下程序给出的运行结果(注意:经理也是雇员)。
*/
class Employee
{
char* _xm;
int _nl;
int _gl;
char* _bm;
static int _n;
public:
void getXm(char*&);
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;
}
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 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;
}
- 2 回答
- 0 关注
- 178 浏览
添加回答
举报