|
|
|
|
|
|
|
declare a Wheel class, a Motor class, a Transmission class, and so forth, and then combine them into a Car class. This declares a has-a relationship: A car has a motor, it has wheels, and it has a transmission. |
|
|
|
|
|
|
|
|
Consider a second example. A rectangle is composed of lines. A line is defined by two points. A point is defined by an x coordinate and a y coordinate. Listing 7.3 shows a complete declaration of a Rectangle class as it might appear in RECTANGLE.HPP. Because a rectangle is defined as four lines connecting four points, and each point refers to a coordinate on a graph, a Point class is first declared to hold the x,y coordinates of each point. Listing 7.4 shows a complete declaration of both classes. |
|
|
|
|
|
|
|
|
LISTING 7.3 DECLARING A COMPLETE CLASS |
|
|
|
 |
|
|
|
|
1: // Begin Rect.hpp
2: #include <iostream.h>
3: class Point // holds x,y coordinates
4: {
5: // no constructor, use default
6: public:
7: void SetX(int x) { itsX = x; }
8: void SetY(int y) { itsY = y; }
9: int GetX()const { return itsX;}
10: int GetY()const { return itsY;}
11: private:
12: int itsX;
13: int itsY;
14: }; // end of Point class declaration
15:
16:
17: class Rectangle
18: {
19: public:
20: Rectangle (int top, int left, int bottom, int right);
21: ~Rectangle () {}
22:
23: int GetTop() const { return itsTop; }
24: int GetLeft() const { return itsLeft; }
25: int GetBottom() const { return itsBottom; }
26: int GetRight() const { return itsRight; }
27:
28: Point GetUpperLeft() const { return itsUpperLeft; }
29: Point GetLowerLeft() const { return itsLowerLeft; }
30: Point GetUpperRight() const { return itsUpperRight; }
31: Point GetLowerRight() const { return itsLowerRight; }
32:
33: void SetUpperLeft(Point Location) {itsUpperLeft = Location;} |
|
|
|
 |
|
|
|
|
continues |
|
|
|
|
|