| | |
classes: understanding the sequence of the source code
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
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.
New members chased away this month: 5
![]() |
Other Threads in the C++ Forum
- Previous Thread: Hash Tables - Need a few pointers
- Next Thread: Completely customised GUI?
Views: 525 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






