SHARE
TWEET

class Matrix

sonprao Oct 21st, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class Matrix{
  4.     public:
  5.         Matrix();
  6.         Matrix(int,int,int);
  7.         ~Matrix();
  8.         friend istream& operator>>(istream &in, Matrix& M);
  9.         friend ostream& operator<<(ostream &out, Matrix &M);
  10.     private:
  11.         int n;
  12.         int m;
  13.         int **Data;
  14. };
  15. Matrix::Matrix()
  16. {
  17.     this->n=0;
  18.     this->m=0;
  19. }
  20. Matrix::Matrix(int n,int m, int val)
  21. {
  22.     this->n=n;
  23.     this->m=m;
  24.     this->Data=new int* [n];
  25.     for(int i=0;i<n; i++)
  26.         this->Data[i]=new int [n];
  27.     for(int i=0;i<n;i++)
  28.     for(int j=0;j<m;j++)
  29.     this->Data[i][j]=val;
  30. }
  31. Matrix::~Matrix()
  32. {
  33.     for(int i=0;i<n;i++)
  34.     delete[] this->Data[i];
  35.     delete this->Data;
  36. }
  37. istream& operator >> (istream &in, Matrix& M)
  38. {  
  39.     int i,j;
  40.     in>>M.n;
  41.     in>>M.m;
  42.     M.Data=new int* [M.n];
  43.     for(i=0; i<=M.n-1;i++) M.Data[i]=new int [M.m];
  44.     // Cấp phát
  45.     for(i=0;i<=M.n-1;i++)
  46.     for(j=0;j<=M.m-1;j++)
  47.         in>>M.Data[i][j];
  48.     return in;
  49. }
  50. ostream& operator<<(ostream &out, Matrix &M)
  51. {  
  52.     for(int i=0;i<=M.n-1;i++)
  53.     {for(int j=0;j<=M.m-1;j++)
  54.         out<<M.Data[i][j]<<" ";
  55.         out<<endl;}
  56. }
  57. int main(){
  58.     Matrix M();
  59.     cin>>M;
  60.     cout<<M;
  61.     return 0;
  62. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top