classes: understanding the sequence of the source code

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2008
Posts: 32
Reputation: salman213 is an unknown quantity at this point 
Solved Threads: 0
salman213 salman213 is offline Offline
Light Poster

classes: understanding the sequence of the source code

 
0
  #1
Jun 20th, 2008
  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: classes: understanding the sequence of the source code

 
0
  #2
Jun 20th, 2008
  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--

  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;
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 32
Reputation: salman213 is an unknown quantity at this point 
Solved Threads: 0
salman213 salman213 is offline Offline
Light Poster

Re: classes: understanding the sequence of the source code

 
0
  #3
Jun 20th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: classes: understanding the sequence of the source code

 
0
  #4
Jun 20th, 2008
Originally Posted by salman213 View Post
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--

  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--

  1.  
  2. cout << rect.getX() << ", " << rect.getY() << endl;
  3. cout << rectb.getX() << ", " << rectb.getY() << endl;
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 32
Reputation: salman213 is an unknown quantity at this point 
Solved Threads: 0
salman213 salman213 is offline Offline
Light Poster

Re: classes: understanding the sequence of the source code

 
0
  #5
Jun 20th, 2008
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.
?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,829
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 750
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: classes: understanding the sequence of the source code

 
1
  #6
Jun 20th, 2008
>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.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 32
Reputation: salman213 is an unknown quantity at this point 
Solved Threads: 0
salman213 salman213 is offline Offline
Light Poster

Re: classes: understanding the sequence of the source code

 
0
  #7
Jun 20th, 2008
alright cool, thanks a lot!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC