转载请注明出处:http://my.csdn.net/feng1790291543
名称:老师和学生的信息管理系统
功能和要求:
1、有CPerson(抽象类)、CTeacher、CStudent三个类,使用继承关系。根据界面的菜单(如打印所有老师信息、打印所有学生信息、打印所有人员信息、
增加老师信息、增加学生信息)等。
2、最好使用链表来实现(为简化,也可先用数组来实现,但录入的人员个数就有限制了)
以下是代码模块分析:
基类Person.h: //抽象类
class CPerson { public: int HumanAge; //抽象类变量 CPerson(); //无参构造函数 virtual ~CPerson(); CPerson(int HumanAge); //含参数的构造函数 void PrintHuman(); };
基类源程序Person.cpp
#include "Person.h" #include <iostream> #include <stdlib.h> #include <string.h> using namespace std; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CPerson::CPerson() { } CPerson::~CPerson() { } CPerson::CPerson(int HumanAge) { this->HumanAge=HumanAge; return ; } void CPerson::PrintHuman() { cout<<" Person‘age is "<<this->HumanAge<<endl; return ; }
模块化——节点Node.h文件
typedef void (*pfun)(void *data); //定义函数指针,为以后使用回调函数做准备 class CNode { public: void *data; //节点数据值 CNode *next; //节点下个指针(指针域) public: CNode(); virtual ~CNode(); };
节点Node.cpp:
//不做任何变化
#include "Node.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CNode::CNode() { } CNode::~CNode() { }
然后,就是CStudent学生类的链表
StudentLink.h
#include "Node.h" class CStudentLink { public: CStudentLink(); virtual ~CStudentLink(); CNode *CreateLink(); //创建节点函数 void StudentLinkAdd(CNode **head,void *data); //学生节点的添加函数 void PrintStudentLink(CNode *head,pfun print_student); //学生链表打印函数 };
源文件 StudentLink.cpp
// StudentLink.cpp: implementation of the CStudentLink class. // ////////////////////////////////////////////////////////////////////// #include "StudentLink.h" #include <iostream> #include <stdlib.h> #include <string.h> #include "Node.h" using namespace std; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CStudentLink::CStudentLink() { } CStudentLink::~CStudentLink() { } CNode *CStudentLink::CreateLink() { CNode *head=NULL; return head; } void CStudentLink::StudentLinkAdd(CNode **head,void *data) { CNode *node=new CNode; //动态分配内存空间 CNode *node2; //定义CNode 类型指针 if(node==NULL) { cout<<"节点为空."<<endl; //节点为空,退出程序 exit(0); } node->data=data; //增加节点值,下个节点置空 node->next=NULL; if(*head==NULL) { *head=node; //如果头结点为空,则将第一个值追加到第一个节点 } else { node2=*head; //node2保存head的值 while(node2->next!=NULL) { node2=node2->next; //node2遍历链表 } node2->next=node; //增加节点 } return ; } void CStudentLink::PrintStudentLink(CNode *head,pfun print_student) { CNode *node=head; if(head==NULL) { return ; //当头为空时,不做任何事情 } while(node!=NULL) { print_student(node->data); //传递函数指针,指向print_student()函数的入口地址,打印节点的值,直到node为空,才退出 node=node->next; } return ; }
TeacherList.h
#include "Node.h" #include "TeacherLink.h" class CTeacherList :public CTeacherLink { public: CNode *header; CTeacherList(); virtual ~CTeacherList(); void TeacherListAdd(void *data); //这里实现跟上述StudentLink一样的功能,只是模块化分层了~ void PrintList(pfun print_teacher); };
源文件 TeacherList.cpp
StudentList.cpp: implementation of the CStudentList class. // ////////////////////////////////////////////////////////////////////// #include "StudentLink.h" #include "StudentList.h" #include "Node.h" #include <iostream> #include <stdlib.h> #include <string.h> using namespace std; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CStudentList::CStudentList() { head=CStudentLink::CreateLink(); //直接调用StudentLink的方法,实现链表的创建 } CStudentList::~CStudentList() { } void CStudentList::StudentListAdd(void *data) { CStudentLink::StudentLinkAdd(&head,data); //直接调用StudentLink的方法,实现节点的添加 return ; } void CStudentList::PrintList(pfun print_student) { CStudentLink::PrintStudentLink(head,print_student); //直接调用StudentLink的方法,实现节点的打印 return ; }
Student.h文件 //应用层实现
#include "Person.h" class CStudent :public CPerson { public: int StudentNumber; char *StudentName; //学号或者称呼/外号 public: CStudent(); virtual ~CStudent(); CStudent(int StudentNumber,char *StudentName,int HumanAge); //有惨构造函数 void print_studentHuman(); //打印函数 };
源文件 Student.cpp
// Student.cpp: implementation of the CStudent class. // ////////////////////////////////////////////////////////////////////// #include "Student.h" #include <iostream> #include <stdlib.h> #include <string.h> #include "StudentList.h" using namespace std; #include "Person.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CStudent::CStudent() { } CStudent::~CStudent() { cout<<"在次调用Stduent析构函数."<<endl; return ; } CStudent::CStudent(int StudentNumber,char *StudentName,int HumanAge):CPerson(HumanAge) //继承父类,构造函数初始化 { this->StudentNumber=StudentNumber; this->StudentName=new char[strlen(StudentName)+1]; strcpy(this->StudentName,StudentName); return ; } void CStudent::print_studentHuman() //打印 { cout<<"StudentNumber is "<<StudentNumber<<endl<<"StudentName is"<<StudentName<<endl; CPerson::PrintHuman(); //调用打印父类变量 return ; }TeacherLink.h //以下与以上学生类、链表的建立同理
#include "Node.h" class CTeacherLink { public: CTeacherLink(); virtual ~CTeacherLink(); CNode *CreateLink(); void TeacherLinkAdd(CNode **head,void *data); void PrintTeacherLink(CNode *head,pfun Print_teacher); };TeacherLink.cpp
// TeacherLink.cpp: implementation of the CTeacherLink class. // ////////////////////////////////////////////////////////////////////// #include "TeacherLink.h" #include <iostream> #include <stdlib.h> #include <string.h> #include "Node.h" using namespace std; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CTeacherLink::CTeacherLink() { } CTeacherLink::~CTeacherLink() { } CNode *CTeacherLink::CreateLink() { CNode *head=NULL; return head; } void CTeacherLink::TeacherLinkAdd(CNode **head,void *data) { CNode *node=new CNode; CNode *node2; if(node==NULL) { cout<<"节点为空."<<endl; exit(0); } node->data=data; node->next=NULL; if(*head==NULL) { *head=node; } else { node2=*head; while(node2->next!=NULL) { node2=node2->next; } node2->next=node; } return ; } void CTeacherLink::PrintTeacherLink(CNode *head,pfun print_teacher) { CNode *node=head; if(head==NULL) { return ; } while(node!=NULL) { print_teacher(node->data); node=node->next; } return ; }
TeacherList.h
#include "Node.h"
#include "TeacherLink.h"
class CTeacherList :public CTeacherLink
{
public:
CNode *header;
CTeacherList();
virtual ~CTeacherList();
void TeacherListAdd(void *data);
void PrintList(pfun print_teacher);
//CNode *CreateList();
};
TeacherList.cpp
// TeacherList.cpp: implementation of the CTeacherList class. // ////////////////////////////////////////////////////////////////////// #include "TeacherList.h" #include "TeacherLink.h" #include "Node.h" #include <iostream> #include <stdlib.h> #include <string.h> using namespace std; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CTeacherList::CTeacherList() { header=CTeacherList::CreateLink(); } CTeacherList::~CTeacherList() { } void CTeacherList::TeacherListAdd(void *data) { CTeacherLink::TeacherLinkAdd(&header,data); return ; } void CTeacherList::PrintList(pfun print_teacher) { CTeacherLink::PrintTeacherLink(header,print_teacher); return ; }
Teacher.h
#include "StudentLink.h" #include "Node.h" #include "Person.h" class CTeacher :public CPerson { public: int TeacherNumber; //教师编号 char *TeacherTitle; //教师职称 char *TeacherName; //教师姓名 CTeacher(); virtual ~CTeacher(); CTeacher(int HumanAge,int TeacherNumber,char *TeacherName,char *TeacherTitle); //含参数构造函数 void PrintTeacher(); //打印教师 };
Teacher.cpp
// Teacher.cpp: implementation of the CTeacher class. // ////////////////////////////////////////////////////////////////////// #include "Teacher.h" #include <iostream> #include <stdlib.h> #include <string.h> using namespace std; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CTeacher::CTeacher() { } CTeacher::~CTeacher() { } CTeacher::CTeacher(int HumanAge,int TeacherNumber,char *TeacherName,char *TeacherTitle):CPerson(HumanAge) { this->TeacherName=new char[strlen(TeacherName)+1]; strcpy(this->TeacherName,TeacherName); this->TeacherNumber=TeacherNumber; this->TeacherTitle=new char[strlen(TeacherTitle)+1]; strcpy(this->TeacherTitle,TeacherTitle); return ; } void CTeacher::PrintTeacher() { cout<<"Teacher‘number is "<<this->TeacherNumber<<" Teacher‘s Name is "<<this->TeacherName<<" Teacher‘s Title is "<<this->TeacherTitle<<endl; CPerson::PrintHuman(); return ; }
main.cpp文件
#include "Course.h" #include "Student.h" #include "Scollege.h" #include "Primary.h" #include "StudentList.h" #include "Teacher.h" #include "TeacherList.h" #include <iostream> #include <stdlib.h> #include <string.h> using namespace std; #define max 2 void print_student(void *data) //应用层打印学生函数 { CStudent *stu=(CStudent *)data; stu->print_studentHuman(); return ; } void print_teacher(void *data) //应用层打印教师函数 { CTeacher *teach=(CTeacher *)data; teach->PrintTeacher(); return ; } int main() { int i; cout<<"学生和教师信息系统"<<endl; CStudentList list; //定义一个list学生连表对象 CTeacherList teacher; //定义一个teacher教师链表对象 CStudent *list2=new CStudent[10]; //定义学生数组 CTeacher *list3=new CTeacher[10]; //定义教师数组 char buffer4[20]={‘\0‘}; char buffer5[20]={‘\0‘}; char buffer6[20]={‘\0‘}; char buffer7[20]={‘\0‘}; char buffer8[20]={‘\0‘}; char buffer9[20]={‘\0‘}; char buffer10[20]={‘\0‘}; //定义缓存,避免字符串越界溢出 int m; //功能选项变量 int stu; //学生人数 int teach; //教师人数 while(1) { cout<<"(1)ADD Student‘s Information."<<endl; cout<<"(2)Print Student‘s Information."<<endl; cout<<"(3)ADD Teacher‘s Information."<<endl; cout<<"(4)Print Student‘s Information."<<endl; cout<<"(5)Print ALL Information."<<endl; cout<<"(6)Quit System."<<endl; m=getchar(); switch(m) { case ‘1‘: cout<<endl<<"准备增加学生信息."<<endl; cout<<"输入增加的人数个数:"; cin>>stu; for(i=0;i<stu;i++) { cout<<"学号:"; cin>>list2[i].StudentNumber; cout<<endl; cout<<"学生姓名:"; cin>>buffer4; list2[i].StudentName=new char[strlen(buffer4)+1]; strcpy(list2[i].StudentName,buffer4); cout<<endl; cout<<"学生年龄:"; cin>>list2[i].HumanAge; cout<<endl; } for(i=0;i<stu;i++) { list.StudentListAdd((void *)&list2[i]); } break; case ‘2‘: cout<<endl<<"准备输出学生信息."<<endl; list.PrintList(print_student); break; case ‘3‘: cout<<endl<<"准备增加教师信息."<<endl; cout<<"输入增加的人数个数:"; cin>>teach; for(i=0;i<teach;i++) { cout<<"教师年龄:"; cin>>list3[i].HumanAge; cout<<endl; cout<<"教师编号:"; cin>>list3[i].TeacherNumber; cout<<endl; cout<<"教师姓名:"; cin>>buffer5; list3[i].TeacherName=new char[strlen(buffer5)+1]; strcpy(list3[i].TeacherName,buffer5); cout<<endl; cout<<"教师职称:"; cin>>buffer6; list3[i].TeacherTitle=new char[strlen(buffer6)+1]; strcpy(list3[i].TeacherTitle,buffer6); cout<<endl; } for(i=0;i<teach;i++) { teacher.TeacherListAdd((void *)&list3[i]); } break; case ‘4‘: cout<<endl<<"准备输出教师信息."<<endl; teacher.PrintList(print_teacher); break; case ‘5‘: cout<<endl<<"准备输出所有人员的信息."<<endl; if(list2!=NULL) { list.PrintList(print_student); } if(list3!=NULL) teacher.PrintList(print_teacher); break; case ‘6‘: cout<<endl<<"退出系统."<<endl; exit(0); break; } fflush(stdin); //清理缓存,防止反复打印 } return 0; }
实现以上增加和打印功能的信息系统,运用到抽象类、继承、链表等C++中常见的知识和数据结构,以下是运行效果: