转载请注明出处: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#include #include using namespace std; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CPerson::CPerson() { } CPerson::~CPerson() { } CPerson::CPerson(int HumanAge) { this->HumanAge=HumanAge; return ; } void CPerson::PrintHuman() { cout<<" Person'age is "< HumanAge<
模块化――节点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#include #include #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<<"节点为空."< 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.cppStudentList.cpp: implementation of the CStudentList class. // ///////////////////////////////////////////////////////////////////