实验目的:开发一个实验室成员管理系统,取代结构体类型,定义教师类和学生类。定义实验室类,将所有教师和学生作为成员,建议将所有成员数据的访问权限定义为private。
实验要求:引入类、对象、构造函数等面向对象编程因素,通过编程加深对“封装”的认识,掌握类、对象、成员函数、构造函数的定义与使用。为教师类和学生类定义一个共同的基类Member,将它们共同的成员数据和函数封装到基类Member中。基类Member数据成员的访问属性可定义为protected,便于派生类中直接访问。教师类和学生类作为Member的派生类,它们的成员数据和成员函数可能需要做相应修改,应尽量复用Member类的代码。
具体功能包括:菜单显示、菜单选择、添加成员、根据姓名查询成员信息、根据姓名删除成员、显示所有教师或学生、统计教师或学生数量、退出系统等。
学生信息应包括姓名、学号、性别、生日、专业、年级、爱好等;教师信息应该包括姓名、工号、性别、生日、专业、职称、研究方向等。
源代码
1、member.h 头文件
#pragma once
#include<string>
using namespace std;
class Member //定义基类
{
protected:
string id;
string name;
string sex;
string birthday;
string major;
public:
string getId() { return id; }
string getName() { return name; }
string getSex() { return sex; }
string getBirthday() { return birthday; }
string getMajor() { return major; }
void setId(string n1) { id = n1; }
void setName(string n2) { name = n2; }
void setSex(string s) { sex = s; }
void setBirthday(string b) { birthday = b; }
void setMajor(string m) { major = m; }
};
class Student :public Member //创建学生的派生类
{
protected:
string grade;
string hobby;
public:
Student() {} //构造缺省构造函数
Student(string Id, string Name, string Sex, string Birthday, string Major, string Grade, string Hobby) //构造带参数的构造函数
{
id = Id;
name = Name;
sex = Sex;
birthday = Birthday;
major = Major;
grade = Grade;
hobby = Hobby;
}
string getGrade() { return grade; }
string getHobby() { return hobby; }
void setGrade(string g) { grade = g; }
void setHobby(string h) { hobby = h; }
void Input(); //输入学生对象信息
void Print(); //打印学生对象信息
};
class Teacher :public Member //创建教师的派生类
{
protected:
string title;
string field;
public:
Teacher() {} //构造缺省构造函数
Teacher(string Id, string Name, string Sex, string Birthday, string Major, string Title, string Field) //构造带参数的构造函数
{
id = Id;
name = Name;
sex = Sex;
birthday = Birthday;
major = Major;
title = Title;
field = Field;
}
string getTitle() { return title; }
string getField() { return field; }
void setTitle(string t) { title = t; }
void setField(string f) { field = f; }
void Input(); //输入教师对象信息
void Print(); //打印教师对象信息
};
2、Laboratory.h 头文件
#pragma once
#include "member.h"
#define MAXSIZE 10000
class Laboratory
{
Student a[MAXSIZE];
Teacher b[MAXSIZE];
int stuCount;
int teaCount;
public:
Laboratory() //构造缺省构造函数
{
stuCount = 0;
teaCount = 0;
Student stu1("001", "小明", "男", "3.11", "计算机科学与技术", "大一", "打篮球");
Student stu2("002", "小红", "女", "6.23", "软件工程", "大二", "看书");
Teacher tea1("1101", "张三", "男", "9.21", "网络空间安全", "教授", "数据挖掘");
AddStudent(stu1);
AddStudent(stu2);
AddTeacher(tea1);
}
Laboratory(Student stus[], int stu_num, Teacher teas[], int tea_num) //构造带参数的构造函数
{
for (int i = 0;i < stu_num;i++)
{
a[i] = stus[i];
}
for (int i = 0;i < tea_num;i++)
{
b[i] = teas[i];
}
stuCount = stu_num;
teaCount = tea_num;
}
int getStuCount() { return stuCount; }
int getTeaCount() { return teaCount; }
void AddStudent(Student& stu); //添加学生到实验室
void AddTeacher(Teacher& tea); //添加教师到实验室
void DeleteStudent(string& name); //根据名字从实验室删除学生
void DeleteTeacher(string& name); //根据名字从实验室删除教师
bool FindStudentByName(string& name, Student& stu); //根据名字查找学生,查找失败返回False,stu用来存放找到的学生
bool FindTeacherByName(string& name, Teacher& tea); //根据名字查找教师,查找失败返回False,tea用来存放找到的教师
void PrintAllMembers(); //打印实验室全体成员信息
};
3、member.cpp 源文件
#include "member.h"
#include <iostream>
#include<iomanip>
#include <cstring>
using namespace std;
void Student::Input()
{
cin >> id >> name >> sex;
cin >> birthday >> major >> grade;
cin >> hobby;
}
void Teacher::Input()
{
cin >> id >> name >> sex;
cin >> birthday >> major;
cin >> title >> field;
}
void Student::Print()
{
cout << setw(20) << id;
cout << setw(20) << name;
cout << setw(20) << sex;
cout << setw(20) << birthday;
cout << setw(20) << major;
cout << setw(20) << grade;
cout << setw(20) << hobby << endl;
}
void Teacher::Print()
{
cout << setw(20) << id;
cout << setw(20) << name;
cout << setw(20) << sex;
cout << setw(20) << birthday;
cout << setw(20) << major;
cout << setw(20) << title;
cout << setw(20) << field << endl;
}
4、Laboratory.cpp 源文件
#include "Laboratory.h"
#include <iostream>
#include<iomanip>
#include <string>
using namespace std;
void Laboratory::AddStudent(Student& Stu)
{
a[stuCount++] = Stu;
}
void Laboratory::AddTeacher(Teacher& Tea)
{
b[teaCount++] = Tea;
}
//要删除一个成员
//先根据Name搜索a[MAXSIZE]/b[MAXSIZE]找到这个成员的下标i
//然后从这个位置i开始,将后面的元素向前移动一个位置
//然后stuCount/teaCount减1
void Laboratory::DeleteStudent(string& Name)
{
int i = 0,n=0;
for (i;i < stuCount;i++)
{
if (a[i].getName() == Name)
{
for (int j = i;j < stuCount - 1;j++)
a[j] = a[j + 1];
stuCount -= 1;
break;
}
}
if (i == n) cout << '\n' << '\n' << '\n' << "很抱歉,未找到此成员!" << '\n' << endl;
else cout << '\n' << '\n' << '\n' << "删除成功!" << '\n' << endl;
}
void Laboratory::DeleteTeacher(string& Name)
{
int i = 0,n=0;
for (i;i < teaCount;i++)
{
if (b[i].getName() == Name)
{
for (int j = i;j < teaCount - 1;j++)
b[j] = b[j + 1];
teaCount -= 1;
break;
}
}
if (i == n) cout << '\n' << '\n' << '\n' << "很抱歉,未找到此成员!" << '\n' << endl;
else cout << '\n' << '\n' << '\n' << "删除成功!" << '\n' << endl;
}
bool Laboratory::FindStudentByName(string& Name, Student& Stu)
{
int i = 0;
for (i;i < stuCount;i++)
{
if (a[i].getName() == Name)
{
Stu.setId(a[i].getId());
Stu.setName(a[i].getName());
Stu.setSex(a[i].getSex());
Stu.setBirthday(a[i].getBirthday());
Stu.setMajor(a[i].getMajor());
Stu.setGrade(a[i].getGrade());
Stu.setHobby(a[i].getHobby());
break;
}
}
if (i == stuCount) return 0;
else return 1;
}
bool Laboratory::FindTeacherByName(string& Name, Teacher& Tea)
{
int i = 0;
for (i;i < teaCount;i++)
{
if (b[i].getName() == Name)
{
Tea.setId(b[i].getId());
Tea.setName(b[i].getName());
Tea.setSex(b[i].getSex());
Tea.setBirthday(b[i].getBirthday());
Tea.setMajor(b[i].getMajor());
Tea.setTitle(b[i].getTitle());
Tea.setField(b[i].getField());
break;
}
}
if (i == teaCount) return 0;
else return 1;
}
void Laboratory::PrintAllMembers()
{
if (stuCount > 0)
{
cout << "学生信息:\n";
cout << setw(20) << "姓名";
cout << setw(20) << "学号";
cout << setw(20) << "性别";
cout << setw(20) << "生日";
cout << setw(20) << "专业";
cout << setw(20) << "年级";
cout << setw(20) << "爱好" << endl;
for (int i = 0;i < stuCount;i++)
a[i].Print();
}
else cout << "学生信息:" << '\n' << setw(20) << "空" << '\n' << '\n' << endl;
if (teaCount > 0)
{
cout << "教师信息:\n";
cout << setw(20) << "姓名";
cout << setw(20) << "工号";
cout << setw(20) << "性别";
cout << setw(20) << "生日";
cout << setw(20) << "专业";
cout << setw(20) << "职称";
cout << setw(20) << "研究领域" << endl;
for (int i = 0;i < teaCount;i++)
b[i].Print();
}
else cout << "老师信息:" << '\n' << setw(20) << "空" << '\n' << endl;
}
5、main.cpp 源文件
#include "Laboratory.h"
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
#include<fstream>
#include<cstdlib>
#include<Windows.h>
#include<WinBase.h>
#include<stdlib.h>
using namespace std;
void Menu(); //菜单
void Input(); //成员信息录入函数
void Display(); //成员信息显示函数
void Statistics(); //成员总数统计函数
void Delete(); //成员信息删除函数
void Search(); //成员信息查询函数
void Quit(); //退出界面
void Error(); //错误界面
Laboratory theLab;
int main()
{
int choice;
bool flag = 1; //用于判断是否退出循环
while (flag)
{
Menu();
cout << '\n' << "请根据您所需要的服务选择输入对应数字:";
cin >> choice;
cout << '\n';
switch (choice)
{
case 1:Input();break; //添加成员
case 2:Display();break; //显示所有成员信息
case 3:Statistics();break; //统计成员总数
case 4:Delete();break; //删除指定成员信息
case 5:Search();break; //根据成员姓名查找成员信息
case 6:Quit(); flag = 0;break; //安全地退出本系统
default: Error(); //输入错误
}
}
return 0;
}
void Menu()
{
cout << "\n-----------------------------------------欢迎进入实验室成员管理系统C语言版V0.1!----------------------------------------";
cout << "\n★★★★★★★★★★★★★★ 请选择你要进行的操作: ★★★★★★★★★★★★★★";
cout << "\n\n★★★★★★★★★★★★★★ 1.录入成员信息 ★★★★★★★★★★★★★★\n";
cout << "\n\n★★★★★★★★★★★★★★ 2.显示成员信息 ★★★★★★★★★★★★★★\n";
cout << "\n★★★★★★★★★★★★★★ 3.统计成员个数 ★★★★★★★★★★★★★★\n";
cout << "\n★★★★★★★★★★★★★★ 4.删除成员信息 ★★★★★★★★★★★★★★\n";
cout << "\n★★★★★★★★★★★★★★ 5.查询成员信息 ★★★★★★★★★★★★★★\n";
cout << "\n★★★★★★★★★★★★★★ 6.安全退出系统 ★★★★★★★★★★★★★★";
cout << "\n\n ";
}
void Input()
{
system("cls"); //清屏
cout << "★★★★★★★★★★★★★★★★★★★★★★★★★★★★录入成员信息★★★★★★★★★★★★★★★★★★★★★★★★★★★★" << '\n' << endl;
string s1;
int n;
while (1)
{
cout << "请输入您要录入的成员的类型:";
cin >> s1;
if (s1 != "学生" && s1 != "老师")
cout << '\n' << '\n' << "错误:您输入的身份有误,请重新输入!" << '\n' << endl;
else break;
}
cout << '\n' << "请输入您要录入的成员的数量:";
cin >> n;
cout << '\n' << "请依次输入成员信息:" << endl;
if (s1 == "学生")
{
Student stu;
cout << "学号 姓名 性别 生日 专业 年级 爱好" << endl;
for (int i = 0;i < n;i++)
{
stu.Input();
theLab.AddStudent(stu);
}
}
else
{
Teacher tea;
cout << "工号 姓名 性别 生日 专业 职称 研究方向" << endl;
for (int i = 0;i < n;i++)
{
tea.Input();
theLab.AddTeacher(tea);
}
}
cout << '\n' << '\n' << "录入成功!" << endl;
cout << '\n' << "★★★★★★★★★★★★★★★★★★★★★★★★★★★★操作成功★★★★★★★★★★★★★★★★★★★★★★★★★★★★" << '\n' << endl;
system("pause"); //程序暂停运行,按任意键继续
system("cls"); //清屏
}
void Display()
{
system("cls");
cout << "★★★★★★★★★★★★★★★★★★★★★★★★★★★显示成员信息★★★★★★★★★★★★★★★★★★★★★★★★★★★" << '\n' << endl;
if (theLab.getStuCount() + theLab.getTeaCount() == 0) cout << "成员信息为空,请先录入信息!" << endl;
else theLab.PrintAllMembers();
cout << '\n' << '\n' << "★★★★★★★★★★★★★★★★★★★★★★★★★★★★操作成功★★★★★★★★★★★★★★★★★★★★★★★★★★★★" << '\n' << endl;
system("pause");
system("cls");
}
void Statistics()
{
system("cls");
cout << "★★★★★★★★★★★★★★★★★★★★★★★★★★★★统计成员信息★★★★★★★★★★★★★★★★★★★★★★★★★★★★" << '\n' << endl;
int num;
num = theLab.getStuCount() + theLab.getTeaCount();
cout << "成员总数:" << num << "人" << endl;
cout << "学生人数:" << theLab.getStuCount() << "人" << endl;
cout << "老师人数:" << theLab.getTeaCount() << "人" << endl;
cout << '\n' << '\n' << "★★★★★★★★★★★★★★★★★★★★★★★★★★★★操作成功★★★★★★★★★★★★★★★★★★★★★★★★★★★★" << '\n' << endl;
system("pause");
system("cls");
}
void Delete()
{
system("cls");
cout << "★★★★★★★★★★★★★★★★★★★★★★★★★★★★删除成员信息★★★★★★★★★★★★★★★★★★★★★★★★★★★★" << '\n' << endl;
string s2, name;
while (1)
{
while (1)
{
cout << "————————————————————————————" << '\n' << endl;
cout << "请输入您要删除的成员的身份[结束删除请输入“over”]:";
cin >> s2;
if (s2 != "学生" && s2 != "老师" && s2 != "over") cout << '\n' << '\n' << "错误:您输入的身份有误,请重新输入!" << '\n' << endl;
else break;
}
if (s2 == "over") break;
else if (s2 == "学生")
{
if (theLab.getStuCount() == 0) cout << '\n' << '\n' << "该身份成员信息为空,请先录入信息!" << '\n' << endl;
else
{
cout << '\n' << "请输入成员姓名:";
cin >> name;
theLab.DeleteStudent(name);
}
}
else
{
if (theLab.getTeaCount() == 0) cout << '\n' << '\n' << "该身份成员信息为空,请先录入信息!" << '\n' << endl;
else
{
cout << '\n' << "请输入成员姓名:";
cin >> name;
theLab.DeleteTeacher(name);
}
}
}
cout << '\n' << '\n' << "★★★★★★★★★★★★★★★★★★★★★★★★★★★★操作成功★★★★★★★★★★★★★★★★★★★★★★★★★★★★" << '\n' << endl;
system("pause");
system("cls");
}
void Search()
{
system("cls");
cout << "★★★★★★★★★★★★★★★★★★★★★★★★★★★★查询成员信息★★★★★★★★★★★★★★★★★★★★★★★★★★★★" << '\n' << endl;
string s2, name;
Student stu;
Teacher tea;
while (1)
{
while (1)
{
cout << "————————————————————————————" << '\n' << endl;
cout << "请输入您需查询的成员的身份[结束查询请输入“over”]:";
cin >> s2;
if (s2 != "学生" && s2 != "老师" && s2 != "over") cout << '\n' << '\n' << "错误:您输入的身份有误,请重新输入!" << '\n' << endl;
else break;
}
if (s2 == "over") break;
else if (s2 == "学生")
{
if (theLab.getStuCount() == 0) cout << '\n' << '\n' << "该身份成员信息为空,请先录入信息!" << '\n' << endl;
else
{
cout << '\n' << "请输入您要查询的成员的姓名:";
cin >> name;
cout << '\n';
theLab.FindStudentByName(name, stu);
if (theLab.FindStudentByName(name, stu) == 1)
{
cout << " 学号:" << stu.getId() << '\n';
cout << " 姓名:" << stu.getName() << '\n';
cout << " 性别:" << stu.getSex() << '\n';
cout << " 生日:" << stu.getBirthday() << '\n';
cout << " 专业:" << stu.getMajor() << '\n';
cout << " 年级:" << stu.getGrade() << '\n';
cout << " 爱好:" << stu.getHobby() << endl;
cout << '\n' << '\n' << "查询成功!" << '\n' << endl;
}
else cout << '\n' << '\n' << "很抱歉,未找到此成员!" << '\n' << endl;
}
}
else
{
if (theLab.getTeaCount() == 0) cout << '\n' << '\n' << "该身份成员信息为空,请先录入信息!" << '\n' << endl;
else
{
cout << '\n' << "请输入您要查询的成员的姓名:";
cin >> name;
cout << '\n';
theLab.FindTeacherByName(name, tea);
if (theLab.FindTeacherByName(name, tea) == 1)
{
cout << " 工号:" << tea.getId() << '\n';
cout << " 姓名:" << tea.getName() << '\n';
cout << " 性别:" << tea.getSex() << '\n';
cout << " 生日:" << tea.getBirthday() << '\n';
cout << " 专业:" << tea.getMajor() << '\n';
cout << " 职称:" << tea.getTitle() << '\n';
cout << " 研究方向:" << tea.getField() << endl;
cout << '\n' << '\n' << "查询成功!" << '\n' << endl;
}
else cout << '\n' << '\n' << "很抱歉,未找到此成员!" << '\n' << endl;
}
}
}
cout << '\n' << '\n' << "★★★★★★★★★★★★★★★★★★★★★★★★★★★★操作成功★★★★★★★★★★★★★★★★★★★★★★★★★★★★" << '\n' << endl;
system("pause");
system("cls");
}
void Quit()
{
system("cls");
cout << '\n';
cout << "★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★" << endl;
cout << " 您已安全退出本系统" << endl;
cout << "★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★" << '\n' << endl;
system("pause");
}
void Error()
{
system("cls");
cout << '\n' << "错误:未找到对应功能,请输入正确的数字!" << '\n' << endl;
system("pause");
system("cls");
}
运行效果截图
1、主菜单
2、录入成员信息
3、显示成员信息
4、统计成员数量
5、删除成员信息
6、查询成员信息
7、安全退出系统