【问题描述】
大学里有各种类型的学生,校方需要对这些学生的信息进行计算机管理。所开发的软件应包括各类
学生的添加、修改、删除和查找等功能。考虑到软件的可重用性、可扩展性和可维护性,校方决定采用面向对象的程序设计方法来开发系统。学生信息需要以文件方式保存到计算机硬盘中。另外,系统的用户界面应该尽可能友好,方便用户使用。
【设计需求及分析】
(1) 使用 C++语言开发,充分利用面向对象程序设计的类、对象、继承、封装和多态性等概念来设计和
实现该管理系统。
(2) 设计一个 Person(人员)类,考虑到通用性,只抽象出所有类型人员都具有的属性:name(姓名),id(身份证号),gender(性别),birthday(出生日期)等等。其中“出生日期”为内嵌子对象,是一个 Date(日期)类型,Date 类具有属性: year(年),month(月),day(日)。用成员函数实现对人员信息的录入和显示等必要功能操作。
(3) 从 Person 类派生出 Student(学生)类,添加属性: studentNo(学号),schoolName(学校),classIn(班级)。从 Person 类派生出 Teacher(教师)类,添加属性:teacherNo(教师编号),schoolName(学校),department(部门)。
(4) 从 Student 类中派生出 UnderGraduate(本科生)类,添加属性:major(专业)。从 Student 类中派生出 Graduate(研究生)类,添加属性:direction(研究方向),adviserName(导师姓名)。
(5) 从 Graduate 类和 Teacher 类派生出 TA(助教博士生)类。
(6) 写程序测试上述各类,看能否正常运行。
(7) 构建必要的辅助类,实现对本科生、研究生和助教博士生的添加、修改、删除、查询管理。
(8) 根据需要定义类的构造函数、析构函数、拷贝构造函数、成员函数。必要时重载函数。
(9) 要求将 Person 类设置为虚基类,以消除其派生类成员访问的二义性问题(注意在虚基类各级派生类的构造函数实现时调用虚基类的构造函数)。
(10)要求在 Person 类中定义虚函数 displayDetails(),用于显示当前对象的信息;同时定义虚函数
inputData( ),用于从键盘获取当前对象的信息。Person 类所有派生类也要定义同名虚函数,使程序
可以实现动态多态性。
(11)用菜单方式设计主控模块程序。
(12)对程序源代码要给出各部分的详细注释,这也是该题目的考核重点之一。
(13)用 UML 语言描述系统用到的类及其关系。
源码
1.main.cpp
/*************************************************
Copyright (C), 2020, Tyut
File name: main.cpp
Author:YQHB Version: 1.0 Date: 2020.5.28 Description:
应用程序主函数
*************************************************/
#include <cstdlib>
#include <iostream>
#include "date.h"
#include "person.h"
#include "student.h"
#include "teacher.h"
#include "undergraduate.h"
#include "graduate.h"
#include "graduateManager.h"
#include "ta.h"
#include "taManager.h"
#include "undergraduateManager.h"
using namespace std;
int main(int argc, char *argv[])
{
int choiceN;
UndergraduateManager unMan;
GraduateManager GMan;
TAManager TAMan;
cout<<"********************************************************"<<endl;
cout<<"*|*| |*|*"<<endl;
cout<<"*|*| 欢迎您使用学生管理系统 |*|*"<<endl;
cout<<"*|*| |*|*"<<endl;
cout<<"********************************************************"<<endl;
do
{
cout << "<---------------------------------------------------->" << endl;
cout << " \n \t\t 1:本科生管理 ";
cout << " \n \t\t 2:研究生管理 ";
cout << " \n \t\t 3.助教博士生管理 ";
cout << " \n \t\t 0:离开 ";
cout << endl;
cout << "<---------------------------------------------------->" << endl;
cout << "请选择:" << endl;
cin >> choiceN;
switch(choiceN)
{
case 1:
unMan.dataManage();
//break;
case 2:
GMan.dataManage();
//break;
case 3:
TAMan.dataManage();
//break;
default:
break;
}
}
while(choiceN!=0);
cout<<" **********************************************************"<<endl;
cout<<"*|*| 感谢使用学生管理系统 |*|*"<<endl;
cout<<" **********************************************************\a"<<endl;
}
2.date.h
/*************************************************
Copyright (C), 2020, Tyut
File name: date.h
Author:YQHB Version: 1.0 Date: 2020.5.29
Description: 日期类
*************************************************/
#ifndef DATE_H_INCLUDED
#define DATE_H_INCLUDED
#include <iostream>
#include "date.h"
using namespace std;
/* Define a Class : Date*/
/* with attributes: year, month, and day, and */
/* operations: getYear, getMonth,.... */
class Date
{
private:
int year;
int month;
int day;
public:
Date()
{
year = 0;
month = 0;
day = 0;
}
Date(int yy,int mm,int dd)
{
year = yy;
month = mm;
day = dd;
}
Date(Date& d)
{
year = d.year;
month = d.month;
day = d.day;
}
~Date() {}
void setYear(int yy)
{
year = yy;
}
void setMonth(int mm)
{
month = mm;
}
void setDay(int dd)
{
day = dd;
}
int getYear()
{
return year;
}
int getMonth()
{
return month;
}
int getDay()
{
return day;
}
void inputDate()
{
cout << "年:";
cin >> year;
cout << "月: ";
cin >> month;
cout << "日: ";
cin >> day;
}
void displayDate()
{
cout << year << "/" << month << "/" << day << endl;
}
};
#endif // DATE_H_INCLUDED
3.person.h
/*************************************************
Copyright (C), 2020, Tyut
File name: person.h
Author:YQHB Version: 1.0 Date: 2020.5.289
Description: 人员类
*************************************************/
#ifndef PERSON_H_INCLUDED
#define PERSON_H_INCLUDED
#include <iostream>
#include <cstring>
#include <string>
#include "date.h"
using namespace std;
/* Define a Class : Person */
/* with attributes: name, id,gender, birthday */
/* operations: getName, getId, and setId. */
/* person is the base class */
//该类为虚基类
class Person
{
protected:
char name[20];//姓名
char id[18]; //身份证号
char gender[2];//性别
Date birthday;//出生日期
public:
Person();
Person(char* sname, char* sid, char* sgender,int yy,int mm,int dd);
~Person() {}
void setName(char* sname)
{
strcpy(name,sname);
}
char* getName()
{
return name;
}
void setId(char* sid)
{
strcpy(id,sid);
}
char* getId()
{
return id;
}
void setGender(char* sgender)
{
strcpy(gender,sgender);
}
char* getGender()
{
return gender;
}
void setBirthday(Date d)
{
birthday = d;
}
Date getBirthday()
{
return birthday;
}
virtual void inputData();//输入数据
virtual void displayDetails(); //显示数据
};
Person::Person()
{
strcpy(name,"NA");
strcpy(gender,"男");
strcpy(id,"000");
}
Person::Person(char* sname, char* sid, char* sgender,int yy,int mm,int dd):birthday(yy,mm,dd)
{
strcpy(name,sname);
strcpy(id,sid);
strcpy(gender,sgender);
}
void Person::inputData()
{
cout << "姓名:" ;
cin >> name ;
cout << "身份证号:" ;
cin >> id ;
cout << "性别:";
cin >> gender ;
cout << "出生日期:" << endl;
birthday.inputDate();
}
void Person::displayDetails()
{
cout << "姓名:" <<name <<endl;
cout << "身份证号:" <<id <<endl;
cout << "性别:" <<gender<<endl;
cout << "出生日期:";
birthday.displayDate();
}
#endif // PERSON_H_INCLUDED
4.student.h
/*************************************************
Copyright (C), 2020, Tyut
File name: student.h
Author:YQHB Version: 1.0 Date: 2020.5.29
Description: 学生类
*************************************************/
#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED
#include <iostream>
#include <string>
#include <cstring>
#include "person.h"
using namespace std;
class Student: virtual public Person
{
protected:
char studentNo[5];//学生学号
char schoolName[20]; //学校名称
char classIn[20]; //班级号
public:
Student();
Student(char* sname, char* sid, char* sgender,int yy,int mm,int dd
,char* sno,char* sschool,char* sclass);
~Student() {}
void setstudentNo(char* sno)
{
strcpy(studentNo,sno);
}
char* getstudentNo()
{
return studentNo;
}
void setschoolName(char* sschool)
{
strcpy(schoolName,sschool);
}
char* getschoolName()
{
return schoolName;
}
void setclassIn(char* sclass)
{
strcpy(classIn,sclass);
}
char* getclassIn()
{
return classIn;
}
virtual void inputData();//输入数据
virtual void displayDetails(); //显示数据
};
Student::Student():Person()
{
strcpy(studentNo,"001");
strcpy(schoolName,"defaultSchoolName");
strcpy(classIn,"defaultclassIn");
}
Student::Student(char* sname, char* sid, char* sgender,int yy,int mm,int dd
,char* sno,char* sschool,char* sclass)
:Person(sname,sid,sgender,yy,mm,dd)
{
strcpy(studentNo,sno);
strcpy(schoolName,sschool);
strcpy(classIn,sclass);
}
void Student::inputData()
{
Person::inputData();
cout << "学生编号:" ;
cin >>studentNo ;
cout << "学校:" ;
cin >> schoolName ;
cout << "班级:" ;
cin >> classIn ;
}
void Student::displayDetails()
{
Person::displayDetails();
cout << "学生编号:" << studentNo <<endl;
cout << "学校:" << schoolName <<endl;
cout << "班级:" <<classIn << endl;
}
#endif // STUDENT_H_INCLUDED
5.teacher.h
/*************************************************
Copyright (C), 2020, Tyut
File name: teacher.h
Author:YQHB Version: 1.0 Date: 2020.5.29
Description: 教师类
*************************************************/
#ifndef TEACHER_H_INCLUDED
#define TEACHER_H_INCLUDED
#include <iostream>
#include <string>
#include <cstring>
#include "person.h"
using namespace std;
/* Define a Class : Teacher */
/* with attributes: */
/* operations: */
class Teacher: virtual public Person
{
protected:
char TeacherNo[5]; //教师编号
char SchoolName[20]; //学校名称
char Department[20]; //部门
public:
Teacher();
Teacher(char* sname, char* sid, char* sgender,int yy,int mm,int dd,char* steacherNo,char* sschool,char* sdepartment);
~Teacher() {}
void setTeacherNo(char* sno)
{
strcpy(TeacherNo,sno);
}
char* getTeacherNo()
{
return TeacherNo;
}
void setSchoolName(char* sschool)
{
strcpy(SchoolName,sschool);
}
char* getSchoolName()
{
return SchoolName;
}
void setDepartment(char* sdepartment)
{
strcpy(Department,sdepartment);
}
char* getDepartment()
{
return Department;
}
virtual void inputData();//输入数据
virtual void displayDetails(); //显示数据
};
Teacher::Teacher():Person()
{
strcpy(TeacherNo,"001");
strcpy(SchoolName,"defaultSchoolName");
strcpy(Department,"defaultDepartment");
}
Teacher::Teacher(char* sname, char* sid, char* sgender,int yy,int mm,int dd
,char* steacherNo,char* sschool,char* sdepartment)
:Person(sname,sid,sgender,yy,mm,dd)
{
strcpy(TeacherNo,steacherNo);
strcpy(SchoolName,sschool);
strcpy(Department,sdepartment);
}
void Teacher::inputData()
{
Person::inputData();
cout << "教师编号:" ;
cin >> TeacherNo ;
cout << "学校:" ;
cin >> SchoolName ;
cout << "部门:" ;
cin >> Department ;
}
void Teacher::displayDetails()
{
Person::displayDetails();
cout << "教师编号:" << TeacherNo <<endl;
cout << "学校:" << SchoolName <<endl;
cout << "部门:" << Department << endl;
}
#endif // TEACHER_H_INCLUDED
本科生类及本科生管理类
6.undergraduate.h
/*************************************************
Copyright (C), 2020, Tyut
File name: undergraduate.h
Author:YQHB Version: 1.0 Date: 2020.5.29
Description: 本科生类
*************************************************/
#ifndef UNDERGRADUATE_H_INCLUDED
#define UNDERGRADUATE_H_INCLUDED
#include <iostream>
#include <string>
#include <cstring>
#include "student.h"
using namespace std;
class Undergraduate :virtual public Student
{
protected:
char major[20];//专业
public:
Undergraduate();
Undergraduate(char* sname, char* sid, char* sgender,int yy,int mm,int dd, char*sno,char*sschool,char*sclass,
char* smajor);
~Undergraduate() {}
void setmajor(char* smajor)
{
strcpy(major,smajor);
}
char* getmajor()
{
return major;
}
virtual void inputData();//输入数据
virtual void displayDetails(); //显示数据
};
Undergraduate::Undergraduate():Student()
{
strcpy(major,"defaultmajor");
}
Undergraduate::Undergraduate(char* sname, char* sid, char* sgender, int yy,int mm,int dd,
char*sno,char*sschool,char*sclass, char* smajor)
:Student(sname,sid,sgender,yy,mm,dd,sno,sschool,sclass)
{
strcpy(major,smajor);
}
void Undergraduate::inputData()
{
Student::inputData();
cout << "本科生专业:" ;
cin >>major;
}
void Undergraduate::displayDetails()
{
Student::displayDetails();
cout << "本科生专业:" <<major <<endl;
}
#endif // UNDERGRADUATE_H_INCLUDED
7.undergraduateManager.h
#ifndef UNDERGRADUATEMANAGER_H_INCLUDED
#define UNDERGRADUATEMANAGER_H_INCLUDED
#include <iostream>
#include <string>
#include <fstream>
#include "undergraduate.h"
#include "student.h"
using namespace std;
/* Define a Class : UndergraduateManager 本科生管理类*/
class UndergraduateManager
{
private:
int top; //记录指针
Undergraduate undergraduates[100]; //本科生记录
public:
UndergraduateManager();//构造函数,将 Undergraduate.txt 读到 undergraduates[]中
int queryByNo(string sno);//按本科生号查找 //找到:返回数组下标//没找到:返回-1
void clearStudent(); //删除所有本科生信息
int addStudent(Undergraduate s); //添加本科生,需要先查找是否存在
int modifyStudent(string sno); //修改学生信息 ,需要先查找是否存在
int deleteStudent(string sno);//删除本科生,删除前先查找其是否存在
int queryStudent(string sno);//查找本科生,查到则显示,否则提示未查到
void displayAll();//输出所有本科生信息
void dataManage(); //本科生库维护
void dataSave();
void dataRead();
~UndergraduateManager();//析构函数,将 undergraduates[]写入 Undergraduate.txt 文件中
};
//构造函数,将 Undergraduate.txt 读到 undergraduates[]中
UndergraduateManager::UndergraduateManager()
{
dataRead();
}
//按本科生号查找
//找到:返回数组下标
//没找到:返回-1
int UndergraduateManager::queryByNo(string sno)
{
for(int i=0; i<=top; i++)
if (undergraduates[i].getstudentNo()==sno)
return i;
return -1;
}
//删除所有本科生信息
void UndergraduateManager::clearStudent()
{
top=-1;
}
//添加本科生,需要先查找是否存在
int UndergraduateManager::addStudent(Undergraduate s)
{
int p=queryByNo(s.getstudentNo());
if (p==-1)
{
top++;
undergraduates[top]= s;
dataSave();//保存
return 1;
}
else
{
cout<<"--------->此学生已经存在 !<----------"<<endl<<endl;
return 0;
}
}
//修改科生,删除前先查找其是否存在
int UndergraduateManager::modifyStudent(string sno)
{
int p=queryByNo(sno);
if (p==-1)
{
cout<<"--------->此学生不存在 !<----------"<<endl<<endl;
return 0;
}
else
{
cout << "请输入该生的新信息: " << endl<<endl;
undergraduates[p].inputData();
dataSave();//保存
return 1;
}
}
//删除本科生,删除前先查找其是否存在
int UndergraduateManager::deleteStudent(string sno)
{
int p=queryByNo(sno);
if (p==-1)
{
cout<<"--------->此学生不存在 !<----------"<<endl<<endl;
return 0;
}
else
{
for(int i = p; i < top ; i++)
undergraduates[i]=undergraduates[i+1];
top--;
cout << "--------->删除完成!<----------" << endl<<endl;
dataSave();//保存
return 1;
}
}
//查找科生
int UndergraduateManager::queryStudent(string sno)
{
int p=queryByNo(sno);
if (p==-1)
{
cout<<"--------->此学生不存在 !<----------"<<endl<<endl;
return 0;
}
else
{
cout<<"--------->此学生存在:<----------"<<endl<<endl;
undergraduates[p].displayDetails();
return 1;
}
}
//输出所有本科生信息
void UndergraduateManager::displayAll()
{
for (int i=0; i<=top; i++)
{
cout << "--------第"<< i <<"个学生情况----------" << endl<<endl;
undergraduates[i].displayDetails();
}
}
//析构函数,将 undergraduates[]写入 Undergraduate.txt 文件中
UndergraduateManager::~UndergraduateManager()
{
dataSave();
}
void UndergraduateManager::dataManage()
{
int choice=1;
string sstudentNo;
Undergraduate s;
while (choice!=0)
{
cout << "********************************************" << endl;
cout << "\t\t 本科生维护\n";
cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
cout << "\n \t\t 1:新增 ";
cout << "\n \t\t 2:修改 ";
cout << "\n \t\t 3:删除 ";
cout << "\n \t\t 4:查找 ";
cout << "\n \t\t 5:显示 ";
cout << "\n \t\t 6:全部删除 ";
cout << "\n \t\t 0:退出 " ;
cout << endl;
cout << "*********************************************" << endl;
cout << "请选择:" << endl;
cin >> choice;
switch (choice)
{
case 1:
s.inputData();
addStudent(s);
break;
case 2:
cout << " 请输入学号:";
cin >> sstudentNo;
modifyStudent(sstudentNo);
break;
case 3:
cout << " 请输入学号:";
cin >> sstudentNo;
deleteStudent(sstudentNo);
break;
case 4:
cout << " 请输入学号:";
cin >> sstudentNo;
queryStudent(sstudentNo);
break;
case 5:
displayAll();
break;
case 6:
clearStudent();
break;
default:
break;
}
}
}
void UndergraduateManager::dataSave()//存储资料函数,将 read[]写入 Undergraduate.txt 文件中
{
fstream file("Undergraduate.dat",ios::out);
for (int i=0; i<=top; i++)
file.write((char *)&undergraduates[i],sizeof(undergraduates[i]));
file.close();
}
void UndergraduateManager::dataRead() //构造函数,将 Undergraduate.txt 读到 read[]中
{
Undergraduate s;
top=-1;
fstream file("Undergraduate.dat",ios::in);
while (1)
{
file.read((char *)&s,sizeof(s));
if (!file)
break;
top++;
undergraduates[top]=s;
}
file.close();
}
#endif // UNDERGRADUATEMANAGER_H_INCLUDED
研究生类及研究生管理类
8.graduate.h
/*************************************************
Copyright (C), 2020, Tyut
File name: graduate.h
Author:YQHB Version: 1.0 Date: 2020.5.29
Description: 研究生类
*************************************************/
#ifndef GRADUATE_H_INCLUDED
#define GRADUATE_H_INCLUDED
#include <iostream>
#include <string>
#include <cstring>
#include "student.h"
#include "undergraduate.h"
#include "teacher.h"
using namespace std;
class Graduate : virtual public Student
{
protected:
char direction[20];//研究方向
char adviserName[10];//导师姓名
public:
Graduate();
Graduate(char* sname, char* sid, char* sgender,int xx,int nn,int dd, char*sno,char*sschool,char*sclass, char*sdirection,char*sadviser);
~Graduate() {}
void setdirection(char* sdirection)
{
strcpy(direction,sdirection);
}
char* getdirection()
{
return direction;
}
void setadviserName(char* sadviser)
{
strcpy(adviserName,sadviser);
}
char* getadviserName()
{
return adviserName;
}
virtual void inputData();//输入数据
virtual void displayDetails(); //显示数据
};
Graduate::Graduate():Student()
{
strcpy(adviserName,"defaultadviserName");
strcpy(direction,"defaultdirection");
}
Graduate::Graduate(char* sname, char* sid, char* sgender,int xx,int nn,int dd,
char*sno,char*sschool,char*sclass, char*sdirection,char*sadviser)
:Student(sname,sid,sgender,xx,nn,dd,sno,sschool,sclass)
{
strcpy(direction,sdirection);
strcpy(adviserName,sadviser);
}
void Graduate::inputData()
{
Student::inputData();
cout << "研究方向:" ;
cin >>direction ;
cout << "导师姓名:" ;
cin >>adviserName ;
}
void Graduate::displayDetails()
{
Student::displayDetails();
cout << "研究方向:" << direction <<endl;
cout << "导师姓名:" << adviserName <<endl;
}
#endif // GRADUATE_H_INCLUDED
9.graduateManager.h
/*************************************************
Copyright (C), 2020, Tyut
File name: graduateManager.h
Author:YQHB Version: 1.0 Date: 2020.5.29
Description: 研究生管理类
*************************************************/
#ifndef GRADUATEMANAGER_H_INCLUDED
#define GRADUATEMANAGER_H_INCLUDED
#include <iostream>
#include <string>
#include <fstream>
//#include "student.h"
#include "graduate.h"
using namespace std;
/* Define a Class : GraduateManager 研究生管理类*/
class GraduateManager
{
private:
int top; //记录指针
Graduate graduates[500]; //研究生记录
public:
GraduateManager();//构造函数,将 Graduate.txt 读到 graduates[]中
int queryByNo(string sno);//按研究生号查找 //找到:返回数组下标//没找到:返回-1
void clearStudent(); //删除所有研究生信息
int addStudent(Graduate s); //添加研究生,需要先查找是否存在
int modifyStudent(string sno); //修改学生信息 ,需要先查找是否存在
int deleteStudent(string sno);//删除研究生,删除前先查找其是否存在
int queryStudent(string sno);//查找研究生,查到则显示,否则提示未查到
void displayAll();//输出所有研究生信息
void dataManage(); //研究生库维护
void dataSave();
void dataRead();
~GraduateManager();//析构函数,将 graduates[]写入 Graduate.txt 文件中
};
//构造函数,将 Graduate.txt 读到 Graduates[]中
GraduateManager::GraduateManager()
{
dataRead();
}
//按研究生号查找
//找到:返回数组下标
//没找到:返回-1
int GraduateManager::queryByNo(string sno)
{
for(int i=0; i<=top; i++)
if (graduates[i].getstudentNo()==sno)
return i;
return -1;
}
//删除所有研究生信息
void GraduateManager::clearStudent()
{
top=-1;
}
//添加研究生,需要先查找是否存在
int GraduateManager::addStudent(Graduate s)
{
int p=queryByNo(s.getstudentNo());
if (p==-1)
{
top++;
graduates[top]= s;
dataSave();//保存
return 1;
}
else
{
cout<<"--------->此学生已经存在 !<----------"<<endl<<endl;
return 0;
}
}
//修改研究生,删除前先查找其是否存在
int GraduateManager::modifyStudent(string sno)
{
int p=queryByNo(sno);
if (p==-1)
{
cout<<"--------->此学生不存在 !<----------"<<endl<<endl;
return 0;
}
else
{
cout << "请输入该生的新信息: " << endl<<endl;
graduates[p].inputData();
dataSave();//保存
return 1;
}
}
//删除研究生,删除前先查找其是否存在
int GraduateManager::deleteStudent(string sno)
{
int p=queryByNo(sno);
if (p==-1)
{
cout<<"--------->此学生不存在 !<----------"<<endl<<endl;
return 0;
}
else
{
for(int i = p; i < top ; i++)
graduates[i]=graduates[i+1];
top--;
cout << "--------->删除完成!<----------" << endl<<endl;
dataSave();//保存
return 1;
}
}
//查找科生
int GraduateManager::queryStudent(string sno)
{
int p=queryByNo(sno);
if (p==-1)
{
cout<<"--------->此学生不存在 !<----------"<<endl<<endl;
return 0;
}
else
{
cout<<"--------->此学生存在:<----------"<<endl<<endl;
graduates[p].displayDetails();
return 1;
}
}
//输出所有研究生信息
void GraduateManager::displayAll()
{
for (int i=0; i<=top; i++)
{
cout << "--------第"<< i <<"个学生情况----------" << endl<<endl;
graduates[i].displayDetails();
}
}
//析构函数,将 graduates[]写入 Graduate.txt 文件中
GraduateManager::~GraduateManager()
{
dataSave();
}
void GraduateManager::dataManage()
{
int choice=1;
string sstudentNo;
Graduate s;
while (choice!=0)
{
cout << "********************************************" << endl;
cout << "\t\t 研究生维护\n";
cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
cout << "\n \t\t 1:新增 ";
cout << "\n \t\t 2:修改 ";
cout << "\n \t\t 3:删除 ";
cout << "\n \t\t 4:查找 ";
cout << "\n \t\t 5:显示 ";
cout << "\n \t\t 6:全部删除 ";
cout << "\n \t\t 0:退出 " ;
cout << endl;
cout << "*********************************************" << endl;
cout << "请选择:" << endl;
cin >> choice;
switch (choice)
{
case 1:
s.inputData();
addStudent(s);
break;
case 2:
cout << " 请输入学号:";
cin >> sstudentNo;
modifyStudent(sstudentNo);
break;
case 3:
cout << " 请输入学号:";
cin >> sstudentNo;
deleteStudent(sstudentNo);
break;
case 4:
cout << " 请输入学号:";
cin >> sstudentNo;
queryStudent(sstudentNo);
break;
case 5:
displayAll();
break;
case 6:
clearStudent();
break;
default:
break;
}
}
}
void GraduateManager::dataSave()//存储资料函数,将 read[]写入 Graduate.txt 文件中
{
fstream file("Graduate.dat",ios::out);
for (int i=0; i<=top; i++)
file.write((char *)&graduates[i],sizeof(graduates[i]));
file.close();
}
void GraduateManager::dataRead() //构造函数,将 Graduate.txt 读到 read[]中
{
Graduate s;
top=-1;
fstream file("Graduate.dat",ios::in);
while (1)
{
file.read((char *)&s,sizeof(s));
if (!file)
break;
top++;
graduates[top]=s;
}
file.close();
}
#endif // GRADUATEMANAGER_H_INCLUDED
助教博士生类以及助教博士生管理类
10. ta.h
/*************************************************
Copyright (C), 2020, Tyut
File name: TA.h
Author:YQHB Version: 1.0 Date: 2020.5.29
Description: 助教博士生类
*************************************************/
#ifndef TA_H_INCLUDED
#define TA_H_INCLUDED
#include <iostream>
#include "Graduate.h"
#include "Teacher.h"
using namespace std;
class TA: public Teacher, public Graduate
{
public:
TA();
TA ( char *sname, char *sid, char *sgender, int yy, int mm, int dd, char *steacherNo, char *sschool, char *sdepartment,
char *sno, char *sclass,
char *sdirection, char *sadvisorName );
~TA() {};
virtual void inputData();
virtual void displayDetails();
};
TA::TA() : Teacher(), Graduate()
{
}
TA::TA ( char *sname, char *sid, char *sgender, int yy, int mm, int dd, char *steacherNo, char *sschool, char *sdepartment,
char *sno, char *sclass,
char *sdirection, char *sadvisorName )
: Person ( sname, sid, sgender, yy, mm, dd )
{
}
void TA::inputData()
{
char teacherNo[5]; // 教师编号
char department[20]; // 部门
Graduate::inputData();
cout << "教师编号:";
cin >> teacherNo;
cout << "部门:";
cin >> department;
Teacher::setId ( teacherNo );
Teacher::setDepartment ( department );
}
void TA::displayDetails()
{
Graduate::displayDetails();
Teacher::displayDetails();
}
#endif // TA_H_INCLUDED
11.taManager.h
/*************************************************
Copyright (C), 2020, Tyut
File name: taManager.h
Author:YQHB Version: 1.0 Date: 2020.5.29
Description: 助教博士管理类
*************************************************/
#ifndef TAMANAGER_H_INCLUDED
#define TAMANAGER_H_INCLUDED
#include <iostream>
#include <string>
#include <fstream>
#include "ta.h"
using namespace std;
/* Define a Class : TAManager 助教博士管理类*/
class TAManager
{
private:
int top; //记录指针
TA tas[100]; //助教博士记录
public:
TAManager();//构造函数,将 TA.txt 读到 tas[]中
int queryByNo(string sno);//按助教博士号查找 //找到:返回数组下标//没找到:返回-1
void clearStudent(); //删除所有助教博士信息
int addStudent(TA s); //添加助教博士,需要先查找是否存在
int modifyStudent(string sno); //修改学生信息 ,需要先查找是否存在
int deleteStudent(string sno);//删除助教博士,删除前先查找其是否存在
int queryStudent(string sno);//查找助教博士,查到则显示,否则提示未查到
void displayAll();//输出所有助教博士信息
void dataManage(); //助教博士库维护
void dataSave();
void dataRead();
~TAManager();//析构函数,将 tas[]写入 TA.txt 文件中
};
//构造函数,将 TA.txt 读到 TAs[]中
TAManager::TAManager()
{
dataRead();
}
//按助教博士号查找
//找到:返回数组下标
//没找到:返回-1
int TAManager::queryByNo(string sno)
{
for(int i=0; i<=top; i++)
if (tas[i].getstudentNo()==sno)
return i;
return -1;
}
//删除所有助教博士信息
void TAManager::clearStudent()
{
top=-1;
}
//添加助教博士,需要先查找是否存在
int TAManager::addStudent(TA s)
{
int p=queryByNo(s.getstudentNo());
if (p==-1)
{
top++;
tas[top]= s;
dataSave();//保存
return 1;
}
else
{
cout<<"--------->此学生已经存在 !<----------"<<endl<<endl;
return 0;
}
}
//修改助教博士,删除前先查找其是否存在
int TAManager::modifyStudent(string sno)
{
int p=queryByNo(sno);
if (p==-1)
{
cout<<"--------->此学生不存在 !<----------"<<endl<<endl;
return 0;
}
else
{
cout << "请输入该生的新信息: " << endl<<endl;
tas[p].inputData();
dataSave();//保存
return 1;
}
}
//删除助教博士,删除前先查找其是否存在
int TAManager::deleteStudent(string sno)
{
int p=queryByNo(sno);
if (p==-1)
{
cout<<"--------->此学生不存在 !<----------"<<endl<<endl;
return 0;
}
else
{
for(int i = p; i < top ; i++)
tas[i]=tas[i+1];
top--;
cout << "--------->删除完成!<----------" << endl<<endl;
dataSave();//保存
return 1;
}
}
//查找助教博士
int TAManager::queryStudent(string sno)
{
int p=queryByNo(sno);
if (p==-1)
{
cout<<"--------->此学生不存在 !<----------"<<endl<<endl;
return 0;
}
else
{
cout<<"--------->此学生存在:<----------"<<endl<<endl;
tas[p].displayDetails();
return 1;
}
}
//输出所有助教博士信息
void TAManager::displayAll()
{
for (int i=1; i<=top; i++)
{
cout << "--------第"<< i <<"个学生情况----------" << endl<<endl;
tas[i].displayDetails();
}
}
//析构函数,将 tas[]写入 TA.txt 文件中
TAManager::~TAManager()
{
dataSave();
}
void TAManager::dataManage()
{
int choice=1;
string sstudentNo;
TA s;
while (choice!=0)
{
cout << "********************************************" << endl;
cout << "\t\t 助教博士生维护\n";
cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" << endl;
cout << "\n \t\t 1:新增 ";
cout << "\n \t\t 2:修改 ";
cout << "\n \t\t 3:删除 ";
cout << "\n \t\t 4:查找 ";
cout << "\n \t\t 5:显示 ";
cout << "\n \t\t 6:全部删除 ";
cout << "\n \t\t 0:退出 " ;
cout << endl;
cout << "*********************************************" << endl;
cout << "请选择:" << endl;
cin >> choice;
switch (choice)
{
case 1:
s.inputData();
addStudent(s);
break;
case 2:
cout << " 请输入学号:";
cin >> sstudentNo;
modifyStudent(sstudentNo);
break;
case 3:
cout << " 请输入学号:";
cin >> sstudentNo;
deleteStudent(sstudentNo);
break;
case 4:
cout << " 请输入学号:";
cin >> sstudentNo;
queryStudent(sstudentNo);
break;
case 5:
displayAll();
break;
case 6:
clearStudent();
break;
default:
break;
}
}
}
void TAManager::dataSave()//存储资料函数,将 read[]写入 TA.txt 文件中
{
fstream file("TA.dat",ios::out);
for (int i=0; i<=top; i++)
file.write((char *)&tas[i],sizeof(tas[i]));
file.close();
}
void TAManager::dataRead() //构造函数,将 TA.txt 读到 read[]中
{
TA s;
top=-1;
fstream file("TA.dat",ios::in);
while (1)
{
file.read((char *)&s,sizeof(s));
if (!file)
break;
top++;
tas[top]=s;
}
file.close();
}
#endif // TAMANAGER_H_INCLUDED
研究生实现插入操作时有些问题,未能解决,解决后再更新博客
把student和person的虚基virtual去掉,问题解决