研究生类

#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;  
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值