fork(1) download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. template <typename T>
  5. class Iterator {
  6. public:
  7. virtual ~Iterator(){};
  8. virtual bool hasNext() = 0;
  9. virtual T *next() = 0;
  10. };
  11.  
  12. template <typename T>
  13. class Aggregate {
  14. public:
  15. virtual ~Aggregate(){};
  16. virtual Iterator<T> *iterator() = 0;
  17. };
  18.  
  19. class Book {
  20. private:
  21. std::string name;
  22. public:
  23. Book(){};
  24. Book(std::string name);
  25. ~Book(){};
  26. std::string getName();
  27. friend std::ostream &operator<<(std::ostream &s, Book *b);
  28. };
  29.  
  30. template <typename T>
  31. class TShelf : Aggregate<T> {
  32. private:
  33. T **t_s;
  34. int maxsize;
  35. int last;
  36. public:
  37. TShelf(int maxsize);
  38. ~TShelf<T>();
  39. T *getAt(int index);
  40. void append(T *book);
  41. int getLength();
  42. Iterator<T> *iterator();
  43. };
  44.  
  45. template <typename T>
  46. class TShelfIterator : public Iterator<T> {
  47. private:
  48. TShelf<T> *tShelf;
  49. int index;
  50. public:
  51. TShelfIterator(TShelf<T> *t_shelf);
  52. ~TShelfIterator<T>(){};
  53. bool hasNext();
  54. T *next();
  55. };
  56. /*-----------------------------------------------*/
  57. Book::Book(std::string name) { this->name = name; }
  58. std::string Book::getName() {return this->name; }
  59. std::ostream &operator<<(std::ostream &s, Book *b) {
  60. s << b->name;
  61. return s;
  62. }
  63. /*-----------------------------------------------*/
  64. template <typename T> TShelf<T>::TShelf(int maxsize) {
  65. this->t_s = new T* [maxsize];
  66. this->maxsize = maxsize;
  67. this->last = 0;
  68. }
  69. template <typename T> TShelf<T>::~TShelf<T>() { delete [] (this->t_s); }
  70. template <typename T> T *TShelf<T>::getAt(int index) { return this->t_s[index]; }
  71. template <typename T> void TShelf<T>::append(T *t) {
  72. if (last < maxsize) {
  73. this->t_s[last] = t;
  74. this->last++;
  75. }
  76. }
  77. template <typename T> int TShelf<T>::getLength() { return this->last; }
  78. template <typename T> Iterator<T> *TShelf<T>::iterator() { return new TShelfIterator<T>(this); }
  79.  
  80. /*-----------------------------------------------*/
  81. template <typename T> TShelfIterator<T>::TShelfIterator(TShelf<T> *tShelf) {
  82. this->tShelf = tShelf;
  83. this->index = 0;
  84. }
  85. template <typename T> bool TShelfIterator<T>::hasNext() {
  86. if (this->index < this->tShelf->getLength()) return true;
  87. else return false;
  88. }
  89. template <typename T> T *TShelfIterator<T>::next() {
  90. T *t = this->tShelf->getAt(index);
  91. index++;
  92. return t;
  93. }
  94.  
  95. int main() {
  96. TShelf<Book> *tShelf = new TShelf<Book>(4);
  97. tShelf->append(new Book("A"));
  98. tShelf->append(new Book("BB"));
  99. tShelf->append(new Book("CCC"));
  100. tShelf->append(new Book("DDDD"));
  101. Iterator<Book> *it = tShelf->iterator();
  102. while (it->hasNext()) {
  103. Book *book = it->next();
  104. std::cout << book << std::endl;
  105. }
  106. delete it;
  107. delete tShelf;
  108. return 0;
  109. }
  110. /* end */
  111.  
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
A
BB
CCC
DDDD