An object can contain another object, in which case the contained object is considered to be a subobject. In Example 2.9, each Square object has two Point subobjects. Notice that Point has a function named ~Point. We discuss this kind of function in the next section.
Example 2.9. src/subobject/subobject.h
[ . . . . ] class Point { public: Point(int xx, int yy) : m_x(xx), m_y(yy){} ~Point() { cout << "point destroyed: (" << m_x << "," << m_y << ")" << endl; } private: int m_x, m_y; }; class Square { public: Square(int ulx, int uly, int lrx, int lry) : m_UpperLeft(ulx, uly), m_LowerRight (lrx, lry) <-- 1 {} Square(const Point& ul, const Point& lr) : m_UpperLeft(ul), m_LowerRight(lr) {} <-- 2 private: Point m_UpperLeft, m_LowerRight; <-- 3 }; [ . . . . ]
|
Whenever an instance of Square is created, each of its subobjects is created with it, so that the three objects occupy contiguous chunks of memory.
The Square is composed of two Point objects.
Because Point has no default constructor, we must properly initialize for each Point subobject in the member initialization list of Square (see Example 2.10).
Example 2.10. src/subobject/subobject.cpp
#include "subobject.h" int main() { Square sq1(3,4,5,6); Point p1(2,3), p2(8, 9); Square sq2(p1, p2); } |
Even though no destructor was defined for Square, each of its Point subobjects is properly destroyed whenever the containing object is. This is an example of composition.
point destroyed: (8,9) point destroyed: (2,3) point destroyed: (8,9) point destroyed: (2,3) point destroyed: (5,6) point destroyed: (3,4)
Part I: Introduction to C++ and Qt 4
C++ Introduction
Classes
Introduction to Qt
Lists
Functions
Inheritance and Polymorphism
Part II: Higher-Level Programming
Libraries
Introduction to Design Patterns
QObject
Generics and Containers
Qt GUI Widgets
Concurrency
Validation and Regular Expressions
Parsing XML
Meta Objects, Properties, and Reflective Programming
More Design Patterns
Models and Views
Qt SQL Classes
Part III: C++ Language Reference
Types and Expressions
Scope and Storage Class
Statements and Control Structures
Memory Access
Chapter Summary
Inheritance in Detail
Miscellaneous Topics
Part IV: Programming Assignments
MP3 Jukebox Assignments
Part V: Appendices
MP3 Jukebox Assignments
Bibliography
MP3 Jukebox Assignments