首页 > 其他分享 >Employee Class

Employee Class

时间:2022-08-30 20:56:41浏览次数:43  
标签:last string int Date birth Employee Class

//Employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <iostream>
#include <iomanip>
#include <string>
#include "Date.h"
using namespace std;

/* Employee Class */
class Employee {
public:
	Employee() = default; // 默认构造函数,便于创建数组。
	Employee(string, string, Date&&, Date&&); // 带参构造函数,使用Date移动构造函数,移动内存,提高效率。
	void setEmployee(string first, string last, Date birth, Date hire);
	void print();                  //打印员工的信息。
	int getAge(Date& date);        //计算员工在参数指定的日期时,满多少岁。
	int getYearsWorked(Date& date);//计算该员工在参数指定的日期时,工作满了多少年。
	int getDaysWorked(Date& date); //计算该员工在参数指定的日期时,工作了多少天。

	/*添加一个静态成员函数,调用日期类中重载的“ > ”运算符,通过比较雇佣日期,在雇员对象数组中,
	找出工作年限最长的雇员。该函数的说明如下:参数employees[]是雇员对象的数组;n是雇员对象数组
	的元素个数。返回值:工作年限最长的雇员对象的引用。*/
	static const Employee& getMostFaith(const Employee employees[], int n);

	int getAgeDay();
	bool operator > (Employee& employee2);

	~Employee(); //析构函数
private:
	string firstname;
	string lastname;
	Date birthDate;
	Date hireDate;
};

Employee::Employee(string first, string last, Date&& birth, Date&& hire) // 带参构造函数,使用“成员初始化器”初始化数据成员。
	:firstname(first), lastname(last), birthDate(birth), hireDate(hire) {
}

void Employee::setEmployee(string first, string last, Date birth, Date hire) {
	firstname = first;
	lastname = last;
	birthDate = birth;
	hireDate = hire;
}

void Employee::print() {
	cout << firstname << ", " << lastname;
	cout << " Hired: ";
	hireDate.printFullYear();
	cout << " Birthday: ";
	birthDate.printFullYear();
	cout << endl;
}

int Employee::getAge(Date& date) {
	return birthDate.fullYearsTo(date);
}

int Employee::getYearsWorked(Date& date) {
	return hireDate.fullYearsTo(date);
}

//int Employee::getDaysWorked(Date& date) {
//	return hireDate.daysTo(date);
//}

int Employee::getDaysWorked(Date& date) { // 修改getDaysWorked,使其调用日期类中重载的减号运算符。
	return hireDate - date;
}

const Employee& Employee::getMostFaith(const Employee employees[], int n) {
	int flag = 0;
	for (int i = 1; i < n; i++) {
		if (employees[flag].hireDate > employees[i].hireDate) {
			flag = i;
		}
	}
	return employees[flag];
}

int Employee::getAgeDay() {
	Date today(2022, 5, 18);
	return today - this->birthDate;
}
bool Employee::operator > (Employee& employee2) {
	if (this->getAgeDay() > employee2.getAgeDay()) {
		return true;
	} else {
		return false;
	}
}

Employee::~Employee() {
}

#endif

标签:last,string,int,Date,birth,Employee,Class
From: https://www.cnblogs.com/catting123/p/16640768.html

相关文章