天天看點

C++作業

#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
	virtual void get()=0;
	virtual void display()=0;
	string name;
	int num;
};
class UnderGraduate :public Student//大學生
{
public:
	void get()
	{
		cout << "姓名:"; cin >> name;
		cout << "學号:"; cin >> num; 
		cout << "班級号:"; cin >> classnum;
	}
	void display();
private:
	int classnum;
};
class Graduate :public Student//研究所學生
{
public:
	void get()
	{
		cout << "姓名:"; cin >> name;
		cout << "學号:"; cin >> num;; 
		cout << "導師姓名:"; cin >> Tutor;
	}
	void display();
private:
	string Tutor;
};
void UnderGraduate::display()
{
	cout << "姓名:" << name;
	cout << "學号:"<< num;
	cout << "班級号:" <<classnum<<endl;
}
void Graduate::display()
{
	cout << "姓名:"<< name;
	cout << "學号:"<< num;
	cout << "導師姓名:"<<Tutor;
}
template <typename T>
class SList;

template <typename T>
class Node
{
	friend class SList<T>;
public:
	Node(T *data) :data(data), next(NULL){}
private:
	T *data;
	Node<T> *next;
};
template < typename T>
class SList
{
public:
	SList():head(NULL), tail(NULL){}
	void Insert(T *newNode)
	{	
		Node<T> *t = new Node<T>(newNode);	
		if (!head)	
		{		
			head = tail = t;		
			length++;	
		}	
		else	
		{	
			tail->next = t;		
			tail = t;		
			length++;		
		}
	}
	void Delete()
	{	
		Node<T> *t = head;	
		head = head->next;	
		delete t;
	}
	void Print()
	{	
		if (!head)	
		{		
			cout << "連結清單空..." << endl;		
			return;	
		}
		Node<T> *t;	
		for (t = head; t; t = t->next)	
		{		
			t->data->display();	
		}
	}
private:
	int length;
	Node<T> *head;
	Node<T> *tail;
};

int main()
{
	char c; UnderGraduate U; Graduate G;
	SList<Student> List;
	for (;;)
	{
		cout << "建立學生:類型(U)大學生,(G)研究所學生,(E)結束:";
		cin >> c;
		if (c == 'E'){ cout << "銷毀連結清單."<<endl; break; }
		else if (c == 'U' || c == 'G')
			switch (c)
			{
			case 'U':U.get(); List.Insert(&U); break;
			case 'G':G.get(); List.Insert(&G); break;
			default: break;
			}		
		else cout << "輸入無效請重新輸入:" << endl;
	}
	List.Print();
	List.Delete();
	List.Delete();
	return 0;
}
           
<img src="https://img-blog.csdn.net/20150527090633384?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcGlwaTA5MDgwN2xpbGk=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
           
下一篇: 35頁作業