m4ly

Macierz. Matrix

Feb 1st, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. /*
  2. Autor: Dawid Mocek
  3.  
  4.  
  5. */
  6. #include <string>
  7. #include <iostream>
  8.  
  9. using namespace std;
  10.  
  11. class Macierz
  12. {
  13.     int **matrix;
  14.     int x;
  15.     int y;
  16.  
  17. public:
  18.     Macierz();
  19.     Macierz(int x, int y);
  20.     ~Macierz();
  21.     friend ostream & operator<<(ostream &out, const Macierz& ma);
  22.     Macierz& operator= (const Macierz&);
  23.  
  24. };
  25.  
  26. Macierz::Macierz() :x(0), y(0), matrix(nullptr)
  27. {
  28.    
  29. }
  30.  
  31. Macierz::Macierz(int x, int y) : x(x), y(y)
  32. {
  33.     matrix = new int*[x];
  34.     for (int c=0; c < x; c++)
  35.         matrix[c] = new int[y];
  36.  
  37. }
  38.  
  39. Macierz::~Macierz()
  40. {
  41.     if (matrix == nullptr) return;
  42.  
  43.     for (int c=0; c < x; c++)
  44.         delete[] matrix[c];
  45.     delete[] matrix;
  46.  
  47.     matrix = nullptr;
  48. }
  49. ostream & operator<<(ostream &out, const Macierz& ma)
  50. {
  51.    
  52.     for (int i = 0; i < ma.x; i++)
  53.     for (int j = 0; j < ma.y; j++)
  54.         out << ma.matrix[i][j] << " ";
  55.  
  56.     return out;
  57. }
  58. Macierz& Macierz::operator= (const Macierz& org)
  59. {
  60.     // ten sam adres pamieci
  61.     matrix = org.matrix;
  62.     x = org.x;
  63.     y = org.y;
  64.  
  65.     return *this;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment