| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
trouble with has-a class in c++
I'm just starting to learn c++ and I'm working through a book. I have a confusion relating to class usage--I'm not even sure what I don't understand but I think I can describe it. To give an example of a has-a class paraphrased from the book:
Code:
class Point // holds x,y coordinates
{
public:
void SetX(int x) { itsX = x; }
void SetY(int y) { itsY = y; }
int GetX()const { return itsX;}
int GetY()const { return itsY;}
private:
int itsX;
int itsY;
};
class Rectangle
{
<code omitted>
private:
Point itsUpperLeft;
Point itsUpperRight;
Point itsLowerLeft;
Point itsLowerRight;
int itsTop;
int itsLeft;
int itsBottom;
int itsRight;
};
Here's my question: WHY are we declaring variables of type Point? I can see that Point can hold 8 bytes assuming that each integer is 4 bytes (from itsX and itsY)...and I understand that Point is now a user-defined type just like int is a built-in type...but what now are the properties of itsUpperLeft, etc.? Point is then used in other places in the code as a return type for several member functions of Rectangle, which seems to present the same problem in my mind. Many Thanks! |
|
#2
|
|||
|
|||
|
Answer
Here's my question: WHY are we declaring variables of type Point? I can see that Point can hold 8 bytes assuming that each integer is 4 bytes (from itsX and itsY)...and I understand that Point is now a user-defined type just like int is a built-in type...but what now are the properties of itsUpperLeft, etc.? Point is then used in other places in the code as a return type for several member functions of Rectangle, which seems to present the same problem in my mind.
I copied this so I could remember the question. I think you declare variables of type point so you have access to the member functions. If they didnt declare itsUpperLeft as type Point this itsUpperLeft.SetX(left); itsUpperLeft.SetY(top); on lines 10-11(8.4 Rect.cpp) wouldn't work because they arent of type Point. The properties are the same as a Point. Since Point is now like a built in type you can return it from a function. Hopes this helps.If not tryposting it at this website http://www.jeffcogswell.com |
|
#3
|
|||
|
|||
|
Hey guys I've been trying to find a way to say this without sounding rude but I'm not sure if its worked or not. Please don't take offence I'm just trying to help.
Based on the questions you are asking it looks like you don't understand some of the core concepts of using classes in code. While I could explain there are enough tutorials all over the net that can help you out just as well. My advice is to find one and rip apart the code until you see whats going on. Hope this helps, -KM- |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > trouble with has-a class in c++ |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|