C++模板和多食性

C++ Templates and Polymorhpism?

本文关键字:C++      更新时间:2023-10-16

我正在尝试在C++中制作java队列接口。从它继承时,我无法让它正确编译。

这是 QueueInterface.h

#ifndef QUEUEINTERFACE_H_
#define QUEUEINTERFACE_H_
/**
 * Java like interface
 */
template<class E>
class QueueInterface {
public:
    virtual bool add(E e);
    virtual E element();
    virtual bool offer(E e);
    virtual E peek();
    virtual E poll();
    virtual E remove();
    virtual int size();
    virtual bool isEmpty();
    virtual ~QueueInterface();
};
#endif /* QUEUEINTERFACE_H_ */

这是 LinkedQueue.h

#ifndef QUEUE_H_
#define QUEUE_H_
#include "QueueInterface.h"
#include "LinkedList.h"
template<class T>
class LinkedQueue: public QueueInterface<T> {
private:
    LinkedList<T> linkedList;
public:
//DOES NOT COMPILEE
//  virtual bool QueueInterface<T>::add(T t) {
//      return false;
//  }
//  virtual T QueueInterface<T>::element() {
//      T t;
//      return T;
//  }
//  virtual bool QueueInterface::offer(T e) {
//      return false;
//  }
//  virtual T QueueInterface::peek() {
//      T t;
//      return t;
//  }
//  virtual T QueueInterface::poll() {
//      T t;
//      return t;
//  }
//  virtual T QueueInterface::remove() {
//      T t;
//      return t;
//  }
//  virtual int QueueInterface::size() {
//      return -1;
//  }
//  virtual bool QueueInterface::isEmpty() {
//      return false;
//  }
//  virtual QueueInterface::~QueueInterface() {
//  }
};
#endif /* QUEUE_H_ */

如何使用模板和多态性来模拟队列接口?

编辑:我必须改变

的事情接口:=0 添加到函数

   #ifndef QUEUEINTERFACE_H_
    #define QUEUEINTERFACE_H_
    /**
     * Java like interface
     */
    template<class E>
class QueueInterface {
public:
    QueueInterface() {
    }
    virtual bool add(E e)=0;
    virtual E element()=0;
    virtual bool offer(E e)=0;
    virtual E peek()=0;
    virtual E poll()=0;
    virtual E remove()=0;
    virtual int size()=0;
    virtual bool isEmpty()=0;
    virtual ~QueueInterface() {
    }
    ;
};
#endif /* QUEUEINTERFACE_H_ */

LinkedQueue 实现(不工作只是试图让继承工作):

#ifndef QUEUE_H_
#define QUEUE_H_
#include "QueueInterface.h"
#include "LinkedList.h"
template<class T>
class LinkedQueue: public QueueInterface<T> {
private:
    LinkedList<T> linkedList;
public:
     LinkedQueue() {
    }
    virtual bool add(T t) {
        std::cout << "entering add";
        return false;
    }
    virtual T element() {
        T t;
        return t;
    }
    virtual bool offer(T e) {
        return false;
    }
    virtual T peek() {
        T t;
        return t;
    }
    virtual T poll() {
        T t;
        return t;
    }
    virtual T remove() {
        T t;
        return t;
    }
    virtual int size() {
        return -1;
    }
    virtual bool isEmpty() {
        return false;
    }
    virtual ~LinkedQueue() {
    }
};
#endif /* QUEUE_H_ */

如果您正在从 java 过渡到 c++,请记住,在打印布尔值时,在 c++ 中产生 0 或 1,而在 java 中为 true 或 false。

您应该使用 LinkedList 成员实现队列接口中定义的操作:

#ifndef QUEUE_H_
#define QUEUE_H_
#include "QueueInterface.h"
#include "LinkedList.h"
template<class T>
class LinkedQueue: public QueueInterface<T> {
private:
    LinkedList<T> linkedList;
public:
    virtual ~LinkedQueue() {
    }

    virtual bool add(T t) {
        linkedList.push_back(t);
    }
    virtual T element() {
        return linkedList.front();
    }
    virtual size() {
        return linkedList.size();
    }
    virtual isEmpty() {
        return linkedList.empty();
    }
// etc...
// you may need to do some blocking operations
// for some of the members like poll
};

显然我没有 LinkedList 的接口,所以列表成员函数调用基于 std::list

相关文章:
  • 没有找到相关文章