Solved

Need help with exam review question- class with pointers

Posted on 2004-10-03
27
329 Views
Last Modified: 2010-04-01
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?      
0
Comment
Question by:coririzzo
  • 8
  • 6
  • 3
  • +4
27 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
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;
instead of:
          int x, int y, int z;
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
There is a syntax error in this line:
          int *x, int *y, int *z;
 must be:
          int *x,  *y,  *z;
 
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 100 total points
Comment Utility
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
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
Now I have to go to sleep, good luck,
Jaime.
0
 

Author Comment

by:coririzzo
Comment Utility
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
 
LVL 30

Expert Comment

by:Axter
Comment Utility
This is clearly a homework question.

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

Assisted Solution

by:rstaveley
rstaveley earned 100 total points
Comment Utility
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
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 300 total points
Comment Utility
>> 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
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
> 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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>> 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
 

Expert Comment

by:ee_ai_construct
Comment Utility
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:
  You are advised to read this link regarding homework if you haven't already:
  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
Community Support Admin
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
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;
>instead of:
>          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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>> 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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
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
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
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
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
I refer to the 'pointers' Chapter.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>> 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
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
The answer to this question is 42.
0
 
LVL 30

Expert Comment

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

Exactly what is the question???
Please post in Dan's link:
http:Q_21155598.html
0
 

Author Comment

by:coririzzo
Comment Utility
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
 
LVL 13

Expert Comment

by:gonzal13
Comment Utility
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
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now