Classes with Other Classes as Member Data
It is not uncommon to build up a complex class by declaring simpler classes and including them in the declaration of the more complicated class. For example, you might 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 8.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 ...
Get Sams Teach Yourself C++ in 24 Hours, Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.