Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie question on classes 1

Status
Not open for further replies.

clayalberty

Programmer
May 17, 2003
2
0
0
US
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!
 
>WHY are we declaring variables of type Point??

The variables seem a bit redundant really, perhaps they are just there for some illustration.

Point mUpperLeft;
Point mBottomRight;

should be enough. You could still get the &quot;bottom&quot;:
int GetBottom() const { return mBottomRight.GetY(); }

No need for the extra itsBottom etc.

>but what now are the properties of itsUpperLeft, etc.?

You mean what value holds itsUpperLeft.itsX, itsUpperLeft.itsY etc? That depends on what (if any) values is given in the constructor.

/Per

[sub]Nerdy signatures are as lame as the inconsistent stardates of STTNG.[/sub]
 
>> I have a basic confusion relating to class usage.
>> WHY are we declaring variables of type Point??

Well we don't know Why the author of the book did that. There sure seems to be more data members in Rectangle than are needed. Perhaps the book is making a specific point about something. If so, we can't help with that.

Many examples given in code are not done so from a design standpoint. What i mean is they are given for the single purpose of clarifying a point rather than this is a correct design.

-pete
 
Thanks, I think I see the corrolation better now. Yes, the program is horribly redundant. The whole thing is about 200 lines of code to find the area of a rectangle given the input of 2 x,y coordinates...none too helpful;)
 
4 coordinates are useful if the edges of a rectangle are not parallel to the the x and y axis (that way a line can be drawn from point to point). Not sure about the other variables.
 
BoulderBum wrote:
4 coordinates are useful if the edges of a rectangle are not parallel to the the x and y axis (that way a line can be drawn from point to point). Not sure about the other variables.

Even still, 4 coordinates means 8 numbers. You could get by with 5 (X and Y coordinate of the center; width, height, rotation.)

[sub]I REALLY hope that helps.[/sub]
Will
 
I hear ya, but I think having 4 points would simplify representing the square graphically. I can see using the four point approach for conceptual simplicity and to eliminate calculation overhead if the prof plans to use draw-line functions later on.

I'm still a little puzzled over the advantage of the other members, however.
 
RE: What's the point of returning a coordinate class? Because that way you can return two values, x and y, simultaneously. Otherwise, you would have to pass them by reference through the parameter list (keep in mind that a class is really just a struct).

Also, it is often conceptually easier to work with a Point than with two separate x/y coordinates. Right now you are drawing a square, but in another application you might use the Points to draw a triangle, or to plot a character's position in a game.

Finally, the whole point of using classes and object oriented design is associate behavior with characteristics. There might be several operations you perform repeadedly on a Point. Rather than creating an x and y int in every class that might rely on them and then rewriting all the coordinate-related functions to manipulate them, you put those functions in the Point class and instantiate the Point. Write once, use many.

RE: Rotated rectangle issue: I would think this would be better handled as a polygon. Regular rectangles are too commonly used to make a special case for rotated versions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top