方便代码贴出来看看
#include <iostream>
#include<string>
#include<fstream>
using namespace std;
class title
{
protected:
enum { MANAGER = 0, HUMAN_SPECIALIST, FINANCIAL_SPECIALIST, SALES_SPECIALIST, ADMINISTRATIVE_SPECIALIST };
static double Salary[5];
std::string title_name;
size_t salary_index;
public:
title(std::string tn, size_t index) : title_name(tn), salary_index(index) {}
title(){}
~title() {}
title(const title& t) : title_name(t.title_name), salary_index(t.salary_index) {}
title& operator=(const title& t)
{
title_name = t.title_name;
salary_index = t.salary_index;
return *this;
}
static void change_Salary(double new_salary, int position);
friend istream& operator>>(istream& in, title& mytitle);
friend ostream& operator<<(ostream& out, title& mytitle);
friend ofstream& operator<<(ofstream& outfile, title& mytitle);
friend class Employee;
};
double title::Salary[5] = { 5000,3000,3000,3000,3000 };
istream& operator>>(istream& in, title& mytitle)
{
in >> mytitle.title_name >> title::Salary[mytitle.salary_index];
return in;
}
ostream& operator<<(ostream& out, title& mytitle)
{
out<< mytitle.title_name <<" "<< title::Salary[mytitle.salary_index];
return out;
}
ofstream& operator<<(ofstream& out, title& mytitle)
{
out << mytitle.title_name << " " << title::Salary[mytitle.salary_index];
return out;
}
void title::change_Salary(double new_salary, int position)
{
Salary[position] = new_salary;
std::cout << "changed Salary list" << std::endl;
std::cout << "manager: " <<Salary[0] << " ; ";
std::cout << "human_specialist: " << Salary[1] << " ; ";
std::cout << "financial_specialist: " << Salary[2] << " ; ";
std::cout << "sales_specialist: " << Salary[3] << " ; ";
std::cout << "administrative_specialist: " << Salary[4] << std::endl;
}
class manager : public title
{
public:
manager() : title("Manager", title::MANAGER) {}
~manager() {}
};
class human_specialist : public title
{
public:
human_specialist() : title("Human_specialist", title::HUMAN_SPECIALIST) {}
~human_specialist() {}
};
class financial_specialist : public title
{
public:
financial_specialist() : title("Financial_specialist", title::FINANCIAL_SPECIALIST) {}
~financial_specialist() {}
};
class sales_specialist : public title
{
public:
sales_specialist() : title("Sales_specialist", title::SALES_SPECIALIST) {}
~sales_specialist() {}
};
class administrative_specialist : public title
{
public:
administrative_specialist() : title("Administrative_specialist", title::ADMINISTRATIVE_SPECIALIST) {}
~administrative_specialist() {}
};
class Employee
{
friend ostream& operator<<(ostream&, const Employee*);
friend istream& operator>>(istream&, Employee*);
friend ofstream& operator<<(ofstream&, const Employee*);
friend ifstream& operator>>(ifstream&, Employee*);
protected:
std::string eid, name, sex, dob, doe, unit;
title mytitle;
public:
Employee(std::string e, std::string n, std::string s, std::string db, std::string de, std::string u, const title& mt) : eid(e), name(n), sex(s), dob(db), doe(de), unit(u), mytitle(mt) {}
~Employee() {}
Employee(const Employee& em) : eid(em.eid), name(em.name), sex(em.sex), dob(em.dob), doe(em.doe), unit(em.unit), mytitle(em.mytitle) {}
void information()
{
std::cout << "eid: " << eid << " ; " << "name: " << name << " ; ";
std::cout << "sex: " << sex << " ; " << "dob: " << dob << " ; ";
std::cout << "doe: " << doe << " ; " << "unit: " << unit << " ; ";
std::cout << "title: " << mytitle.title_name << " ; " << "salary: " << title::Salary[mytitle.salary_index] << std::endl;
}
};
istream& operator>>(istream& input, Employee* employee)//重载输入
{
cout << "请输入职工编号:\n" << endl;
input >> employee->eid;
cout << "请输入职工姓名:" << endl;
input >> employee->name;
cout << "请输入职工性别" << endl;
input >> employee->sex;
cout << "请输入出生年月日" << endl;
input >> employee->dob;
cout << "请输入入职日期" << endl;
input >> employee->doe;
cout <<"请输入职工部门:" <<endl;
input>>employee->unit;
cout << "请输入职工职称和薪资:" << endl;
input >> employee->mytitle;
return input;
}
ostream& operator<<(ostream& output, const Employee* employee)//重载输出
{
output << "职工编号:" << employee->eid << " 职工姓名:" << employee->name
<< " 职工性别:" << employee->sex << " 职工出生日期:" << employee->dob << " 职工入职日期: " << employee->doe << " 职工职称与薪资:";
output << employee->mytitle << endl;
return output;
}
ofstream& operator<<(ofstream& outEmployee, const Employee* employee)//向文件中写信息的重载
{
outEmployee << "职工编号:" << employee->eid << " 职工姓名:" << employee->name
<< " 职工性别:" << employee->sex << " 职工出生日期:" << employee->dob << " 职工入职日期: " << employee->doe << " 职工职称与薪资:";
outEmployee << employee->mytitle << endl;
return outEmployee;
}
ifstream& operator>>(ifstream& in, Employee* Employees)//从文件中读取信息的重载
{
int index;
std::string eid, name, sex, dob, doe, unit;
title mytitle;
in >> eid >> name >> sex >> dob >> doe >> unit >>mytitle;
if (in)
{
Employees->eid=eid;
Employees->name=name;
Employees->sex=sex;
Employees->dob=dob;
Employees->doe=doe;
Employees->unit=unit;
Employees->mytitle=mytitle;
}
return in;
}
const manager TITLE_MANAGER;
const human_specialist TITLE_HUMAN_SPECIALIST;
const financial_specialist TITLE_FINANCIAL_SPECIALIST;
const sales_specialist TITLE_SALES_SPECIALIST;
const administrative_specialist TITLE_ADMINISTRATIVE_SPECIALIST;
/******************************************************************************************************************************************/
//test//
int main()
{
double newsalary;
int choose;
cin >> newsalary>>choose;
Employee* p=new Employee("32", "23", "23", "32", "323", "1", TITLE_MANAGER);
p->information();
title::change_Salary(newsalary, choose);
p->information();
Employee r("44", "442", "23", "32", "323", "1", TITLE_HUMAN_SPECIALIST);
r = *p;
r.information();
delete p;
}