Files
Avancerad_CPP/Lab1/matrix.h
2026-03-05 13:16:26 +01:00

112 lines
3.5 KiB
C++

//##################################################################
// LAB 1
// ADVANCED PROGRAMMING IN C++
// MATRIX/ARRAY WITH DEEP COPY
//==================================================================
// matrix.h
// CLASS IMPLEMENTATION AND DEFINITION FOR
// THE MATRIX
// Christian Ohlsson
// Daniel Alfredsson
// Karlstad 990907
//==================================================================
#include "array.h"
class Matrix {
private:
int cols, rows; //# of cols and rows in the matrix
Array *myMatrix; //Array-pointer to our matrix
public:
void Matrix::operator = (const Matrix &src);
int getCols() const { return cols;};
int getRows() const { return rows;};
Array &Matrix::operator [] (int index) const;
Matrix(const Matrix &src);
Matrix(int cols, int rows);
~Matrix();
};
//################################################################
// Constructor
// Creates a new Matrix with 'c' columns and 'r' rows
//================================================================
Matrix::Matrix(int c, int r) {
myMatrix = new Array[r];
for(int i=0; i < c; i++)
myMatrix[i] = Array(c);
cols = c;
rows = r;
}
//################################################################
// Constructor
// Creates a new copy of a Matrix
//================================================================
Matrix::Matrix(const Matrix &src) {
cols = src.cols;
rows = src.rows;
int i,j;
myMatrix = new Array[rows];
for(i=0; i < cols; i++)
myMatrix[i] = Array(cols);
for(i=0 ; i < cols ; i++)
for(j=0 ; j < rows ; j++)
myMatrix[i][j] = src.myMatrix[i][j];
}
//################################################################
// Destructor
// De-allocates memory for a Matrix
//================================================================
Matrix::~Matrix() {
delete [] myMatrix;
}
//################################################################
// Operator overload
// Sets one Matrix equal to another
//================================================================
void Matrix::operator = (const Matrix &src) {
delete [] myMatrix;
cols = src.cols;
rows = src.rows;
myMatrix = new Array[rows];
for(int i=0 ; i<cols ; i++)
for(int j=0 ; j<rows ; j++)
myMatrix[i][j] = src.myMatrix[i][j];
}//################################################################
// Operator overload
// Reyurns a row in a Matrix, and checks for bad indexes.
//================================================================
Array &Matrix::operator [] (int index) const {
if(index>=rows || index<0) {
cout <<"\nIndex out of bounds! Quitting...\n";
exit(0);
}
else
return myMatrix[index];
}
//################################################################
// Operator overload
// Allow inserts in a Matrix, using STDIN
//================================================================
istream &operator >> (istream &is, Matrix &v) {
int i,j;
for(i=0 ; i<v.getCols() ; i++)
for (j=0 ; j<v.getRows() ; j++)
is >> v[i][j];
return is;
}
//################################################################
// Operator overload
// Prints the Matrix on the screen, using STDUOT
//================================================================
ostream &operator << (ostream &os, const Matrix &v) {
int i,j;
for(i=0 ; i < v.getCols() ; i++)
for(j=0 ; j < v.getRows() ; j++)
os << i << "," << j<< " = " << v[i][j] << "\n";
return os;
}