pat wrote:
Hi, I have been asked for an exam question to implement the
constructor, copy constructor and destructor for the following class
that describes an n-by-n matrix containing n squared integer values.
(Eg. a matrix of n rows and n colums):
import <iostream.h>
There is no keyword "import" in C++, and iostream.h is an old pre-standard
header that shouldn't be used anymore.
class myMatrix
{
private:
int **data; //pointer to array of n pointers, which in turn
point to n
arrays that contain matrix data
int size; //size of matrix
public:
//contructor, matrix elements are initialized with defaultValue
myMatrix(int matrixSize, int defaultValue);
//copy constructor
myMatrix(const myMatrix &v);
//destructor
~myMatrix
();
};
I'm a bit lost on what "int **data" is.
This is a pointer to a pointer to int. The comment explains quite accurately
what it's used for in this case.
How do you actually use this to create the constructor?
First, dynamically allocate an array of n pointers to int. Then, for each of
them, allocate an array of n int.
I didn't even know you could declare something like that.
You can declare pointers to any type, including pointers.
.