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