Yurttas/PL/OOL/Cplusplus/F/03/02/03/00/01/Matrix.cpp

  1/*
  2   Copyright(C) 1998
  3   All Rights Reserved. Salih Yurttas, ZCubes, BitsOfCode Software Systems, Inc..
  4
  5   Permission to use, copy, modify, and distribute this
  6   software and its documentation for EDUCATIONAL purposes
  7   and without fee is hereby granted provided that this
  8   copyright notice appears in all copies.
  9
 10   date   : January 1, 1998.
 11   authorĀ : Salih Yurttas.
 12
 13   Matrix.cpp
 14*/
 15
 16
 17#include <iostream>
 18
 19using namespace std;
 20
 21#include "Matrix.h"
 22
 23const int r_s=4,
 24          c_s=4;
 25
 26Matrix::
 27Matrix() : r_size = r_s),
 28           c_size = c_s),
 29           m = new int*[r_size]) {
 30  for(int i=0; i<r_size; ++i) {
 31    m[i]= new int[c_size];
 32    for(int j=0; j<c_size; ++j)
 33      m[i][j] = 0;
 34  }
 35}
 36
 37Matrix::
 38Matrix(const int r, 
 39       const int c) : r_size(r),
 40                      c_size(c),
 41                      m(new int*[r_size]) {
 42  for(int i=0; i<r_size; ++i) {
 43    m[i]= new int[c_size];
 44    for(int j=0; j<c_size; ++j)
 45      m[i][j] = 0;
 46  }
 47}
 48
 49Matrix::
 50Matrix(const Matrix& m1) : r_size(m1.r_size),
 51                           c_size(m1.c_size),
 52                           m(new int*[r_size]) {
 53  for(int i=0; i<r_size; ++i) {
 54    m[i]= new int[c_size];
 55    for(int j=0; j<c_size; ++j)
 56      m[i][j] = m1.m[i][j];
 57  }
 58}
 59
 60Matrix::
 61~Matrix() {
 62  for(int i=0; i<r_size; ++i)
 63    delete [] m[i];
 64  delete [] m;
 65}
 66
 67Matrix&
 68Matrix::
 69operator=(const Matrix& m1) {
 70  for(int i=0; i<r_size; ++i)
 71    delete [] m[i];
 72  delete [] m;
 73
 74  m = new int*[r_size];
 75  for(int i=0; i<r_size; ++i) {
 76    m[i]= new int[c_size];
 77    for(int j=0; j<c_size; ++j)
 78      m[i][j] = m1.m[i][j];
 79  }
 80
 81  return *this;
 82}
 83
 84Matrix&
 85Matrix::
 86operator+(const Matrix& m1) {
 87  for(int i=0; i<r_size; ++i)
 88    for(int j=0; j<c_size; ++j)
 89      m[i][j] = m[i][j] + m1.m[i][j];
 90  return *this;
 91}
 92
 93Matrix&
 94Matrix::
 95get_matrix() {
 96  for(int i=0; i<r_size; ++i)
 97    for(int j=0; j<c_size; ++j)
 98      cin >> m[i][j];
 99  return *this;
100}
101
102void
103Matrix::
104put_matrix() {
105  for(int i=0; i<r_size; ++i) {
106    for(int j=0; j<c_size; ++j)
107      cout << m[i][j] << " ";
108    cout << endl;
109  }
110}