#include <iostream>
#include <string>
using namespace std;
class Employee {
public:
Employee();
void display();
private:
static int base_id;
int id;
string name;
};
// 初始化静态数据成员
int Employee::base_id = 1000;
// Employee类的构造函数定义
Employee::Employee() {
id = ++base_id; // 自动分配员工编号,并增加基数
cout << "Input name:";
cin >> name;
}
// Employee类的显示函数定义
void Employee::display() {
cout << "ID:" << id << " Name:" << name << endl;
}
int main() {
Employee e1;
Employee e2;
e1.display();
e2.display();
return 0;
}
【输入形式】
参考的输入如下(提示“Input name:”):
Input name:zhangsan
Input name:lisi
【输出形式】
参考的输出如下:
ID:1001 Name:zhangsan
ID:1002 Name:lisi