程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++專題之實現信息系統(抽象類、繼承、鏈表)

C++專題之實現信息系統(抽象類、繼承、鏈表)

編輯:C++入門知識

轉載請注明出處: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.cpp

 StudentList.cpp: implementation of the CStudentList class.
//
//////////////////////////////////////////////////////////////////////
#include "StudentLink.h"
#include "StudentList.h"
#include "Node.h"
#include 
#include 
#include 
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 
#include 
#include 
#include "StudentList.h"
using namespace std;
#include "Person.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CStudent::CStudent()
{

}

CStudent::~CStudent()
{
	
        cout<<"在次調用Stduent析構函數."<StudentNumber=StudentNumber;
	this->StudentName=new char[strlen(StudentName)+1];
	strcpy(this->StudentName,StudentName);
	return ;
}


void CStudent::print_studentHuman()                                                   //打印                         
{ 
        cout<<"StudentNumber is "<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 
#include 
#include 
#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<<"節點為空."<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 
#include 
#include 
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 
#include 
#include 

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 "<TeacherNumber<<" Teacher's Name is "<TeacherName<<" Teacher's Title is "<TeacherTitle<
main.cpp文件

#include "Course.h"
#include "Student.h"
#include "Scollege.h"
#include "Primary.h"
#include "StudentList.h"
#include "Teacher.h"
#include "TeacherList.h"
#include 
#include 
#include 

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<<"學生和教師信息系統"<>stu;
			 for(i=0;i>list2[i].StudentNumber;
				 cout<>buffer4;
				 list2[i].StudentName=new char[strlen(buffer4)+1];
				 strcpy(list2[i].StudentName,buffer4);
				 cout<>list2[i].HumanAge;
		                  cout<>teach;
			 for(i=0;i>list3[i].HumanAge;
				 cout<>list3[i].TeacherNumber;
				 cout<>buffer5;
				 list3[i].TeacherName=new char[strlen(buffer5)+1];
				 strcpy(list3[i].TeacherName,buffer5);
				 cout<>buffer6;
				 list3[i].TeacherTitle=new char[strlen(buffer6)+1];
				 strcpy(list3[i].TeacherTitle,buffer6);
				 cout<
實現以上增加和打印功能的信息系統,運用到抽象類、繼承、鏈表等C++中常見的知識和數據結構,以下是運行效果:


\

\

\

\

\

\



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved