469,926 Members | 1,903 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

Implementing this constructor...

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!!!

May 10 '06 #1
4 1799
pat wrote:
Hi, I have been asked for an exam question to


The FAQ covers this question:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.2
"How do I get other people to do my homework problem for me?"

Checking the FAQ before posting is always good.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
May 10 '06 #2
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.

May 10 '06 #3
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.

May 10 '06 #4
pat wrote:
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.


Sorry; my bad. Here:

http://www.google.com/search?q=%22pointer+to+pointer%22

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
May 10 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by adeleinandjeremy | last post: by
6 posts views Thread by Martyn Lawson | last post: by
6 posts views Thread by Kerry Sanders | last post: by
2 posts views Thread by sophie | last post: by
4 posts views Thread by phl | last post: by
32 posts views Thread by Abhishek Srivastava | last post: by
reply views Thread by ecestd | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.