Programming Example 13: Implementing Your Own Classes


This example illustrates some important concepts related to the design and implementation of your own classes. As you study this example look for the following important concepts:

  1. First, look at the class diagram for this class. Look at the data members of this class and the methods of this class. Notice how the data and the methods all work together. The data is private and the methods are public.
  2. Look carefully at the over-all structure of this program. It actually contains two classes. The first is named Program and this class contains the driver code in Main( ). The second class is the Book class.
  3. Study the code in the Book class. See how it mirrors the information in the class diagram.
  4. Study the signatures for the getters, setters, and data manipulation methods in the book class. Getters take no parameters but always return something. Setters take a parameter, but return nothing. Data Manipulation functions usually take no parameters, and usually return something.
  5. The Main( ) method will create objects of the Book class.
  6. The Main( ) method now sends messages to these object to carry out the operation of the program.
  7. Notice that the Program class also contains a method to get data from the user and store that data in a Book object. This method takes a Book object as a parameter.

The problem statement for this program is located here.

A UML class diagram for the Book class is here.

The example program is located here.  An executable of this program can be found here.