Implementing this constructor...



 DEVELOP > c-Plus-Plus > Implementing this constructor...

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "pat"
Date: 09 May 2006 05:58:10 AM
Object: Implementing this constructor...
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!!!
.

User: "Phlip"

Title: Re: Implementing this constructor... 09 May 2006 06:04:09 AM
pat wrote:

Hi, I have been asked for an exam question to

The FAQ covers this question:
http://www.parashift.com/c++-faq-lite/how-to-post.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!!!
.
User: "pat"

Title: Re: Implementing this constructor... 09 May 2006 06:31:12 AM
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.
.
User: "Phlip"

Title: Re: Implementing this constructor... 09 May 2006 06:55:57 AM
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!!!
.



User: "Rolf Magnus"

Title: Re: Implementing this constructor... 09 May 2006 06:39:36 AM
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.
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER