| | |
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:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; class CRectangle { int x, y; public: void set_values (int,int); int area () {return (x*y);} }; void CRectangle::set_values (int a, int b) { x = a; y = b; } int main () { CRectangle rect, rectb; rect.set_values (3,4); rectb.set_values (5,6); cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; return 0; }
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.
c++ Syntax (Toggle Plain Text)
CRectangle rect, rectb; rect.set_values (3,4); rectb.set_values (5,6); cout << "rect area: " << rect.area() << endl; 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)
CRectangle rect; // a CRectangle reference-variable named rect CRectangle rectb; // a CRectangle reference-variable named rectb rect.set_values (3,4); rectb.set_values (5,6); cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl;
•
•
•
•
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 get a specific variable, you'll need to provide a public "getter," like this--
c++ Syntax (Toggle Plain Text)
class CRectangle { int x, y; public: void set_values (int,int); int area () {return (x*y);} int getX(){return x;}; int getY(){return y;}; };
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)
cout << rect.getX() << ", " << rect.getY() << endl; cout << rectb.getX() << ", " << rectb.getY() << endl;
>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.
>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.
I'm here to prove you wrong.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Hash Tables - Need a few pointers
- Next Thread: Completely customised GUI?
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






