MeehoweCK

Untitled

Mar 22nd, 2021
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class WektorInt
  6. {
  7.     int* tablica;
  8.     unsigned rozmiar;
  9. public:
  10.     WektorInt();
  11.     explicit WektorInt(unsigned);
  12.     WektorInt(unsigned, int);
  13.     ~WektorInt();
  14.     int get_value(unsigned);
  15.     void set_value(unsigned, int);
  16.     unsigned size();
  17. };
  18.  
  19. WektorInt::WektorInt() : tablica(nullptr), rozmiar(0)
  20. {
  21.     // tworzymy pusty wektor
  22. }
  23.  
  24. WektorInt::WektorInt(unsigned n) : rozmiar(n), tablica(new int[n])
  25. {
  26.     // tworzymy wektor o podanym rozmiarze
  27. }
  28.  
  29. WektorInt::WektorInt(unsigned n, int wartosc) : rozmiar(n), tablica(new int[n])
  30. {
  31.     for(unsigned i = 0; i < n; ++i)
  32.         tablica[i] = wartosc;
  33. }
  34.  
  35. WektorInt::~WektorInt()
  36. {
  37.     if(tablica)
  38.         delete[] tablica;
  39.     cout << "Wektor zostal usuniety\n";
  40. }
  41.  
  42. int WektorInt::get_value(unsigned pozycja)
  43. {
  44.     if(pozycja < rozmiar)
  45.         return tablica[pozycja];
  46.     return 0;
  47. }
  48.  
  49. void WektorInt::set_value(unsigned pozycja, int wartosc)
  50. {
  51.     if(pozycja < rozmiar)
  52.         tablica[pozycja] = wartosc;
  53. }
  54.  
  55. unsigned WektorInt::size()
  56. {
  57.     return rozmiar;
  58. }
  59.  
  60. int main()
  61. {
  62.     WektorInt wektor(6, 3);
  63.     wektor.set_value(2, 10);
  64.     cout << wektor.get_value(2) << endl;
  65.     cout << wektor.size() << endl;
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment