研究生类

来源:互联网 发布:德兴软件ui设计师 编辑:程序博客网 时间:2024/05/16 01:14
#include<iostream>  #include<string>  using namespace std;  class Student  {  public:      virtual void get()=0;      virtual void display()=0;      string name;      int num;  };  class UGraduate :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 UGraduate::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; UGraduate 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;  }  

0 0
原创粉丝点击