C++专题之实现信息系统(抽象类、继承、链表)
来源:互联网 发布:网络语炸鱼是什么意思 编辑:程序博客网 时间:2024/06/07 16:27
转载请注明出处: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 2void 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++中常见的知识和数据结构,以下是运行效果:
0 0
- C++专题之实现信息系统(抽象类、继承、链表)
- 继承之抽象类
- C#OOP之八 继承 接口和抽象类
- 抽象类----接口----继承---实现
- C语言实现学生信息系统
- 抽象类,接口继承与实现关系
- 抽象类的继承与方法实现
- Java 抽象类 接口 继承 实现
- Java,抽象类,接口,实现,继承
- 继承、实现 、抽象
- 继承之接口和抽象类区别
- 继承之接口和抽象类区别
- Java基础之继承,抽象类,接口
- JavaSE之抽象类的继承
- java之继承、多态、抽象类
- JAVASE之继承和抽象类
- 黑马程序员_JavaSE基础09 之 继承extends 实现implements 抽象类 接口
- 【C#】C#中的继承 (继承、抽象类和抽象方法、接口)
- windows防火墙无法更改某些设置,错误代码0x80070422解决方法
- 程序员写代码为啥用俩屏幕?
- 很有意思的简单题目,你会做吗?
- 进入排名了
- 手机网站如何建立,怎么开手机网站
- C++专题之实现信息系统(抽象类、继承、链表)
- ubuntu安装ia32-libs失败解决方法
- zendStudio 颜色配置
- ORA-29861: 域索引标记为 LOADING/FAILED/UNUSABLE
- 指针函数与函数指针
- python string与list互转
- jsp c:foreach 说明
- python if 多条件和获取字符串长度方法
- C++之继承和动态内存分配