设计一个职员类Employee,信息包括工号(num)、姓名(name)、性别(gender)、生日(birthday)。要求:(1) 从键盘输入职员人数n及每个人的信息;(2) 按出生年月日由小到大顺序输出每个职员的信息;(3) 统计男性和女性人数。
#include <iostream>
using namespace std;
class Employee {
private:
string num;
string name;
string gender;
string birthday;
public:
Employee(){}//默认构造函数
Employee(string n, string na, string gen, string bir){//构造函数
num = n;
name = na;
gender = gen;
birthday = bir;
}
void in() {
cout <<"num:";
cin >> num;
cout << "name:";
cin >> name;
cout << "gender:";
cin >> gender;
cout << "birthday:";
cin >> birthday;
}
void out() {
cout << "num: " << num << " name: " << name << " gender: " << gender << " birthday: " << birthday << endl;
}
int sex() {
if (gender == "男")
return 1;
else
return 2;
}
string getbirthday(){return birthday;}
};
int main() {
int n, boy = 0, girl = 0;
cin >> n;
Employee *p = new Employee[n];
for (int i = 0; i < n; i++) {
printf("请输入第%d位员工的信息:\n",i);
p[i].in();
if(p[i].sex() == 1) boy++;
else girl++;
}
int x, y;
Employee temp;
for(y = 0;y < n;y++)
for(x = y + 1;x < n;x++)
if((p[y].getbirthday()) > (p[x].getbirthday())){
temp = p[y];
p[y] = p[x];
p[x] = temp;
}
for (int i = 0; i < n; i++)
p[i].out();
cout << "boy: " << boy <<endl;
cout << "girl: " << girl << endl;
delete[] p;
return 0;
}