Code:
//main
#include <iostream>
#include "Table.h"
using namespace std;
int main()
{
Table<int> array1 (2,3);
array1 (0,0) = 1;
array1 (0,1) = 2;
array1 (0,2) = 3;
array1 (1,0) = 4;
array1 (1,1) = 5;
array1 (1,2) = 6;
print (array1, 2,3);
Table<int> array2 (2,3);
array2 (0,0) = 7;
array2 (0,1) = 8;
array2 (0,2) = 9;
array2 (1,0) = 10;
array2 (1,1) = 11;
array2 (1,2) = 12;
print (array2, 2,3);
Table<int> array3 = array1 + array2;
print (array3, 2,3);
}
Code:
//Table.h
#ifndef TABLE_H
#define TABLE_H
template<typename T>
class Table
{
public:
Table();
Table(int m, int n);
Table(int m, int n, const T& value);
Table(const Table<T>& rhs);
~Table();
Table<T>& operator = (const Table& rhs);
Table<T> operator + (const Table& rhs);
T& operator()(int i, int j);
int numRows()const;
int numCols()const;
void resize(int m, int n);
void resize(int m, int n, const T& value);
void print(Table<T>& data, int x, int y);
private:
void destroy();
private:
int mNumRows;
int mNumCols;
T** mDataMatrix;
};
template <typename T>
Table<T>::Table()
{
mDataMatrix = 0;
mNumRows = 0;
mNumCols = 0;
}
template <typename T>
Table<T>::Table(int m, int n)
{
mDataMatrix = 0;
mNumRows = 0;
mNumCols = 0;
resize(m, n, T());
}
template <typename T>
Table<T>::Table(int m, int n, const T& value)
{
mDataMatrix = 0;
mNumRows = 0;
mNumCols = 0;
resize(m, n, value);
}
template <typename T>
Table<T>::Table(const Table<T>& rhs)
{
mDataMatrix = 0;
mNumRows = 0;
mNumCols = 0;
*this = rhs;
}
template <typename T>
Table<T>::~Table()
{
//Destroy any previous memory
destroy();
}
template <typename T>
Table<T>& Table<T>::operator=(const Table& rhs)
{
//Check for self assessment
if( this == &rhs ) return *this;
resize(rhs.mNumRows, rhs.mNumCols);
for(int i = 0; i < mNumRows; ++i)
for(int j = 0; j < mNumCols; ++j)
mDataMatrix[i][j] = rhs.mDataMatrix[i][j];
return *this;
}
template <typename T>
Table<T> Table<T>::operator + (const Table &rhs)
{
Table<T> sum;
for(int i = 0; i < mNumRows; ++i)
for(int j = 0; j < mNumCols; ++j)
{
sum.mDataMatrix[i][j] = mDataMatrix[i][j] + rhs.mDataMatrix[i][j];
}
return sum;
}
template <typename T>
T& Table<T>::operator()(int i, int j)
{
return mDataMatrix[i][j]; //Return the ijth table entry
}
template <typename T>
int Table<T>::numRows()const
{
return mNumRows; //Return number of rows
}
template <typename T>
int Table<T>::numCols()const
{
return mNumCols; //Return the number of cols
}
template <typename T>
void Table<T>::resize(int m, int n)
{
resize(m, n, T()); //Call resize and use default constructor T()
//as a value
}
template <typename T>
void Table<T>::resize(int m, int n, const T& value)
{
//destroy previous data
destroy();
//Save dimensions
mNumRows = m;
mNumCols = n;
//Allocate a row(array) of pointers
mDataMatrix = new T*[mNumRows];
//Loop through each pointer in this row array
for(int i = 0; i < mNumRows; ++i)
{
//Allocate a col for the ith row to build the table
mDataMatrix[i] = new T[mNumCols];
//Loop through each element in this row[i] and copy value into it
for(int j = 0; j < mNumCols; ++j)
mDataMatrix[i][j] = value;
}
}
template <typename T>
void Table<T>::destroy()
{
//Does the matrix exist
if(mDataMatrix)
{
//Iterate over each row i
for(int i = 0; i < mNumRows; ++i)
{
//Does the ith col array exist
if(mDataMatrix[i])
{
//Yes delete it
delete[]mDataMatrix[i];
mDataMatrix[i] = 0;
}
}
//Now delete the row array
delete[] mDataMatrix;
mDataMatrix = 0;
}
//Table destroyed so dimensions are zero
mNumRows = 0;
mNumCols = 0;
}
template <typename T>
void print(Table<T>& data, int x, int y)
{
for(int i = 0; i < x; ++i)
{
cout << "{";
for(int j = 0; j < y; ++j)
cout << data(i, j) << " ";
cout << "}" << endl;
}
}
#endif
