943,650 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 636
  • C++ RSS
Jun 20th, 2008
0

classes: understanding the sequence of the source code

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class CRectangle {
  5. int x, y;
  6. public:
  7. void set_values (int,int);
  8. int area () {return (x*y);}
  9. };
  10.  
  11. void CRectangle::set_values (int a, int b) {
  12. x = a;
  13. y = b;
  14. }
  15.  
  16. int main () {
  17. CRectangle rect, rectb;
  18. rect.set_values (3,4);
  19. rectb.set_values (5,6);
  20. cout << "rect area: " << rect.area() << endl;
  21. cout << "rectb area: " << rectb.area() << endl;
  22. return 0;
  23. }

In the above code I am confused on the lines from rect.set_values to cout<<

After rect.set_values(3,4) is executed are the values 3 and 4 stored in the variables "x" and "y" ?

and then when rectb.setvalues(5,6) is executed are the variables "x" and "y" overwritten with "5" and "6"?

If that is true then how come when the cout<< 's are executed afterwards they each have a different value?


Thanks, if my question does not make sense, please tell me I will try to be more clear.




EDITED: NEVERMIND

I think I just understood it.
rectb and rect are different variables so basically the class that was defined is a general one which is a definition for each variable. So each variable has a different class thats "template" is the one shown at the top. So I guess the set_values stores the numbers in "x" and "y" which are different for each variable.


If thats right then I guess the thread can be closed, sorry about that
Last edited by salman213; Jun 20th, 2008 at 12:17 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
salman213 is offline Offline
32 posts
since Jun 2008
Jun 20th, 2008
0

Re: classes: understanding the sequence of the source code

c++ Syntax (Toggle Plain Text)
  1. CRectangle rect, rectb;
  2. rect.set_values (3,4);
  3. rectb.set_values (5,6);
  4. cout << "rect area: " << rect.area() << endl;
  5. cout << "rectb area: " << rectb.area() << endl;

rect and rectb are 2 different variables that are of the same class-type.

to make the code more understandable it might be better for you to do something like this--

c++ Syntax (Toggle Plain Text)
  1. CRectangle rect; // a CRectangle reference-variable named rect
  2. CRectangle rectb; // a CRectangle reference-variable named rectb
  3. rect.set_values (3,4);
  4. rectb.set_values (5,6);
  5. cout << "rect area: " << rect.area() << endl;
  6. cout << "rectb area: " << rectb.area() << endl;
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Jun 20th, 2008
0

Re: classes: understanding the sequence of the source code

Ok thanks I think I understand my previous question but another question are the values "3" and "4" or "5" and "6" stored in any specific variable?

Like if I wanted to show the "3" and "4" can I write a line

cout <<specificvariable;

what would that specificvariable be?
Reputation Points: 10
Solved Threads: 0
Light Poster
salman213 is offline Offline
32 posts
since Jun 2008
Jun 20th, 2008
0

Re: classes: understanding the sequence of the source code

Click to Expand / Collapse  Quote originally posted by salman213 ...
Ok thanks I think I understand my previous question but another question are the values "3" and "4" or "5" and "6" stored in any specific variable?

Like if I wanted to show the "3" and "4" can I write a line

cout <<specificvariable;

what would that specificvariable be?
If you want to display the individual variables, you will have to understand that calling rect.x or rectb.x and rect.y and rectb.y are illegal calls since classes are initialized with private member variables until you provide public access members/functions.

If you want to get a specific variable, you'll need to provide a public "getter," like this--

c++ Syntax (Toggle Plain Text)
  1. class CRectangle {
  2. int x, y;
  3. public:
  4. void set_values (int,int);
  5. int area () {return (x*y);}
  6. int getX(){return x;};
  7. int getY(){return y;};
  8. };

Now for rext and rectb you can call getX() and getY() with cout to display their respective x's and y's--

c++ Syntax (Toggle Plain Text)
  1.  
  2. cout << rect.getX() << ", " << rect.getY() << endl;
  3. cout << rectb.getX() << ", " << rectb.getY() << endl;
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Jun 20th, 2008
0

Re: classes: understanding the sequence of the source code

oh that makes sense!!!!!!

but can u just make the

int x and int y public instead?

then call rectb.x and rect.x etc.
?
Reputation Points: 10
Solved Threads: 0
Light Poster
salman213 is offline Offline
32 posts
since Jun 2008
Jun 20th, 2008
1

Re: classes: understanding the sequence of the source code

>but can u just make the
>int x and int y public instead?
You can, but public data is typically a bad idea because it means you're giving full control over the values to everyone. Most of the time you don't want to do that, which is why public or protected member functions are used to give access without losing control.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 20th, 2008
0

Re: classes: understanding the sequence of the source code

alright cool, thanks a lot!
Reputation Points: 10
Solved Threads: 0
Light Poster
salman213 is offline Offline
32 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Hash Tables - Need a few pointers
Next Thread in C++ Forum Timeline: Completely customised GUI?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC