Implementing this constructor...

P

pat

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>


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. How do you actually use this to

create the constructor? I didn't even know you could declare something
like that. Hopefully someone can sort me out here??? Thanks!!!
 
P

pat

Once again I feel the need to defend the fact that if you read my post
you will see that I am asking about what the "int **dat" declaration
means? I am putting it in context not asking soneone to do the
question.

It is a past exam question, not a homework question and I will gain no
marks from doing it. I just want to know what to do if something
similar is asked again.

The difference between me and the 1000's of other people posting here
is that I have been honest and said that I am using the information for
a question at college.

If you don't want to help thats fine but I don't see the point in
disencouraging other people from doing so.
 
R

Rolf Magnus

pat said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top