< previous page page_107 next page >

Page 107
LISTING 7.4. continued
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
23:
24:  // compute area of the rectangle by finding corners,
25:  // establish width and height and then multiply
26:  int Rectangle::GetArea() const
27:  {
28:        int Width = itsRight-itsLeft;
29:        int Height = itsTop - itsBottom;
30:        return (Width * Height);
31:  }
32:
33:  int main()
34:  {
35:        //initialize a local Rectangle variable
36:        Rectangle MyRectangle (100, 20, 50, 80 );
37:
38:        int Area = MyRectangle.GetArea();
39:
40:        cout << Area:  << Area <<  \n;
41:        cout << Upper Left X Coordinate: ;
42:        cout << MyRectangle.GetUpperLeft().GetX();
43:      return 0;
44:  }

Output:
Area: 3000
Upper Left X Coordinate: 20
Analysis: Lines 314 in Listing 7.3 declare the class Point, which is used to hold a specific x,y coordinate on a graph. As it is written, this program doesn't use Points much. However, other drawing methods require Points.
Within the declaration of the class Point, you declare two member variablesitsX and itsYon lines 12 and 13. These variables hold the values of the coordinates. As the x coordinate increases, you move to the right on the graph. As the y coordinate increases, you move upward on the graph. Other graphs use different systems. Some windowing programs, for example, increase the y coordinate as you move down in the window.
The Point class uses inline accessor functions to get and set the X and Y points declared on lines 710. Points use the default constructor and destructor; therefore, you must set their coordinates explicitly.
Line 17 begins the declaration of a Rectangle class. A Rectangle consists of four points that represent the corners of the Rectangle.
The constructor for the Rectangle (line 20) takes four integers, known as top, left, bottom, and right. The four parameters to the constructor are copied into four member variables, and then the four Points are established.

 
< previous page page_107 next page >

If you like this book, buy it!