Advertisement
MeehoweCK

Untitled

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