Advertisement
soulrpg

po_kolos

Nov 28th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 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.     void setnext() = 0;
  21.     ElementListy* next() = 0;
  22.     T getvalue() = 0;
  23. };
  24.  
  25. template <class T>
  26. class MyElement: public ElementListy
  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 el)
  37.     {
  38.         next = &el;
  39.     }
  40.     MyElement* next()
  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* first;
  56.     MyElement* last;
  57.     MyElement* current;
  58. public:
  59.     Lista(MyElement mel)
  60.     {
  61.         first = &mel;
  62.     }
  63.     void addElement(MyElement 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;
  87.         lista = Lista(MyElement("Ala ma kota"));
  88.         lista.addElement(MyElement("Jarek ma psa"));
  89.         ElementListy odczyt = lista.getFirstValue();
  90.     }
  91.     catch(EndOfList &e)
  92.     {
  93.         cout << "Blad! Poza zakresem!" << endl;
  94.     }
  95.  
  96.     cin.sync();
  97.     cin.get();
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement