Advertisement
MeehoweCK

Untitled

May 11th, 2024
755
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. class FigureTab
  2. {
  3.     int dlugosc;
  4.     Figure **tab;
  5. public:
  6.     FigureTab(int dl=0) : dlugosc(dl)  //konstruktor
  7.     {
  8.         if (dlugosc>0)
  9.         {
  10.             tab = new Figure *[dlugosc] { nullptr };
  11.         }
  12.         else
  13.         {
  14.             dlugosc=0;
  15.             tab=nullptr;
  16.         }
  17.     }
  18.  
  19.     ~FigureTab()
  20.     {
  21.         cout << endl << "destruktor FigureTab zadzialal " << endl;
  22.         for (int i=0; i<dlugosc; i++)
  23.         {
  24.             delete tab[i];
  25.         }
  26.         delete[] tab;
  27.     }
  28.  
  29.     void dodajFigure(Figure* nowa);
  30.     void usunWszystko();
  31.     Figure*& operator[](int index) { return tab[index - 1]; }
  32. };
  33.  
  34. void FigureTab::dodajFigure(Figure* nowa) {
  35.     if (tab == nullptr) {   // tablica nie istnieje
  36.         tab = new Figure * [1];
  37.         tab[0] = nowa;
  38.     }
  39.     else {
  40.         // tymczasowa tablica do przechowania dotychczasowej
  41.         Figure** temp = new Figure * [dlugosc + 1];
  42.         // przepisanie elementów z dotychczasowej tablicy do tymczasowo stworzonej
  43.         for (int i{ 0 }; i < dlugosc; ++i) {
  44.             temp[i] = tab[i];
  45.         }
  46.         delete[] tab;
  47.         temp[dlugosc] = nowa;
  48.         tab = temp;
  49.     }
  50.     ++dlugosc;
  51. }
  52.  
  53. void FigureTab::usunWszystko() {
  54.     for (int i{}; i < dlugosc; ++i) {
  55.         delete tab[i];
  56.     }
  57.     delete[] tab;
  58.     tab = nullptr;
  59.     dlugosc = 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement