# Need help with exam review question- class with pointers

Here is the problem:  (I think the output is 20, 30, 40  and 20, 30, 40, but I am not sure about pointb.)
Given the following class:
class pointtype {
private:
int *x, int *y, int *z;
public:
pointtype(int, int, int);
~pointtype();
void setpoint(int, int, int);
void getpoints(int *, int *, int *)
void print() const; // a function which prints the values of x and y
};

And the following declaration and c++ statements:

pointtype pointa(12, 16, 20), pointb(0,0, 0);
pointb = pointa;
pointa.setpoint(20, 30, 40 );
pointa.print();
pointb.print();

What is the output?
###### Who is Participating?

x

Commented:
>> lead me to believe the answer is 20, 30, 40 for pointa and 12, 16, 20 for pointb.

That is wrong.

Look at that:

pointa:  { 0x00001230, 0x00001240, 0x00001250 };

pointa.x = 0x00001230:  { 12 }
pointa.y = 0x00001240:  { 16 }
pointa.z = 0x00001250:  { 20 }

After pointb = pointa; you have (because of binary copy)

pointb:  { 0x00001230, 0x00001240, 0x00001250 };

pointb.x = 0x00001230:  { 12 }
pointb.y = 0x00001240:  { 16 }
pointb.z = 0x00001250:  { 20 }

After pointa.setpoint(20, 30, 40) you have that:

pointa.x = 0x00001230:  { 20 }
pointa.y = 0x00001240:  { 30 }
pointa.z = 0x00001250:  { 40 }

and (because the pointers x, y, z didn't change)

pointb.x = 0x00001230:  { 20 }
pointb.y = 0x00001240:  { 30 }
pointb.z = 0x00001250:  { 40 }

Regards, Alex

0

Software ArchitectCommented:
It is hard to figure out without full class implementation code.
But I don't see the reason for private member to be:
int *x, int *y, int *z;
int x, int y, int z;
0

Software ArchitectCommented:
There is a syntax error in this line:
int *x, int *y, int *z;
must be:
int *x,  *y,  *z;

0

Software ArchitectCommented:
I think I understand what you are trying to do.
pointb is copied from pointa, but when you assign new values to pointa, values won't be automatically copied to pointb unless you use a pointer.

pointtype pointa(12, 16, 20);
pointtype *pointb;
*pointb = pointa;
pointa.setpoint(20, 30, 40 );
pointa.print();
pointb->print();

assuming an implementation similar to this:

class pointtype
{
private:
int x, y, z;
public:
pointtype(int a, int b, int c):x(a),y(b),z(c) {};
~pointtype() {};
void setpoint(int a, int b, int c) {
x = a;
y = b;
z = c;
};
void getpoints(int *, int *, int *) {};
void print() { cout << x << y << z <<endl;}; // a function which prints the values of x and y
};
0

Software ArchitectCommented:
Now I have to go to sleep, good luck,
Jaime.
0

Author Commented:
Thanks Jaime, but I didn't write this code.  It was given to us by the instructor as a review for an exam, and we are asked to say what the output is the way it is written.  Your comment about the values won't be copied automatically to point b without a pointer lead me to believe the answer is 20, 30, 40 for pointa and 12, 16, 20 for pointb.
0

Commented:
This is clearly a homework question.

It's against EE policy for Experts to do homework questions.
0

Commented:
Have a think about deep copy vs shallow copy. Ask yourself what sort of copy you'd expect to get when using a compiler-generated assignment operator.
0

Software ArchitectCommented:
> From Jaime:
> pointb is copied from pointa, but when you assign new values to pointa, values won't be automatically copied to pointb
> unless you use a pointer.

coririzzo:
I think I have explained earlier (not as detailed as Alex) that both object won't contain the same. So I think I deserve some credits.
Jaime.
0

Commented:
>>>> that both object won't contain the same

Jaime, the objects contain the same. That is different to the explanation you gave as you posted a class definition with int x, y, z and the question was about int pointers *x, *y, *z. So, your answer was correct, but wasn't an answer to the question.

Regards, Alex
0

Commented:
Based on Comment http:#12214435 this question clearly falls under the guidelines as homework.
It will be deleted in 24 hours.

coririzzo and participating Experts:
http://www.experts-exchange.com/Community_Support/New_Topics/help.jsp#hi56

The accepted answer has been vacated and the question PAQ'd pending delete.

ee_ai_construct
0

Software ArchitectCommented:
Sorry, I think we was not DOING  a homework, there are many EXPLANATIONS about the use of pointers and copy constructors. So let me disagree with you (and Axter). But you are the moderator.....

I WOULDN'T CONSIDER THIS AN **ANSWER**
>But I don't see the reason for private member to be:
>          int *x, int *y, int *z;
>          int x, int y, int z;

THIS IS NOT DIRECTLY RELATED TO SPECIFIC QUESTION. JUST ADVICING:
>There is a syntax error in this line:
>          int *x, int *y, int *z;
>must be:
>          int *x,  *y,  *z;

THIS IS AN EXPLANATION.
>I think I understand what you are trying to do.
>pointb is copied from pointa, but when you assign new values to pointa, values won't be automatically copied to pointb >unless you use a pointer.

THIS IS A DETAILED EXPLANATION (AS ALEX LIKE);
>That is wrong.
>Look at that:
>pointa:  { 0x00001230, 0x00001240, 0x00001250 };
>pointa.x = 0x00001230:  { 12 }
>pointa.y = 0x00001240:  { 16 }
>pointa.z = 0x00001250:  { 20 }
etcetera.....
0

Commented:
>>>> If you have homework, and don't understand part of it, be honest about it. The >>>> Experts will be more than happy to teach you.

There is nothing in the question (and the answers) that goes beyond that part of the EE guide lines regarding homework.

Regards, Alex
0

Commented:
I know Axter is a teacher and teacher's don't like if their questions to pupils were answered by experts. But that is his problem. That is/was an open forum and not any single person should be allowed to make their own narrow-minded opinion a general rule.

Regards, Alex
0

Software ArchitectCommented:
I am a teacher too (sometimes), and I know that it is hard for everybody (specially for a 16-18 years old kid) to understand them.

0

Software ArchitectCommented:
I refer to the 'pointers' Chapter.
0

Commented:
It's hard to unterstand for me what happens here. As fast as they have been to delete the question as slow now are their reactions...

Axter, ee_ai_construct: I want an answer. And i'm no school boy.

Regards, Alex
0

Commented:
>>>> Thanks Jaime, but I didn't write this code.  It was given to us by
>>>> the instructor as a review for an exam, and we are asked to say
>>>> what the output is the way it is written.  Your comment about
>>>> the values won't be copied automatically to point b without a
>>>> pointer lead me to believe the answer is 20, 30, 40 for pointa
>>>> and 12, 16, 20 for pointb.

That was the author's comment before Axter's first and only comment. You can easily see, that the questioner wasn't awaiting a complete answer but tried to unterstand what happens and gave an own suggestion. Jaime's answer was misleading the questioner to a wrong conclusion.  So, the answer *must* be corrected.

Regards, Alex

0

Commented:
The answer to this question is 42.
0

Commented:
>>Axter, ee_ai_construct: I want an answer. And i'm no school boy.

Exactly what is the question???
http:Q_21155598.html
0

Author Commented:
Thanks to all who helped me, and I have split the points.  I will say that I am NOT 16 - 18 (ha ha), but am a 54 year old who is taking classes while working full time( testing server management software).  I have a master's degree, but not in the right field to advance any further in my job.  I am also taking this class online, so I don't have a live instructor to ask for advice.  I also did not think that getting input for a question from an exam review was the same as asking for code for a homework assignment.
Cori
0

RetiredCommented:
I am in the same position as coririzzo: You can look at my bio under gonzal13. I too am trying to learn VB6 just for the pleasure of it. I took the last semester that it was offered since they were changing over to VB.Net. The teacher changed the two day schedule to one day schedule.

Fortunately I bought the books recomended on the visual basic site and use the MSDN reference manual. Still they are all generic. One learns by seeing examples of code also. Thus my need to post on th VB site. Several have been very patient with me. Others are suspicious.

On to of thing to knowt isthat I am a 60 year old retired roject engineer that always has had to jump into the unknown. Since I have free time, I decided to take a very large program that I wrote in Basic in 1982 and rewrite it in vsuac basic. I reason that by the time I finish I should have a fair to good command of VB6.

Thus I need to work with understanding and patient experts in theVB6 area.

gonzal13(Joe)
0

Commented:
gonzal13, this is a C++ area not VB. However, I can recommend learning C++ for the pleasure of it.

I find it reassuring to know that there are some old timers out there learning this stuff. I've been a slow learner all my life and am probably getting slower in middle age. I'd like to think that I can still be receptive to new ideas in retirement. It is inspirational to hear about 60+s jumping in.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.