MeehoweCK

Untitled

May 29th, 2024
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class Plansza {
  4.     int size{ 5 };
  5.     int* tab{ new int[5] };
  6. public:
  7.     Plansza() {
  8.         for (int i{}; i < size; ++i) {
  9.             tab[i] = i;
  10.         }
  11.     }
  12.     ~Plansza() { if (tab) delete[] tab; }
  13.     void print() const {
  14.         for (auto i{ 0 }; i < size; ++i) {
  15.             std::cout << tab[i] << '\t';
  16.         }
  17.         std::cout << std::endl;
  18.     }
  19.     void dodajElement(int nowy) {
  20.         if (size == 0) {
  21.             tab = new int[1];
  22.             tab[0] = nowy;
  23.         }
  24.         else {
  25.             int* temp{ new int[size + 1] };
  26.             for (auto i{ 0 }; i < size; ++i) {
  27.                 temp[i] = tab[i];
  28.             }
  29.             delete[] tab;
  30.             temp[size] = nowy;
  31.             ++size;
  32.             tab = temp;
  33.         }
  34.     }
  35. };
  36.  
  37. int main() {
  38.     Plansza p;
  39.     p.print();
  40.     p.dodajElement(10);
  41.     p.print();
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment