[The Daily] cats Queue - "Subclass Manager"

【topic】

  • Dogs and cats following categories:
class Pet
{
    private:
        string m_type;
        
    public:
        Pet(string _type){ m_type=_type; }
        string GetPetType(){ return m_type; }
};

class Dog : public Pet
{
    public:
        Dog() : Pet("dog"){}
};

class Cat : public Pet
{
    public:
        Cat() : Pet("cat"){}    
};
  • Required to achieve the following functions:
    • add: The method of Example cat or dog class in the queue class
    • pollAll: The team listed team (in the order of the queue)
    • pollDog: The dog class instance dequeue the queue according to the order of the queue
    • pollCat: empathy
    • isEmpty: whether the queue is empty
    • isDogEmpty: Check whether there is queue class instance dog
    • isCatEmpty: empathy


【analysis】

  • Two dogs and cats are all Pet Subclass class. Clearly, the most suitable method is to use a combination of two queues in the same class inside. So the question is, how total the team in order to determine the two queues of it?
  • Determining the total order into the team is mainly to meet pollAll implementation of the method.
  • In the book clearly pointed out the best not to change the original class of cats and dogs, so we can do these two classes a wrapper. The data type is represented by Pet Cat and Dog, Pet as a unified data type.
  • Tag design a timestamp can be recorded into the team order

Here's a question: Now that cats and dogs since it is packaged into the same type, why not just use a queue to store?
Because we have to consider the above seven operation, 4, respectively, based on the operation, if only to a queue for storage, the complexity will be increased substantially.
It is quite obvious, analyze specific issues, is to design a special data structure examined this question


【achieve】

  • Implementation language: C ++
  • Source
/*猫狗队列*/

#include<iostream>
#include<string>
#include<queue>
using namespace std;


class Pet
{
    private:
        string m_type;
        
    public:
        Pet(){}
        Pet(string _type){ m_type=_type; }
        string GetPetType(){ return m_type; }
};


class Dog : public Pet
{
    public:
        Dog() : Pet("dog"){}
};


class Cat : public Pet
{
    public:
        Cat() : Pet("cat"){}    
};

//将两个同父类的子类(Cat And Dog)包装起来
//方便用同种泛型的Queue包装起来 
class PetEnterQueue
{
    private:
        Pet m_pet;
        long m_count;    //这个类的最终目的:就是包装一个时间戳,以确定入队的顺序 
        
    public:
        PetEnterQueue(Pet _pet,long _count)
        {
            m_pet=_pet;
            m_count=_count;
        }
        
        Pet getPet() { return m_pet;}
        long getCount(){ return m_count;}
        string getType(){ return m_pet.GetPetType();}
}; 

//猫狗队列类
class CatAndDogQueue
{
    private:
        queue<PetEnterQueue> m_catQueue;  //猫队列 
        queue<PetEnterQueue> m_dogQueue;  //狗队列 
        long m_Count;                     //队列元素的个数
        
    public:
        CatAndDogQueue(){ m_Count=0; }    //构造函数中将容量设为0
        
        //主要操作函数
        
        //add:方法将cat类或dog类的实例放入队列
        void add(Pet pet)
        {
            if(pet.GetPetType()=="dog") //狗元素
                m_dogQueue.push(PetEnterQueue(pet,m_Count++)); 
            else if(pet.GetPetType()=="cat")
                m_catQueue.push(PetEnterQueue(pet,m_Count++));
            else
                cout<<"添加的元素类型有误,请检查!"<<endl;
        } 
        
        //pollAll : 将队列出队(按照队列的先后顺序)
        Pet pollAll()
        {
            Pet pet;
            if(!m_catQueue.empty()&&!m_dogQueue.empty())    //两个队列皆不为空时 
            {
                if(m_catQueue.front().getCount() > m_dogQueue.front().getCount())
                {
                    pet=m_dogQueue.front().getPet();
                    m_dogQueue.pop();
                }
                else
                {
                    pet=m_catQueue.front().getPet();
                    m_catQueue.pop();
                }
            }
            else if(!m_dogQueue.empty()) 
            {
                pet=m_dogQueue.front().getPet();
                m_dogQueue.pop();
            }
            else if(!m_catQueue.empty())
            {
                pet=m_catQueue.front().getPet();
                m_catQueue.pop();
            }
            else
            {
                cout<<"当前队列为空队,无法出队!"<<endl;
                return pet; 
            }
            
            return pet; 
        }
        
        //pollDog:将队列的dog类实例按照队列的先后顺序出队 
        Pet pollDog()
        {
            Pet pet;
            if(m_dogQueue.empty())
            {
                cout<<"狗队为空,无法出队"<<endl;
                return pet;
            }
            pet=m_dogQueue.front().getPet();
            m_dogQueue.pop();
            return pet;
        } 
        
        //pollCat:同理
        Pet pollCat()
        {
            Pet pet;
            if(m_catQueue.empty())
            {
                cout<<"猫队为空,无法出队"<<endl;
            }
            pet=m_catQueue.front().getPet();
            m_catQueue.pop();
            return pet;
        }
        
        //isEmpty:队列是否为空
        bool isEmpty()
        {
            return m_dogQueue.empty()&&m_catQueue.empty(); 
        } 
        
        //isDogEmpty:检查队列是否存在dog类实例
        bool isDogEmpty()
        {
            return m_dogQueue.empty();
        }
        
        //isCatEmpty:同理
        bool isCatEmpty()
        {
            return m_catQueue.empty();
        }   
        
        void ShowCount()
        {
            cout<<"dog count : "<<m_dogQueue.size()<<endl;
            cout<<"cat count : "<<m_catQueue.size()<<endl;
            //cout<<"合计      : "<< m_dogQueue.size()+m_catQueue.size()<<endl;
        }   
}; 


[In addition]

  • "Subclass Manager", as the name implies, it can be well managed with a collection of different subclasses of a superclass. This can be extended to apply to other data structures
  • In C ++, subclass instance transformed parent class instance (in my impression, it is not, it is not advocated). And on Java can be cast. Thus, in PollCat () method and the like may also return type of the subclass the C ++ I directly back to the Pet (parent type)
  • Can be directly assigned to the parent instance instance of a subclass (not thought of that at the time of writing code)
Pet pet;
Dog dog;
Pet *pPet=&dog;
*pPet=pet;     //赋值成功

Guess you like

Origin www.cnblogs.com/ZhuSenlin/p/12388326.html