c++类的应用

设计一个职员类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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值