这道有点坑,不太懂怎么构造那个队列模板,下面的仅供参考。
做出来的希望能留言给我看看是怎么实现的
Queue.cpp
//#include <iostream> //#include "Work.cpp" //#include <memory>//智能指针模板 //using namespace std; //为了简化,这个队列我就只定义简单的功能了 template <class T> class Queue{ private: struct Node{//在类里面定义一个结构,用于实现队列中的节点 T *t; Node *next; }; //enum{MAX=10}; Node *front; Node *rear; int size; public: Queue() { front=rear=nullptr; size=0; } ~Queue() { //T *p=front; //while(p!=rear) //delete p; } bool isEmpty() { return front==nullptr; } /* 队列长度不设上限 bool isFull() { return size==MAX; }*/ bool enQueue(T *t) { Queue::Node *add=new Queue::Node; add->t=t;//需要该类定义一个“=”运算符 if(isEmpty()) { front=add; add->next=nullptr; rear=add->next; return true; } else { rear=add; add->next=nullptr; rear=add->next; return true; } return false; } T show() { Node *p=front; if(p==rear) return rear->t; else { p=p->next; return show(); } } };
Worker.cpp
#include <iostream> #include <string> using namespace std; class Worker{ private: string fullname; long id; protected: virtual void Data()const{ cout<<"Name:"<<fullname<<endl; cout<<"Employee ID:"<<id<<endl; }; virtual void Get() { getline(cin,fullname); cout<<"Enter worker's ID:"; cin>>id; while(cin.get()!='\n') continue; }; public: Worker():fullname("no one"),id(0L) {}; Worker(const string &s,long n):fullname(s),id(n) {}; Worker(const Worker &w) { fullname=w.fullname; id=w.id; } virtual ~Worker()=0{}; virtual void Set()=0;//虚基类里面的纯虚方法可以交由派生类具体实现 virtual void Show()const=0; }; class Waiter:virtual public Worker{ private: int panache; protected: void Data()const{ cout<<"Panache rating:"<<panache<<endl; }; void Get() { cout<<"Enter waiter's panache rating:"; cin>>panache; while(cin.get()!='\n') continue; }; public: Waiter():Worker(),panache(0){} Waiter(const string &s,long n,int p=0):Worker(s,n),panache(p){} Waiter(const Worker &wk,int p=0):Worker(wk),panache(p){} void Set() { cout<<"Enter waiter's name:"; Worker::Get(); Get(); }; void Show()const { cout<<"Category waiter:"<<endl; Worker::Data(); Data(); }; }; class Singer:virtual public Worker { protected: enum{Vtypes=7};//类似于前置声明 enum{other,alto,contralto,soprano,bass,baritone,tenor}; private: static char *pv[Vtypes];//={"other","alto","contralto","soprano","bass","baritone","tenor"}; int voice; protected: void Data()const { cout<<"Vocl range:"<<pv[voice]<<endl; }; void Get(){ cout<<"Enter number for singer's vocal range:"<<endl; int i; for(i=0;i<Vtypes;i++)//用于展示 { cout<<i<<":"<<pv[i]<<" "; if(i%4==3) cout<<endl; } if(i%4!=0) cout<<endl; cin>>voice; while(cin.get()!='\n') continue; } public: Singer():Worker(),voice(other){} Singer(const string &s,long n,int v=other):Worker(s,n),voice(other){} Singer(const Worker &wk,int v=other):Worker(wk),voice(v){} void Set() { cout<<"Enter singer's name:"; Worker::Get(); Get(); } void Show()const { cout<<"Category:singer"<<endl; Worker::Data(); Data(); } }; //char *Singer::pv[Singer::Vtypes]={"other","alto","contralto","soprano","bass","baritone","tenor"}; class SingerWaiter:public Singer,public Waiter { protected: void Data()const { Singer::Data(); Waiter::Data(); } void Get() { Waiter::Get(); Singer::Get(); } public: SingerWaiter(){} SingerWaiter(const string &s,long n,int p=0,int v=other) :Worker(s,n),Waiter(s,n,p),Singer(s,n,v){} SingerWaiter(const Worker &wk,int p=0,int v=other) :Worker(wk),Waiter(wk,p),Singer(wk,v){} SingerWaiter(const Waiter &wt,int v=other) :Worker(wt),Waiter(wt),Singer(wt,v){} SingerWaiter(const Singer &sg,int p=0) :Worker(sg),Waiter(sg,p),Singer(sg){} void Set() { cout<<"Enter singerWaiter 's name:"<<endl; Worker::Get(); Get(); } void Show()const{ cout<<"Category:singerWaiter:"<<endl; Worker::Data(); Data(); } };
main143.cpp
#include <iostream> #include "QueueTP.cpp" #include "Work.cpp" //#include <string> using namespace std; //char *Singer这一句如果放到其他地方会出现重定义错误; char *Singer::pv[Singer::Vtypes]={"other","alto","contralto","soprano","bass","baritone","tenor"}; const int SIZE=5; void main143() { Queue<Worker*> line;//注意类型应为Worker* Worker *lolas[SIZE]; int ct; for(ct=0;ct<SIZE;ct++) { char choice; cout<<"Enter the employee category:"<<endl <<"w:waiter s:singer"<<endl <<"t:singerWaiter q:quit"<<endl; cin>>choice; while(strchr("wstq",choice)==NULL)//p564关于strchr有解释 { cout<<"Please enter a w,s,t or q:"; cin>>choice; } if(choice=='q') break; switch(choice) { case 'w':lolas[ct]=new Waiter;break; case 's':lolas[ct]=new Singer;break; case 't':lolas[ct]=new SingerWaiter;break; /* case 'w':line.enQueue(new Waiter());break; case 's':lolas[ct]=new Singer;break; case 't':lolas[ct]=new SingerWaiter;break; */ } cin.get(); lolas[ct]->Set(); line.enQueue(&lolas[ct]); } cout<<"Here is your staff:"<<endl; int i; for(i=0;i<ct;i++) { lolas[i]->Show(); cout<<endl; } cout<<endl<<endl; cout<<"Next is the line:"<<endl; //line.show()->Show(); cin.get(); };
——————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:https://www.royalchen.com/
author:royalchen
Email:royalchen@royalchen.com
———————————————————————————————————————————————————