2 Star 2 Fork 0

Alkaid#3529/Staff management system

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
staff.cpp 2.81 KB
一键复制 编辑 原始数据 按行查看 历史
#include"head.h"
//对外提供获取与更改私有属性的接口
void worker::set_name(string name)
{
this->m_name = name;
}
string worker::get_name()
{
return this->m_name;
}
void worker::set_id(string id)
{
this->m_id = id;
}
string worker::get_id()
{
return this->m_id;
}
void worker::set_post(string post)
{
this->m_post = post;
}
string worker::get_post()
{
return this->m_post;
}
worker::~worker() {}
//有参构造,添加默认参数以兼容无参构造
staff::staff(string name, string id, string post, int salary)
{
this->m_name = name;
this->m_id = id;
this->m_post = post;
//在堆区开辟空间存放薪水值,带来了深浅拷贝与析构函数的相关问题
this->m_salary = new int(salary);
}
//拷贝构造,即便不使用也必须重写,解决潜在问题
staff::staff(staff& s)
{
this->m_name = s.m_name;
this->m_id = s.m_id;
this->m_post = s.m_post;
//深拷贝,否则只会将指针地址进行复制
*this->m_salary = *s.m_salary;
}
//重写析构函数,正确释放开辟在堆区的数据
staff::~staff()
{
//确保指针不为空后,再释放
if (m_salary != NULL)
{
delete m_salary;
m_salary = NULL;
}
}
//对外提供接口获取和修改薪水值
void staff::set_salary(int salary)
{
*this->m_salary = salary;
}
int staff::get_salary()
{
return *m_salary;
}
//赋值运算符重载,方便类之间相互赋值
staff& staff::operator=(staff& s)
{
this->set_name(s.m_name);
this->set_id(s.m_id);
this->set_post(s.m_post);
this->set_salary(*s.m_salary);
return *this;
}
//关系运算符重载,用于后序排序
bool staff::operator<(staff& s)
{
if (this->m_id < s.m_id)
{
return 1;
}
else
{
return 0;
}
}
ostream& operator<<(ostream& cout, staff& s)
{
cout << "Name : " << s.m_name;
for (unsigned int i = 0; i < 13 - s.m_name.length(); i++)
{
cout << " ";
}
cout << "ID : " << s.m_id << " Post : " << s.m_post;
return cout;
}
istream& operator>>(istream& cin, staff& s)
{
cout << "请输入职工姓名:";
while (1)
{
cin >> s.m_name;
if (s.m_name.length() > 13)
{
cout << "名称超出指定长度,请重新输入:";
}
else
{
break;
}
}
cout << "请输入职工岗位(Boss, Manager, Employee):";
while (1)
{
cin >> s.m_post;
string str_Boss = "Boss";
string str_Manager = "Manager";
string str_Employee = "Employee";
if (s.m_post == str_Boss || s.m_post == str_Manager || s.m_post == str_Employee)
{
break;
}
else
{
cout << "无该职工岗位,请重新输入:";
}
}
cout << "请输入职工编号(六位数字,如:000001):";
while (1)
{
cin >> s.m_id;
if (s.m_id.length() < 6)
{
cout << "编号长度不足,请重新输入:";
continue;
}
else if (s.m_id.length() > 6)
{
cout << "编号长度过长,请重新输入:";
continue;
}
int i = 0;
while (i < s.m_id.length())
{
if (!(s.m_id[i] >= '0' && s.m_id[i] <= '9'))
{
break;
}
i++;
}
if (i != 6)
{
cout << "编号必须由纯数字组成,不包含特殊字符,请重新输入:";
}
else
{
break;
}
}
return cin;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/alkaid3529/staff-management-system.git
git@gitee.com:alkaid3529/staff-management-system.git
alkaid3529
staff-management-system
Staff management system
master

搜索帮助