设一个公司的正式员工实行终生编号制;第一个正式员工的工号为1,第二个正式员工的工号为2,依次类推。试编写程序设计员工类,其基本属性包括员工工号、姓名、年龄和职称等。工号按上述规则自动生成。请设计必要的成员函数,完成此类。、
时间: 2024-05-06 18:19:51 浏览: 11
```
#include<iostream>
#include<string>
using namespace std;
class Employee {
public:
Employee() {
id = ++count;
}
Employee(string name, int age, string title) {
id = ++count;
this->name = name;
this->age = age;
this->title = title;
}
void display() {
cout << "工号:" << id << endl;
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "职称:" << title << endl;
}
private:
int id;
string name;
int age;
string title;
static int count;
};
int Employee::count = 0;
int main() {
Employee e1("张三", 25, "工程师");
Employee e2("李四", 30, "经理");
Employee e3("王五", 28, "助理");
e1.display();
e2.display();
e3.display();
return 0;
}
```