Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need help with exam review question- class with pointers

Posted on 2004-10-03
27
Medium Priority
?
373 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 3
  • +4
27 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12214370
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
ID: 12214382
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 400 total points
ID: 12214411
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12214412
Now I have to go to sleep, good luck,
Jaime.
0
 

Author Comment

by:coririzzo
ID: 12214435
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
ID: 12214722
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 400 total points
ID: 12214885
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 1200 total points
ID: 12215433
>> 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
ID: 12216722
> 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
ID: 12216986
>>>> 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
ID: 12217244
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12217336
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
ID: 12217342
>>>> 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
ID: 12217432
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
ID: 12217456
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
ID: 12217465
I refer to the 'pointers' Chapter.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12217545
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
ID: 12217839
>>>> 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
ID: 12218774
The answer to this question is 42.
0
 
LVL 30

Expert Comment

by:Axter
ID: 12221974
>>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
ID: 12223050
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
ID: 12343592
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
ID: 12345515
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

688 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