【C++】班级人事管理

来源:互联网 发布:图书收银软件 编辑:程序博客网 时间:2024/04/30 13:13


1) 设计一个人事管理的People(人员)类。考虑到通用性,这里只抽象出所有类型人员都具有的属性:name(姓名),number(编号),birthday(出生日期),id(身份证号)等等。其中“出生日期”定义为一个“日期”类(具有属性):year(年),month(月),date(日)内嵌子对象。用成员函数实现对人员信息的录入和显示等必要的功能操作(自己独立思考设计)。要求包括:构造函数和析构函数,拷贝构造函数,内联成员函数。

2) 从people(人员)类派生出student(学生)类,添加属性:班号 char chassNO;从people(人员)类派生出teacher(教师)类,添加属性:职务 char principalship,部门 char department. student类中派生出graduate(研究生)类,添加属性:专业 char subject,导师 teacher adviser(为(教师类内嵌子对象));graduate类和 teacher类派生出TA(助教生)类,注意虚基类的使用,重载相应的成员函数。

3) 对people类重载“= =”运算符和“=”运算符,“= =”运算符判断两个people类对象的id属性是否相等;“=”运算符实现people类对象的赋值操作。

4)定义一个对people类对象数组按编号排序的函数,一个按编号查找people对象的函数。



#include<iostream>#include<fstream>#include<iomanip>#include<cstring>using namespace std;static int number=0;     //编号class date      //时间类{public:    int year;    int month;    int day;};class people     //定义people类{ protected:    char name[20];    char sex[10];    char id[18];    date birthday;  public:      void setname(char *n)      { strcpy(name,n);}      char *getname()      { return name;}      int getnumber()      {return number;}      void setsex(char *s)      {             strcpy(sex,s);      }      char *getsex()      { return sex;}      void setid(char *i)      { strcpy(id,i);}      char* getid()      {return id;}       void setyear(int i)  //设置       { birthday.year=i;}       int getyear()   //获得       {return birthday.year;}         void setmonth(int i)  //设置       { birthday.month=i;}       int getmonth()   //获得      {return birthday.month;}         void setday(int i)  //设置      { birthday.day=i;}      int getday()   //获得      {return birthday.day;}  };class student:public people   //学生类{private:     char classno[7];    //班号public:     void setclassno(char *c)     { strcpy(classno,c);}     char *getclassno()     { return classno;}};class teacher:virtual public people  //老师类{protected:     char principaiship[11];     char department[21];public:     void setprin(char *p)     { strcpy(principaiship,p);}     char *getprin()     { return principaiship;}     void setdepart(char *d)     { strcpy(department,d);}     char *getdepart()     { return department;}};class graduate:virtual public student   //研究生类{protected:     char subject[21];public:     void setsubject(char *s)     { strcpy(subject,s);}     char *getsubject()     { return subject;}};int main(){     student s;     teacher t;     graduate g;     char namestr[21];    //字符的变量     char b[18];     int a=0;     int i=0; char y; int c; //定义传值过程中的变量     char flag;   //判断是否继续输入的变量  //ofstream ofile("people.bin",ios::out |ios::binary);  //定义输出的一个对象ofile//建立结构体,用于write和read的传值struct data {     int year,month,day,nn;     char name[20],sex[10],classno[7],principaiship[11],department[21],subject[21],id[18];}; //number=ds.nn;    data ds={0,0,0,0,0};//初始化结构体    people *p[3]={&s,&t,&g};   //分配地址do{   cout<<"**********************************************************"<<endl;   cout<<"*                添加学生信息     按  1                  *"<<   "\n"<<"*                添加老师信息     按  2                  *"<<   "\n"<<"*               添加研究生信息    按  3                  *"<<   "\n"<<"*                 按编号查询      按  4                  *"<<   "\n"<<"*                 按编号排序      按  5                  *"<<endl;   cout<<"**********************************************************"<<endl;    cin>>c;   if(c==1)   {    number++;    //调用一次标号加一      cout<<"输入学生姓名:"<<endl;      cin>>namestr;      p[i]->setname(namestr);//设置学生姓名      strcpy(ds.name,namestr);   //姓名的传值              //判断性别      do         {          cout<<"输入性别(m/f)"<<endl;          char y;          cin>>y;          if (y=='m'||y=='f')           {              if(y=='m')              {                   p[i]->setsex(namestr);                  strcpy(ds.sex,"男");              }              else if(y=='f')              {                   p[i]->setsex(namestr);                  strcpy(ds.sex,"女");              }          }          else          {              cout << "error!\n";              cout << "again\n";              cin >> y;          }      }while( y=='m'||y=='f') ;       //获取id信息       cout<<"输入id:"<<endl;       cin>>b;          p[i]->setid(b);        strcpy(ds.id,b);       //id的传值       //获取编号       cout<<"输入编号:"<<endl;       cin>>namestr;       s.setclassno(namestr);           strcpy(ds.classno,namestr);      //班号的传值      //获取生日       cout<<"输入生日:"<<"\n"<<"year:"<<endl;       cin>>a;       s.setyear(a);       ds.year=a;       cout<<"month:"<<endl;      //判断月份       do       {          cin>>a;          if(a<=12)          {              s.setmonth(a);              ds.month=a;          }          else          {                 cout << "输入错误!"<<endl;              cout << "请您重新输入:"<<endl;              cout<<"month:"<<endl;              cin>>a;          }       }while(a>12);   //判断日期   cout<<"day:"<<endl;   do   {   cin>>a;   if(a<=30)   {   s.setday(a);   ds.day=a;   }   else   {   cout<<"输入错误!"<<endl;   cout<<"请您重新输入:"<<endl;   cout<<"day:"<<endl;   cin>>a;   }   }while(a>30);   cout<<"录入成功"<<endl;   //将获得的信息写入文档中   ofstream ofile("people.txt",ios::out |ios::app);   if(!ofile)   {   cout<<"Cannot open file.\n";   return 1;   }   ofile.write((char*)&ds,sizeof ds);   ofile.close();   //是否继续   cout<<"是否继续?(y/n)"<<endl;   //flushall();   cin>>flag; }     //添加老师信息     else if(c==2)     {        number++; i = 1 ;           cout<<"输入老师姓名?"<<endl; cin>>namestr; strcpy(ds.name,namestr);  p[i]->setname(namestr); do {      cout<<"输入性别(m/f)"<<endl;             char y; cin>>y; if(y=='m'||y=='f') {  if(y=='m') {   p[i]->setsex(namestr); strcpy(ds.sex,"男"); } else if( y=='f')    {  p[i]->setsex(namestr); strcpy(ds.sex,"女"); }  } else {   cout<<"输入错误!"<<endl; cout<<"请您重新输入?"<<endl; cin>>y; } } while(y=='m'||y=='f');     //id的获得    cout<<"输入id:"<<endl; cin>>a; p[i]->setid(b); strcpy(ds.id,b);     // 获得老师职务信息  cout<<"输入老师职务?"<<endl; cin>>namestr; t.setprin(namestr); strcpy(ds.principaiship,namestr);     // 获得部门信息 cout<<"输入部门:"<<endl;     cin>>namestr; t.setdepart(namestr);  strcpy(ds.department,namestr);   // 获得生日信息  cout<<"输入生日: "<<"\n"<<"year:"<<endl; cin>>a;  s.setyear(a); ds.year=a; do  { cout<<"month:"<<endl; cin>>a; if(a<=12) {  s.setmonth(a); ds.month=a;  } else  { cout <<"输入错误!"<<endl; cout<<"请您重新输入:"<<endl; cin>>a; } } while(a>12); do  {  cout<<"day:"<<endl; cin>>a; if(a<=30) { s.setday(a); ds.day=a; }  else  {   cout<<"error!"<<endl;  cout<<"again:"<<endl;         cin>>a; }  } while(a>30);      //将得到的信息写入文挡保存  ofstream ofile("people.bin",ios::out |ios::binary|ios_base::app);         if(!ofile)         {          cout<<"Cannot open file.\n";          return 1;         }   ofile.write((char * )&ds,sizeof ds);   ofile.close();   cout<<"录入成功!"<<endl;    //是否继续   cout<<"是否继续?(y/n)"<<endl;//   flushall();   cin>>flag; }  //添加研究生信息     else if(c==3)     {        number++; i=2;           cout<<"输入研究生姓名:"<<endl; cin>>namestr; strcpy(ds.name,namestr);  p[i]->setname(namestr); //判断性别 do {      cout<<"输入性别(m/f)"<<endl;             char y; cin>>y; if(y=='m'||y=='f') {  if(y=='m') {   p[i]->setsex(namestr); strcpy(ds.sex,"男"); } else if( y=='f')    {  p[i]->setsex(namestr); strcpy(ds.sex,"女"); }  } else {   cout<<"输入错误!"<<endl; cout<<"请您重新输入?"<<endl; cin>>y; } } while(y=='m'||y=='f');     //获得id信息    cout<<"输入id:"<<endl; cin>>a; p[i]->setid(b); strcpy(ds.id,b);     // 获得专业信息  cout<<"输入专业:"<<endl; cin>>namestr; g.setsubject(namestr); strcpy(ds.subject,namestr);   // 获得生日信息  cout<<"输入生日: "<<"\n"<<"year:"<<endl; cin>>a;  s.setyear(a); ds.year=a; //判断月份 do  { cout<<"month:"<<endl; cin>>a; if(a<=12) {  s.setmonth(a); ds.month=a;  } else  { cout <<"输入错误!"<<endl; cout<<"请您重新输入:"<<endl; cin>>a; } } while(a>12); //判断日期 do  {  cout<<"day:"<<endl; cin>>a; if(a<=30) { s.setday(a); ds.day=a; }  else  {   cout<<"error!"<<endl;  cout<<"again:"<<endl;         cin>>a; }  } while(a>30);     //将得到的信息写入文挡保存  ofstream ofile("people.bin",ios::out |ios::binary|ios_base::app);         if(!ofile)         {          cout<<"Cannot open file.\n";          return 1;         }          ofile.write((char * )&ds,sizeof ds);          ofile.close();   cout<<"录入成功!"<<endl;    //是否继续   cout<<"是否继续?(y/n)"<<endl;   //flushall();   cin>>flag;}//输入编码查询信息else if(c==4){cout<<"请输入编码:"<<endl;int z;cin>>z;//读取文件通过指针定位来查找需要的信息fstream iofile;iofile.open("peoplo.bin",ios::in|ios::out);iofile.seekp(sizeof(ds),ios::beg);iofile.read((char*)&ds,sizeof ds);cout<<"*************************************"<<endl;cout<<number<<setw(10)<<ds.name<<setw(10)<<ds.id<<setw(10)<<ds.sex<<""<<ds.year<<"."<<ds.day<<setw(10)<<ds.classno<<setw(10)<<ds.department<<setw(10)<<ds.principaiship<<setw(10)<<ds.subject<<endl;cout<<"*************************************"<<endl;iofile.close();cout<<"\n"<<"是否继续?(y/n)"<<endl;//flushall();cin>>flag;}else if(c==5) //列出所有信息{cout<<"信息列表:"<<endl;cout<<"*************************************"<<endl;cout<<"*************************************"<<endl;fstream iofile;iofile.open("people.bin",ios::in|ios::out);for(int h=1;h<=number;h++){iofile.seekp((h-1)*sizeof(ds),ios::beg);iofile.read((char*)&ds,sizeof ds);        cout<<h<<number<<setw(10)<<ds.name<<setw(10)<<ds.id<<setw(10)<<ds.sex<<""<<ds.year<<"-"<<ds.month<<"-"<<ds.day<<setw(10)<<ds.classno<<setw(10)<<ds.department<<setw(10)<<ds.principaiship<<setw(10)<<ds.subject<<endl;}iofile.close();      cout<<"*************************************"<<endl;  cout<<"*************************************"<<endl;  cout<<"是否继续?(y/n)"<<endl;    //flushall();cin>>flag;}elsecout<<"输入错误!"<<endl; }while(flag=='y'||flag=='Y'); return 0; //ds.nn=number; }


0 0