An ARR3D class implemented as a pointer to a pointer pointer

KBleivik
Site Admin
Posts: 88
Joined: Tue Jan 31, 2006 3:10 pm
Location: Moss Norway
Contact:

An ARR3D class implemented as a pointer to a pointer pointer

Postby KBleivik » Fri Sep 22, 2006 4:55 pm

/*

The Matrix class is inspired by Bjarne Stroustrup "The C++ programming Language
Second Edition" Addison-Wesley Publishing Company 1992 page 236, Ira Pohl
"Objectoriented programming in C++" Benjamin/Cummings Publishing Company, Inc.
1993 page 256 -> and Andrew Koenig "Multidimensional arrays and new-expressions"
C++ Report February 1994 page 52.

*/
#include <iostream.h>
#include <assert.h>

enum boolean{false, true};

template <class Type> class Stack;
template <class Type> class Iterator;
template <class Type> class Arr3d
{
private:
int dim1, dim2, dim3;
Type ***p; // Pointer to pointer to pointer.
friend class Iterator<Type>;
friend class Stack<Type>;
public:
Arr3d(){ // Default constructor.
dim1=dim2=dim3; // 3x3x3 Identity Array is default.
p={{1,0,0},{0,1,0},{0,0,1}};
}
Arr3d(int d1, int d2, int d3); //Parameterized constructor type 1.
Arr3d(const Type a[], int d1, int d2,int d3); //Parameterized constructor type 2. Array
//initialization.
Arr3d(const Arr3d& m); // Copy constructor
~Arr3d(); // Destructor.
Arr3d& operator()(int i1, int i2, int i3) { return(p[i1][i2][i3]);}
};
/*
Matrix& operator=(const Matrix& m);
Matrix& operator+=(const Matrix& m);
Matrix& operator*=(const Matrix& m);
friend Matrix operator+(const Matrix&, const Matrix&);
friend Matrix operator*(const Matrix&, const Matrix&);
};

template<class Type>
Matrix<Type>::Matrix(int c, int r) : cSize(c), rSize(r)
{
p = new Type*[c]; // Array of pointers to double
for (int i = 0; i < c; ++i)
p[i] = new double[r];
}
template<class Type>
Matrix<Type>::~Matrix()
{
for (int i = 0; i < cSize; ++i)
delete [] p[i];
delete [] p;
}

template<class Type>
Matrix<Type>& Matrix<Type>::operator=(const Matrix<Type>& m)
{
assert(m.cSize == cSize && m.rSize == rSize);
int i, j;
for (i = 0; i < cSize; ++i)
for (j = 0; j < rSize; ++j)
p[i][j] = m.p[i][j];
return (*this); // returns the actual total object.
}

template<class Type>
Matrix<Type>& Matrix<Type>::operator+=(Matrix<Type>& m)
{
assert(m.cSize == cSize && m.rSize == rSize);
int i, j;
for (i = 0; i < cSize; ++i)
for (j = 0; j < rSize; ++j)
p[i][j] += m.p[i][j];
return (*this); // returns the actual total object.
}
template<class Type>
Matrix<Type>& Matrix<Type>::operator*=(Matrix<Type>& m)
{
assert(m.cSize == cSize && m.rSize == rSize);
int i, j;
for (i = 0; i < cSize; ++i)
for (j = 0; j < rSize; ++j)
p[i][j] *= m.p[i][j];
return (*this); // returns the actual total object.
}
*/
Kjell Gunnar Bleivik
Make it simple, as simple as possible but no simpler: | DigitalPunkt.no |

Return to “C++”

Who is online

Users browsing this forum: No registered users and 2 guests