Programming Previous page   Next Page

Example -- The Portfolio Container

Aggregation is the containment of one class by another class. The basic relationship is: each contained class "is a part of" the container class.

For example, consider a financial portfolio class as a container for a set of assets (stocks, bonds, savings, etc.). Once the individual assets are grouped, they can be analyzed, and useful information can be returned. The contained objects are not accessible directly, but only via the portfolio class methods.

See Example -- Assets and Asset Subclasses for information about the assets collected by this portfolio class.

Designing the Portfolio Class

The portfolio class is designed to contain the various assets owned by a given individual and provide information about the status of his or her investment portfolio. This example implements a somewhat over-simplified portfolio class that

Required Portfolio Methods

The portfolio class implements only three methods:

Since a portfolio object contains other objects, the portfolio class methods can use the methods of the contained objects. For example, the portfolio display method calls the stock class display method, and so on.


Previous page  The Stock display Method The Portfolio Constructor Method Next page

© 1994-2005 The MathWorks, Inc.