In our previous discussion of classes and objects, we limited ourselves to non-pointer member variables. Adding pointer members increases the power and flexibility of object-oriented programs and allows programmers to implement more UML class relationships. Nevertheless, allowing pointer members also increases the complexity and effort of programming with classes and objects.
class Person
{
private:
string* name;
int weight;
double height;
}; |
||
(a) | (b) | (c) |
Syntactically, adding a pointer member variable to a class is seemingly trivial - adding an asterisk to one or more of the variable names. But conceptually, the change has significant consequences. When we change a member variable from a non-pointer to a pointer, we also change the relationship from composition to aggregation. Changing the relationship means changing a strong, tight binding to a loose, weak one. Aggregated objects may have different lifetimes, the whole can share its parts with other objects in the program, and the whole can discard or replace its parts whenever convenient. The increase in complexity requires us to explore five distinct concepts related to pointer member variables:
We explore these concepts in the following sections.