Hey all,

I'm currently working on a project that exemplifies matrix multiplication algorithms. My problem isn't working on the algorithms, but more of the structure of the classes. If any of you wouldn't mind looking over my code and helping me get a little more familiar with how things should be implemented, that'd be great!

Here's my first piece of code, MatrixBaseClass.h

``````#ifndef _BASECLASS
#define _BASECLASS

#include <utility>
using namespace std;

template <typename T>
class MatrixBaseClass{
protected:
int row, col;
T** data;

public:
virtual T* operator[] (int)            = 0;
virtual pair<int, int> getDimensions() = 0;
};

#endif``````

It is fairly straight forward and shouldn't change at all.

The second bit of code is 'Matrix.h', where matrix should be implemented right? like getElement(r,c), setElement, constructors, etc. does this look right to you lined up with MatrixBaseClass.h?

``````#ifndef MATRIX_H
#define MATRIX_H
#include "MatrixBaseClass.h"

using namespace std;

class Matrix
{
private:
int rows;
int cols;
int **data;

public:
Matrix();
Matrix(int r, int c);

int getElement(int r, int c);
void setElement(int r, int c, int v) const;
int* operator[](int);
};

Matrix::Matrix(){
this->rows =3;
this->cols =3;
this->data =new int*[this->rows];
for(int i=0; i<this->rows; i++)
{
this->data[i]=new int[this->cols];
}

for(int r=0; r<this->rows; r++)
{
for(int c=0; c<this->cols; c++)
{
this->data[r][c]=0;
}
}
}

Matrix::Matrix(int r, int c)
{
this->rows =r;
this->cols =c;
this->data = new int*[this->rows];
for(int i=0; i<this->rows; i++)
{
this->data[i] = new int[this->cols];
}

for(int r=0; r<this->rows; r++)
{
for(int c=0; c<this->cols; c++)
{
this->data[r][c]=0;
}
}
}

int Matrix::getElement(int r, int c)
{
if(r>this->rows || c>this->rows)
{
return -1;
}
return this->data[r][c];
}

void Matrix::setElement(int r, int c, int v) const
{
this->data[r][c]=v;
}

int* Matrix::operator [](int i)
{
return this->data[i];

#endif``````

Finally, the matrix algorithm header file. Now, I'm not asking any guidence on implementation of the algorithm, but more of just how to call the matrix base class from it. i.e. on the brute force algorithm, how would I go about calling m1? just m1.getElement(r,c,)? or what about when it has to deal with MatrixBaseClass->data ? Since that is what is templated.

``````#ifndef MATRIXMULTALGORITHM_H
#define MATRIXMULTALGORITHM_H
#include "MatrixBaseClass.h"

using namespace std;

MatrixBaseClass<T> * BF_MatrixMult(MatrixBaseClass<T>* m1, MatrixBaseClass<T>* m2)
{

}

MatrixBaseClass<T> * DC_MatrixMult(MatrixBaseClass<T>*, MatrixBaseClass<T>*)
{

}

MatrixBaseClass<T> *SA_MatrixMult(MatrixBaseClass<T>*, MatrixBaseClass<T>*)
{

}

MatrixBaseClass<T> *TDC_MatrixMult(MatrixBaseClass<T>*,MatrixBaseClass<T>*)
{
}

#endif``````

Sorry if my post is kind of destructive on what I'm looking for here, because I'm not really sure how to explain it, but I guess the real question is everything in line to start working on the algorithms, and if so, is there any differences in calling since it is templated?

Thanks for all and any help!

Unless this is a specific assignment you don't really need an abstract class from which to derive a matrix class.

## All 3 Replies

Did some editing and figured some stuff out.. I think.. any suggestions?

``````#ifndef MATRIX_H
#define MATRIX_H
#include "MatrixBaseClass.h"

using namespace std;

template <typename T>
class Matrix : public MatrixBaseClass
{
private:
int row;
int col;
T** data;

public:
Matrix();
Matrix(int r, int c);

int getElement(int r, int c);
void setElement(int r, int c, int v) const;
T* operator[] (int);
pair<int, int> getDimensions();

};

template<typename T>
Matrix::Matrix(){
row =3;
col =3;
data =new T*[row];
for(int i=0; i<row; i++)
{
data[i]=new int[col];
}

for(int r=0; r<row; r++)
{
for(int c=0; c<col; c++)
{
data[r][c]=0;
}
}
}

template<typename T>
Matrix::Matrix(int r, int c)
{
row =r;
col =c;
data = new T*[row];
for(int i=0; i<row; i++)
{
data[i] = new int[col];
}

for(int r=0; r<row; r++)
{
for(int c=0; c<col; c++)
{
data[r][c]=0;
}
}
}

int Matrix::getElement(int r, int c)
{
if(r>row || c>row)
{
return -1;
}
return data[r][c];
}

void Matrix::setElement(int r, int c, int v) const
{
data[r][c]=v;
}

template <typename T>
T* Matrix::operator [](int i)
{
return data[i];
}
#endif``````
``````#ifndef _BASECLASS
#define _BASECLASS

#include <utility>
using namespace std;

template <typename T>
class MatrixBaseClass{
protected:
int row, col;
T** data;

public:
virtual T* operator[] (int)            = 0;
virtual pair<int, int> getDimensions() = 0;
};

#endif``````

Unless this is a specific assignment you don't really need an abstract class from which to derive a matrix class.

It's a specific assignment :/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.