Advertisement
soulrpg

PO_kolos_v2

Nov 28th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <exception>
  3.  
  4. using namespace std;
  5.  
  6. // to tylko do testow
  7. class EndOfList: public exception
  8. {
  9. public:
  10. virtual const char* what()
  11. {
  12. return "EndOfList exception happened!";
  13. }
  14. };
  15.  
  16. template <class T>
  17. class ElementListy
  18. {
  19. public:
  20. virtual void setnext(ElementListy<T>& el) = 0;
  21. virtual ElementListy* getnext() = 0;
  22. virtual T getvalue() = 0;
  23. };
  24.  
  25. template <class T>
  26. class MyElement: public ElementListy<T>
  27. {
  28. protected:
  29. const T value;
  30. MyElement* next;
  31. public:
  32. MyElement(T value)
  33. {
  34. this->value = value;
  35. }
  36. void setnext(MyElement<T>& el)
  37. {
  38. next = &el;
  39. }
  40. MyElement* getnext()
  41. {
  42. return next;
  43. }
  44. T getvalue()
  45. {
  46. return value;
  47. }
  48.  
  49. };
  50.  
  51. template <class X>
  52. class Lista
  53. {
  54. protected:
  55. MyElement<X>& first;
  56. MyElement<X>& last;
  57. MyElement<X>*& current;
  58. public:
  59. Lista(MyElement<X>& mel)
  60. {
  61. first = &mel;
  62. }
  63. void addElement(MyElement<X>& newelem)
  64. {
  65. last = &newelem;
  66. }
  67. X getFirstValue()
  68. {
  69. return first->getvalue();
  70. }
  71. X getNextValue()
  72. {
  73. if(current == last)
  74. {
  75. EndOfList eol;
  76. throw eol;
  77. }
  78. return current->next->getvalue();
  79. }
  80. };
  81.  
  82. int main()
  83. {
  84. try
  85. {
  86. Lista<string> lista(MyElement<string>("Ala ma kota");
  87. lista.addElement(MyElement<string>("Jarek ma psa"));
  88. ElementListy odczyt = lista.getFirstValue();
  89. }
  90. catch(EndOfList &e)
  91. {
  92. cout << "Blad! Poza zakresem!" << endl;
  93. }
  94.  
  95. cin.sync();
  96. cin.get();
  97. return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement