公司员工管理系统报告
题目:设计一个虚基类 Staff(员工),包含编号、姓名和年龄保护数据成员以及相关的成员函数;由 Staff 派生出工程师类 Engineer,包含专业和职称保护数据成员以及相关的成员函数;再由 Staff 派生出领导类 Leader,包含职务和部门保护数据成员以及相关的成员函数;然后由 Engineer 和 Leader 派生出主任工程师类 Chairman。
设计一个利用文件处理方式实现对公司人员(包括工程师、领导和主任工程师)进行管理,具有增加数据、更新数据、查询数据、删除数据以及重组文件的功能。编程思想:
- 首先设计虚基类 Staff,然后再设计派生类 Engineer 和 Leader,最后再设计 Chariman 类。将成员函数 Show()与 Set()设置成虚函数实现动态绑定,为了避免多重继承下的二义性问题,编写了 output()与 get()两个辅助函数,为了让 Chairman 只继承 Staff 类的一个对象,将 Staff 类设置成虚基类。
- 为了以文件处理的方式实现对公司人员的管理,编写了三个文件,分别存储工程师、领导和主人工程师的信息,为了实现目的,本题使用二进制文件的随机访问技术,首先在各个类中定义记录格式,然后以二进制格式把数据以二进制形式写入文件,因为类中有虚函数,所以不能用 read()和 write()一次将一个对象存储到文件中,但可以一次读写一个数据成员。在数据的存储和读取技术的基础上,设计了一个操作员工信息的 Oper 类,将所有针对数据记录的操作封装在一起,包括更新,添加,查询,删除等。为了能够访问类中的私有成员,将 Oper 设置成其它类的友元类。
源代码:
1. chairman.h
#ifndef CHAIRMAN_H_ #define CHAIRMAN_H_ #include<string> #include<cstring>
using namespace std;//Staff 类的声明
class Staff
{private:
long Id;//编号 char fullname[15];//姓名 int Age; //年龄
protected:
virtual void output()const;//辅助模块 virtual void get();//辅助模块
public:
void setName( char*name); //设置雇员的名字 void setId(long id);//设置雇员的编号
void setAge(int age);//设置雇员的年龄 char* getName();//返回雇员的名字
long getId();//返回编号 intgetAge();//返回年龄
Staff(long id=0,char*name="noname",int age=0);virtual ~Staff(){} virtual void show() const=0;//显示员工信息
virtual void set()=0;//设置员工信息 friendclass Oper;
};
//Engineer 类的声明
class Engineer:virtual public Staff
{private:
char specialty[20];//专业 char position[20];//职位 protected:
void output()const;//辅助模块 void get(); //辅助模块 public:
Engineer(longid=0,char*name="noname",intage=0,char*spe="null",char*pos="null"):Staff(i d,name,age)
{
strcpy(specialty,spe); strcpy(position,pos);
}
void show() const;void set();
void setSpecialty(char*spe);//设置专业 void setPosition(char*pos);//设置职位
char* getSpecialty(); //返回专业 char* getPosition();//返回职位 friendclass Oper;
} ;
//Leader 类的声明
class Leader: virtual public Staff
{
private:
char department[20];//部门 char title[20];//职务 protected:
void get();//辅助模块 void output() const;//辅助模块 public:
Leader(longid=0,char*name="noname",intage=0,char*dep="null",char*tit="null"):Staff(id,n ame,age)
{strcpy(department,dep); strcpy(title,tit);
}
void show()const;//显示 leader 的信息
void set() ;//设置 leader 的信息
void setDepartment(char *depar); //设置部门 void setTitle(char* tit);//设置职 char*getDepartment();//返回部门
char*getTitle();//返回职务 friendclass Oper;};
//Chairman 类的声明
class Chairman:public Engineer,public Leader
{public:
Chairman(longid=0,char*name="noname",intage=0,char*spe="null", char*pos="null",char*dep="null",char*tit="null"):Staff(id,name,age),Engineer(id,name,age,spe,p os),Leader(id,name,age,dep,tit){}
void show() const; //显示 Chairman 的信息
void set() ; //设置 Chariman 的信息
protected:
} ;
#endif
void get() ; //辅助模块
void output()const ; //辅助模块 friendclass Oper;
Chairman.cpp #include "chairman.h" #include <iostream>
//Staff methods
void Staff:: output()const//辅助模块
{
cout<<"Name:"<<fullname<<endl; cout<<"Age:"<<Age<<endl; cout<<"Id:"<<Id<<endl;
}
void Staff::get()//辅助模块
{
cin>>fullname;cout<<"Enter staff's age:";cin>>Age; cout<<"Enter staff's id:";cin>>Id;
}
Staff::Staff(long id, char*name,int age)//构造函数
{ Id=id; Age=age; strcpy(fullname,name);}
void Staff:: setName( char*name)//设置雇员的名字
{strcpy(fullname,name);
}
void Staff::setId(long id)//设置雇员的编号
{ Id=id;
}
void Staff::setAge(int age)//设置雇员的年龄
{Age=age}}
char* Staff::getName()//返回雇员的名字
{ return fullname;}
long Staff:: getId()//返回编号
{ return Id;}
intStaff:: getAge()//返回年龄
{return Age;}
//Engineer methods
void Engineer:: output()const//辅助模块
{cout<<"Specialty:"<<specialty<<endl; cout<<"Position:"<<position<<endl;}
void Engineer:: get()//辅助模块
{
cout<<"Enter Specialty:";cin>>specialty; cout<<"Enter Position:";cin>>position;
}
void Engineer:: show() const//显示 engineer 的信息
{
cout<<"###Engineer###"<<endl; Staff::output();output();
}
void Engineer:: set()//设置 engineer 的信息
{cout<<"Enter Engineer's name:"; Staff::get();get();}
void Engineer:: setSpecialty(char*spe)//设置专业
{strcpy(specialty,spe);}
void Engineer::setPosition(char*pos)//设置职位
{strcpy(position,pos);}
char* Engineer:: getSpecialty()//返回专业
{return specialty;}
char* Engineer::getPosition()//返回职位
{ return position;}
//Leader methods
void Leader::output() const
{ cout<<"Department:"<<department<<endl;cout<<"Title:"<<title<<endl;} void Leader::get()
{cout<<"Enter Department:";cin>>department; cout<<endl; cout<<"Enter Title:";cin>>title; cout<<endl;
}
void Leader:: show()const
{cout<<"###Leader###"<<endl; Staff::output();output();
}
void Leader::set()
{cout<<"Enter Leader's name:"; Staff::get(); get();
}
void Leader::setDepartment(char *depar) //设置部门
{ strcpy(department,depar);}
void Leader::setTitle(char* tit)//设置职务
{ strcpy(title,tit);}
char* Leader::getDepartment()//返回部门
{ return department;}
char*Leader::getTitle()//返回职务
{ return title;
}
//Chairman methods void Chairman::get()
{Engineer::get();Leader::get() ;} void Chairman::output() const
{Engineer::output();Leader::output();}
void Chairman:: show() const //显示 Chairman 的信息
{ cout<<"###Chairman###"<<endl; Staff::output();
output();
}
voidChairman::set()//设置Chariman 的信息
{
}
Oper.h
cout<<"Enter Chairman's name:"; Staff::get(); get();
#ifndef OPER_H #define OPER_H #include <iostream> #include <fstream> #include "chairman.h" #include<string> #include<string> using namespace std;
enum Choice{UPDATE,NEW,DELETE,QUERY,END};
class Oper//操作
{
public:
void CreateFile();//创建文件 int enterChoice();//输入选项
void updateRecord(fstream&); //更新记录
void newRecord(fstream&);//创建和插入记录 void queryRecord(fstream&);//查询信息
void deleteRecord(fstream&);//删除记录 long getId(const char* );//得到编号 void CreateEngineerFile();
void UpdateEngineer (fstream& updateFile) ; //更新 engineer 文件
void NewEngineerRecord(fstream& insertFile);//插入,创建 Engineer 文件
void DeleteEngineerRecord(fstream& deleteFile);//删除 engineer 记
void queryEngineerRecord(fstream& queryFile);//查询 engineer 信息
void CreateLeaderFile() ;//创建 Leader 文件
void UpdateLeaderFile(fstream& updateFile);//更新 Leader 文件
voidNewLeaderRecord(fstream& insertFile);//插入,创建 Leader 文件
void DeleteLeaderRecord(fstream& deleteFile);/