Sams Teach Yourself C++ in 21 Days

(singke) #1
Understanding Object-Oriented Programming 167

6


9:
10: itsUpperLeft.SetX(left);
11: itsUpperLeft.SetY(top);
12:
13: itsUpperRight.SetX(right);
14: itsUpperRight.SetY(top);
15:
16: itsLowerLeft.SetX(left);
17: itsLowerLeft.SetY(bottom);
18:
19: itsLowerRight.SetX(right);
20: itsLowerRight.SetY(bottom);
21: }
22:
23:
24: // compute area of the rectangle by finding sides,
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: std::cout << “Area: “ << Area << “\n”;
41: std::cout << “Upper Left X Coordinate: “;
42: std::cout << MyRectangle.GetUpperLeft().GetX();
43: return 0;
44: }

Area: 3000
Upper Left X Coordinate: 20
Lines 3–14 in Rectangle.hpp(Listing 6.8) declare the class Point, which is
used to hold a specific x- and y-coordinate on a graph. As written, this program
doesn’t use Pointsmuch; however, other drawing methods require Points.

OUTPUT


LISTING6.9 continued


ANALYSIS
Free download pdf