Help understanding how values are assigned in this code.

Hi,

I am currently looking at a program and cannot understand how it works as I am still learning about how classes can use functions from another class. I understand how GetArea() works. I understand how itsTop, itsLeft, itsBottom & itsRight are assigned values. However, I do not understand how itsUpperLeft, itsUpperRight, itsLowerLeft & its LowerRight are being assigned values and how they can have an x & y value. I'm also unsure how location works, is location an object of the Point class?

I think all the values are being assigned in Rectangle (int top, int left, int bottom, int right).

Would really appreciate if someone could explain it to me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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;
}; //end of Point class declaration

class Rectangle
{
	public:
			Rectangle (int top, int left, int bottom, int right);
			~Rectangle () {}
			
			int GetTop() const {return itsTop;}
			int GetLeft () const {return itsLeft;}
			int GetBottom () const {return itsBottom;}
			int GetRight () const {return itsRight;}
			
			Point GetUpperLeft () const {return itsUpperLeft;}
			Point GetLowerLeft () const {return itsLowerLeft;}
			Point GetUpperRight () const {return itsUpperRight;}
			Point GetLowerRight () const {return itsLowerRight;}
			
			void SetUpperLeft (Point location) {itsUpperLeft = Location;}
			void SetLowerLeft (Point location) {itsLowerLeft = Location;}
			void SetUpperRight (Point location) {itsUpperRight = Location;}
			void SetLowerRight (Point location) {itsLowerRight = Location;}
			
			void SetTop {int top) {itsTop = top;}
			void SetLeft {int left) {itsleft = left;}
			void SetBottom {int bottom) {itsBottom = bottom;}
			void SetRight {int right) {itsRight = right;}
			
			int GetArea () const;
			
	private:
			Point ItsUpperLeft;
			Point ItsUpperRight;
			Point ItsLowerLeft;
			Point ItsLowerRight;
			int itsTop;
			int itsLeft;
			int itsBottom;
			int itsRight;
};//end of Rectangle class declaration

Rectangle::Rectangle (int top, int left, int bottom, int right)
{
	itsTop = top;
	itsLeft = left;
	itsBottom = bottom;
	itsRight = right;
	
	itsUpperLeft.SetX(left);
	itsUpperLeft.SetY(top);
	
	itsUpperRight.SetX(right);
	itsUpperRight.SetY(top);
	
	itsLowerLeft.SetX(left);
	itsLowerLeft.SetY(bottom);
	
	itsLowerRight.SetX(right);
	itsLowerRight.SetY(bottom);
}

//compute area if the rectangle by finding corners sides,
//establish width and height and then multiply
int Rectangle::GetArea() const
{
	int Width = itsRight - itsLeft;
	int Height = itsTop - itsBottom;
	return (Width * Height);
}
To begin in the definition of the Rectangle constructor (the Rectangle::Rectangle(int top...) part) itsUpperLeft should be ItsUpperLeft, itsUpperRight should be ItsUpperRight etc. Now the reason they have can have two values is that they are objects of type Point, which has two data members named itsX and itsY. Also since they are objects of type Point you can use Point member functions on them like SetX(). Also note that a Rectangle object does not use Point member functions directly, so you can do something like
1
2
Rectangle rect;
rect.SetX()

rect.SetX() doesn't even make sense, so it's a good thing you can't do this. The Point member functions are only used in the class definition of a rectangle.

Also if you were to switch the declaration of the Point class to after the declaration of the Rectangle class you would have a compile-time error as the Point class is not in scope for use by the Rectangle class definition.

Finally
I'm also unsure how location works, is location an object of the Point class?

You got it. To be excessively precise:
void SetUpperLeft (Point location)
means SetUpperLeft is a function that takes a Point argument and returns nothing.

Edit: Also just noticed some more typos
1
2
3
4
void SetTop {int top) {itsTop = top;}
void SetLeft {int left) {itsleft = left;}
void SetBottom {int bottom) {itsBottom = bottom;}
void SetRight {int right) {itsRight = right;}

should be
1
2
3
4
void SetTop (int top) {itsTop = top;}  //change {int to (int
void SetLeft (int left) {itsleft = left;}  //same
void SetBottom (int bottom) {itsBottom = bottom;}
void SetRight (int right) {itsRight = right;}
Last edited on
Ok I understand why it can have 2 values now. (I know it doesn't really as it is two different objects)

Would I be right in thinking that many of the functions in Rectangle are not actually used and probably not needed?

For example the 4 coordinates are being assigned their value by:

1
2
3
4
5
6
7
8
9
10
11
itsUpperLeft.SetX(left);
	itsUpperLeft.SetY(top);
	
	itsUpperRight.SetX(right);
	itsUpperRight.SetY(top);
	
	itsLowerLeft.SetX(left);
	itsLowerLeft.SetY(bottom);
	
	itsLowerRight.SetX(right);
	itsLowerRight.SetY(bottom);


so these functions are not needed:

1
2
3
4
void SetUpperLeft (Point location) {itsUpperLeft = Location;}
			void SetLowerLeft (Point location) {itsLowerLeft = Location;}
			void SetUpperRight (Point location) {itsUpperRight = Location;}
			void SetLowerRight (Point location) {itsLowerRight = Location;}


I'm just confused what is the point of location as I don't see how it is working.
The functions that aren't used right now could be used for something else. For example say you have a resize() function that takes a Rectangle object reference and a double value and shrinks/expands the rectangle by that factor. You may use code like this in a program:
1
2
Rectangle blocky(100, 10, 50, 20);
resize(blocky, 1.5);


To work the code for resize() may contain lines like the following:
1
2
3
4
Point newTopLeft;
newTopLeft.setX(*stuff*);
newTopLeft.setY(*stuff*);
squarish.SetUpperLeft(newTopLeft); //assuming squarish is a Rectangle object in resize() argument list 


these functions are not needed

Yes and no. Let say that you have a simple rectangle: { {0,0}, {0,1}, {2,1}, {2,0} }

You can now move the upper left corner from {0,1} to {0,3}. Problem is that you no longer have a rectangle, and 'top' and 'area' are wrong too.

If you proceed with:
SetTop(3);
SetUpperRight( Point( 2, 3 ) );
you will again have a proper rectangle: { {0,0}, {0,3}, {2,3}, {2,0} }

Being able to change a Rectangle is a good thing and as such is needed.
Incomplete, inconsistent changes break promises and as such do things in very bad way.
Topic archived. No new replies allowed.