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:
- 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.
- 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.
- Study the code in the Book class. See how it mirrors the information
in the class diagram.
- 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.
- The Main( ) method will create objects of the Book class.
- The Main( ) method now sends messages to these object to carry out the operation of the program.
- 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
A UML class diagram for the Book class is
The example program is located
here. An executable of this program can be found