clayalberty
Programmer
I'm just starting to learn c++ and I'm working through a book. I have a basic confusion relating to class usage. To give an example of a has-a class paraphrased from the book:
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 also 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!
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 also 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!