Class Diagram for a Hospital Management System TUTORIAL Part II Modeling and Programming OO Entities Car Simulation

Document Sample
Class Diagram for a Hospital Management System TUTORIAL Part II Modeling and Programming OO Entities Car Simulation Powered By Docstoc
					TUTORIAL
Part II. Modeling and Programming OO
Entities: Car Simulation System
5. Modeling the System and Classes in the Object Model
by Richard C. Felsinger for Microgold Software, Inc.
WithClass Scripting CASE Tool developed by Michael Gold

The purpose of this chapter is to describe how to model the system and classes in a system. The basic
topics are:

          What is a system, class, object, attribute, and operation?
          How to model and describe a system.

          How to model a class consisting of attributes and operations with a drawing, class diagram, class
           specification, data dictionary and C++ code.

          How to create the class diagram with WithClass.
          How to create the class specification with WithClass.
          How to generate C++ source code with a WithClass script and how to understand the C++
           source code.


In this chapter we will describe the an instructional Car Simulation System. We will describe the system
consisting of a car class, motor class, and other classes. We will use diagrams, text specifications, and
C++ code. In the later chapters we will describe the relationships between the classes in the Car
Simulation System with diagrams, text specifications, and C++ code.


First, the system object model is a description of the system as a whole. It consists of diagrams and text
specifications at the system level. It is important to understand the system as a whole first, before you
model the classes and object inside the system Next the object model is a description of classes and
objects in a system. The object model is like a still photograph of a house, park, tennis court, etc. It
captures the static structure of a system. It is like an electronics wiring diagram that shows the structure
of electronic circuits in a VCR, radio, or television. The key question to create the object model is "What
are the classes, objects, and relationships in the system?"


MODELING THE SYSTEM IN THE SYSTEM OBJECT MODEL

What is a System?


The object model is a description of the static structure of the system and classes and objects in a
system. A system is an entity that can be treated as a unit that is composed of simpler components that



Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
work together to perform a function. A subsystem is a system that is a component of a larger more
complex system. The term system is a general term that may refer to a very large system with smaller
subsystems or to a small system without subsystems. Examples of physical systems are a house, a ship,
an airplane, a shopping maul, and a hospital.


In software, a small system (or subsystem) may consist of 10 to 100 classes as a rough order of
magnitude. In some C++ development environments, such as Borland C++ and Microsoft Visual C++ , a
project groups together a set of files of classes as a system. For execution, a system is an executable
program (.EXE file), a dynamic link library, a process, or other executable entity. In this tutorial we will
use the term system to refer to a small system or to a subsystem. Sample systems are the TV Controller
System or the Automatic Teller Machine System which consist of 10 to 15 classes. A simplified diagram
of classes, objects, and relationships in a system is shown below.




Figure 5.1. Classes, Objects, and Relationships in a System

The first system that we will examine is a very simple Car Simulation System. This is for instructional
purposes only. It is instructive to examine a real world, physical system in terms of classes, objects,
and relationships. Later we'll examine typical software information systems like a TV control, coffee
machine control, automobile cruise control, and currency control system.


The Car Simulation System is a simulation of a car with a motor, tires, passengers and a cellular phone.
It has the stimulus response behavior to respond to events (stimuli or commands) such as start car,
add passenger, and make phone call. In this example, there will be no graphic user interface.


In modeling this system we will use the "OO Modeling and Prototyping Roadmap" in a previous chapter.
We will create numerous diagrams and text specifications.


Documenting the System in the System Object Model


There are many documentation products to describe the system in the system object model.
As shown in the figure below, the system object model consists of the following documentation products:
requirements statement, drawing, block diagram, system diagram, and system specification.
We will create these documentation products for the Car Simulation System.




Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            2
Figure 5.2. Documentation Products in the System Object Model



Steps to Model the System in the System Object Model


Step 1. To start, create the system requirements statement and system use case descriptions stating
what the system as a whole must do.


Step 2. Create the system drawing to pictorially describe the system as a whole.


Step 3. Create the system block diagram to show the system with interacting systems.


Step 4. Create the system specification to collect system information.


Step 5. Create the system diagram. This diagram uses the class diagram format and will be described in
a later chapter.


Creating the System Requirements Statement


The system requirements statement describes the system and its functionality. It is often referred to as
the charter, functional requirements, or problem statement. The system requirements statement may be
created with any text editor or word processor. WithClass includes a simple text editor that can be used
to create the requirements specification. The system requirements statement may describe use cases
presented in the dynamic model.


The "Car Simulation System requirements statement" is as follows: The system shall store car
information such as gasQuantity. A user shall be able to start the car, operate the car, stop the car, add
a passenger, remove a passenger, make a cellular phone call and receive a cellular phone call.


The steps to create the system requirements statement using WithClass or text editor are listed below.


Step 1. Create a directory for Car products, e.g. c:\md Car


Step 2. Run WithClass or a text editor from Windows


Step 3. Select "File - Edit File"


Step 4. Enter the file name, e.g. c:\car\carreqs.txt


Step 5. Enter the text for the requirements



Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            3
Step 6. Select "File - Save"


Step 7. Select "Exit"


Creating the System Drawing


The drawing is a graphic, visual representation showing user and other entities listed in the system
requirements statement, interviews, and other information. The drawing for the Car Simulation System is
shown below. The dotted line shows the system boundary.




Figure 5.3. Car Simulation System Drawing

The system drawing is very important for the following reasons:



          helps identify H/W and S/W systems,
          helps identify the boundary of the system,

          helps define links (communications) between the system and H/W and S/W systems,
          helps define input and output objects that are passed to and from the system in events,
          helps maintain an object orientation versus a functional orientation,
          provides an excellent "starting point" because it is easy to understand and discuss with the client
           and users.


The steps to create the drawing using WithClass are listed below.


Step 1. Run WithClass or drawing editor from Windows


Step 2. Select "File - New"


Step 3. Select "File - SaveAs" e.g. c:\car\cardraw.omt




Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            4
Step 4. Select and place drawing icon, e.g. rectangle, circle, line, and arrow


Step 5. For labels, select the text icon, enter the text, and place the text


Step 6. Select "File - Save" to save the diagram


Step 7. Select "File - Print" to print the diagram


Creating the System Block Diagram (Inputs and Outputs)


The system block diagram is a graphic, visual representation showing the system and interacting
systems. An interacting system may be an actor from the use case description or may be equipment,
devices, or S/W programs that the actors uses to interact with the system. The block diagram has a box
for the system being modeled and a box for each interacting system. The system block diagram shows
a very high level view of the system. It shows the context and boundary of the system. Optionally, the
block diagram may show information that is passed between the system and interacting system. For
example, status and speed information are passed between the Car User and the Car Simulation System.




Figure 5.4. Car Simulation System Block Diagram

The steps to create the system block diagram using the WITHCLASS tool are listed below.


Step 1. Run the WithClass tool or a drawing editor from Windows


Step 2. Select "File - New"


Step 3. Select "File - SaveAs" e.g. c:\car\carblock.omt


Step 4. Select and place drawing icon, e.g. rectangle, circle, line, and arrow


Step 5. For labels, select the text icon, enter the text, and place the text


Step 6. Select "File - Save" to save the diagram


Step 7. Select "File - Print" to print the diagram




Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            5
CREATING THE SYSTEM DIAGRAM (OMT NOTATION) AND SYSTEM TABLE


The system diagram (OMT Notation) is a graphic representation of the system and interacting systems. This
diagram shows the system with its attributes, operations, and relationships. See the section "Creating
the Class Diagram" later in this chapter for instructions on how to create this diagram shown below.




Figure 5.5. System Diagram (OMT Notation)

This system diagram (OMT Notation) shows that the Car Simulation System with seven system
operations: start, operate, stop, makePhoneCall, receivePhoneCall, addPassenger, and removePassenger.




Figure 5.6. System Diagram (OMT Notation) for the Car Simulation System

The system table for the Car Simulation System is shown below. This table shows the system and
interacting system information.


System Table

System Name                                        System Attribute Names                              System Operation Names
CarSimulationSystem                                speed                                               start operate stop
                                                                                                       makePhoneCall receivePhoneCall
                                                   gasQuantity                                         addPassenger removePassenger
CarUser                                            --                                                  showStatus




The script (TABSYS.SCT) to generate this table is shown below.


Script to Generate a System Table


SCRIPT_NOREPEAT_HEADER_BEGIN

System Name, System Attribute Names, System Operation Names
   SCRIPT_NOREPEAT_HEADER_END



Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            6
CLASS_NAME,[NO_RETURN ATTRIBUTE_NAME],[NO_RETURN OPERATION_NAME]

CREATING THE SYSTEM SPECIFICATION AND REPORTS


The system specification lists key information about the system as a whole. We will first present
WithClass specification input form, define the information, and present a sample report. To create the
system specification, start with the system requirements statement and system block diagram. The steps
to create the system specification using WithClass are listed below.


Step 1. Run the WithClass tool or a text editor from Windows


Step 2. Select "System Specification"


Step 3. Enter the system specification information


Step 4. Select "Generate - Generate Class Report" and select a system specification report script, e.g.
RPTSYS.SCT


Step 5. Select "File - Edit File" and enter the report file name to view the report


System Specification Input Form


The following is a sample system specification form from WithClass.




Figure 5.7. Sample class specification form in WithClass.



System Specification Information




Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            7
This specification form is based upon Grady Booch's Object-Oriented Design with Applications [Booch-94].
He presents a useful system specification for the "Class Category Specification". The following is the key
information.

          System Name states the name of the system.

          Enclosing System states the name of an enclosing system if the system is a subsystem within a
           larger

          enclosing system.
          Access states the visibility of the system, e.g. public or private.

          Imports states the name of any supporting libraries.

          Description/Responsibilities states the system description and system operations. Information on
           subsystems may be included.


System Specification WithClass Script Variables


To access the system specification information, you need to know the script variable for each piece of
information in the system specification input form. The following are sample script variables from the
system specification in WithClass. A brief description states what occurs when the script variable appears
in a script. For example, when the script variable SYSTEM_NAME appears in a script, then the actual value
of the variable is printed out, e.g. Car Simulation System.


SYSTEM_NAME Prints the system name, e.g. Vehicle System

SYSTEM_ENCLOSING_SYSTEM Prints the name of the enclosing system

SYSTEM_ACCESS Prints the system access, e.g. public or private

SYSTEM_IMPORTS Prints the name of imported systems, e.g. library

SYSTEM_DESCRIPTION Prints the system description

System Specification Report and Script


The system report for Car Simulation System is shown below.

System Specification Report


System Name: Car Simulation System

System Access: Public

System Imports: C++ Libraries

Enclosing System: None

Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            8
System Description: The Car Simulation System is a simulation of a
physical car as a simple object-oriented system. The system
responds to the following user commands: start, operate, stop,
addPassenger, removePassenger, makePhoneCall, and
receivePhoneCall.


Figure 5.8. Car Simulation System Specification Report

The abbreviated script (RPTSYS.SCT) to generate this report is shown below. This script consists of
literals, e.g. System Name, and variables, e.g. SYSTEM_NAME. WithClass’s script interpreter uses "text
replacement". It prints each literal. It replaces model information for each variable and prints the value
of the variable. For example, the variable SYSTEM_NAME is replaced with value Car Simulation System in
the output report.

Script to Generate a System Specification Report


System Name: SYSTEM_NAME

System Access: SYSTEM_ACCESS

System Imports: SYSTEM_IMPORTS

Enclosing System: SYSTEM_ENCLOSING_SYSTEM

System Description: SYSTEM_DESCRIPTION

MODELING CLASSES AND OBJECTS IN THE OBJECT MODEL


The object model corresponds to the OMT Object Model. The major question is "What are the classes in
the system?" We are concerned with OO entities, basic building blocks inside a system, e.g. class and
object. We are concerned with OO relationships, links or connections between OO entities, e.g.
association, aggregation, and generalization specialization. As described below the object model consists
of the following documentation products: class diagram and class specification. A prototype may be
created.


In the object model, we define the static structure of classes. We define what is the composition of a
class in terms of attributes and operations. We specify the relationships between classes, e.g.
association, aggregation, and generalization specialization. The interaction relationship is specified in the
dynamic model. We specify constraints which are rules that restrict or limit the values that objects,
attributes, and relationships can have. A constraint is that a car can only have one motor and that the
gasQuantity has a minimum and maximum value.


The most important aspect of the object model is to describe classes and objects. Modeling classes and
objects consists of identifying, describing, and coding. In this section we will define a class and object


Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            9
and then present how to identify classes and objects.


The object model is "snapshot" of a class at a point in time. For example the object model of a Car
Simulation System consists of a class diagram, class specification, and data dictionary as shown below.


The object model is important for several reasons. First, it identifies the key entities and relationships in
a system. Classes and relationships between classes are the building blocks and connections in a system.
Second, it provides the framework of classes to build the system. Third, it ties together all the models.
Fourth, WithClass can generate an extensive amount of code based upon the object model. WithClass
generates code from the class diagram and class specifications in the object model.


Documenting Classes and Objects in the Object Model


There are many documentation products to describe classes and objects in a system in the object model.
As shown in the figure below, the object model consists of the following documentation products to
describe classes and objects: requirements statement, drawing, class diagram, class specification, and
prototype. We will create these documentation products for classes in the Car Simulation System.




Figure 5.9. Documentation products in the object model.

     In creating the object model we will accomplish the following:

          Identify classes and objects in a drawing.
          List classes and objects in a data dictionary.
          Diagram classes in a class diagram.

          Describe classes in a text class specification.
          Generate C++ code for each class.


What is a Class?


A class is a description of a group of objects with similar attributes, common operations, common
relationships (association, aggregation, and generalization specialization) and a common semantic
purpose. An attribute is a characteristic or property of an object. An attribute is typically an atomic literal,
such as integer, float, character, etc. or a set of literals, such as a string of characters. For example, an
attribute of a car is gasQuantity which is a float. An operation is a function, action or set of actions. For
example, an operation of a car is to "set gasQuantity" and "start". A relationship is a connection or link
between classes or between objects. The primary relationships are association ("has a"), aggregation
("part of"), and generalization specialization ("is a"). For example, the car class has an association
relationship with the passenger class. The car class has a generalization specialization relationship with
the vehicle class. The car class has an aggregation relationship with the motor class. The semantic purpose
of a class is the reason for being or existence of objects of the class. For example, the semantic purpose


Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            10
of objects of the car class is to provide transportation to carry users from one location to another.
Examples of classes include customer, product, order, and account.


We model a class with a class diagram, a class specification, and code as shown in the figure below.




Figure 5.10. Transition from the class diagram, to the class specification to C++ code.

What is an Object?


An object represents a person, place, or thing. An object is an instance of a class. An object has an object
name (ID) and a value for all attributes and associated objects. For example an object is Car1 with the
attribute value of gasQuantity of 14.2 gallons. In execution an object responds to messages. We model
objects in message scenarios. Objects may be diagrammed as shown in the figure below for instructional
purposes or for message tracing. The Rumbaugh OMT graphic notation for objects is shown below. The
Coad/Yourdon graphic notation has a symbol for "class&objects". This represents the class and all objects
of the class. The Booch graphic notation has a object symbol to represents objects to show the execution
of messages between objects.




Figure 5.11. Car class with car object in C++.

Identifying Classes and Objects


How do you identify classes and objects in a system? Classes define objects. Objects represent people,
places, and things. The initial question is: What are the people, places, and things in the system? It is
easy to identify physical, tangible things in a system. A drawing helps to identify physical, tangible
things. In the small Car Simulation System the following are the physical, tangible things: a car, a motor,
a cellular phone, tires, and passengers. We start with modeling physical, tangible things first because
they are the simplest to identify and to understand.


It is more difficult to identify intangible, abstract things. Ask the following: What is the important
information in the system? For example, accounts are important in a bank system. What are the


Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            11
important forms that collect information? For example, an account creation form is important in a bank
system. What are the important reports that collect information? For example, a bank transaction report
is important. What is the logical grouping of data? For example, an invoice groups together customer and
product data items.


There are several techniques to identify classes and objects. The simplest technique is to ask the
question "what are the major things in this system". Another technique to identify classes and objects by
extracting the major nouns in the requirements statement. Wifs-Brock suggests the use of class -
responsibility - collaboration (CRC) cards [Wifs-Brock-90]. Identify the major things (objects) in a
system. Assign a person to represent each major object. Walk messages through the system and identify
additional objects. Then, describe the class of each object on the CRC card.


Steps to Model Classes and Objects in the Object Model


Step 1. Start with the system requirements statement, system block diagram, and system drawing to
understand the system as a whole.


Step 2. Create the class diagram. The class diagram is a visual, graphic representation of classes showing
attributes, operations, and relationships.


Step 2a. Identify classes. A class defines objects that represent people, places, and things in the
system.


Step 2b. Identify attributes. An attribute is a characteristic, property, or component of an object. An
attribute consists of an attribute name, type/class, and value.


Step 2c. Identify operations. An operation is an action, algorithm, or set of steps that typically uses or
modifies an attribute value.


Step 2d. Identify the relationships for association, aggregation, and generalization specialization. These
relationships represent a connection or link between two classes. Defer identification of interaction
relationships (events or messages) until the dynamic model.


Step 3. Create the text specifications for each class, attribute, operation, and relationship. Generate
reports to display or print out the text specifications.


Step 3a. Create the class specification. The class specification provides text information about the class.
Class information should include class description, superclasses, access, cardinality, concurrency,
transformations and correctness assertions and other information necessary for programming or
documentation. Document constraints in the class specification.


Step 3b. Create the attribute specification. The attribute specification provides text information about each
attribute. Attribute information includes type (integer, float, string, etc.), access (private, protected,
public), initial value, minimum value, maximum value, constraints (rules), and description.


Step 3c. Create the operation specification. The operation specification provides text information about
each operation. Operation information includes the return type or class, input parameters, access
(private, protected, public), preconditions, postconditions, transformation, exceptions, and description.


Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            12
Step 3d. Create the relationship specification. The relationship specification provides text information on
each relationship such as its name, constraints (rules), and implementation features. For association and
aggregation relationships, the cardinality (1 to 1, 1 to many, many to many) is shown.


Step 4. Create or update the data dictionary. The data dictionary lists the terms and a brief description of
each term used in the model, e.g. system and classes.


Step 5. Check and update consistency with other models, e.g. the dynamic model and functional model.
Check all diagrams and text specifications for the consistent spelling, capitalization, and meaning of all
names.


Step 6. Generate the prototype - C++ source code with WithClass. Compile and execute the program in
the C++ development environment.


Step 7. After modifying the C++ source code in the C++ development environment, reverse engineer
the updated C++ to create an updated class diagram.


CREATING THE CLASS DIAGRAM



The class diagram is a graphic representation of classes and relationships. For each class it shows
attributes, operations, and relationships. There are several graphic notations to diagram classes that are
available in WithClass. A primary notation is the Rumbaugh OMT notation. The class symbol is a box with
three sections: class name, attributes, and operations. The figure below is an initial class diagram of the
Car Simulation System showing the car, motor, and passenger classes. The car class has two attributes,
gasQuantity and speed. It has three operations, start, operate, and stop. The motor class has two
attributes, rpm and displacement. It has three operations, start, operate, and stop. The passenger class
has two attributes, name and seatBeltStatus. It has two operations, fastenSeatBelt and
unfastenSeatBelt. Relationships will be shown in a subsequent chapter.




Figure 5.12. Class diagram of the car, motor, and passenger classes.

Creating a class diagram is an iterative process of starting with a very basic diagram and then adding
more and more details. The following are the general steps to create a class diagram.


Step 1. Identify classes, e.g. car, motor, passenger, cellular phone.


Step 2. Enter a few attributes, e.g. speed and gasQuantity in the Car class.


Step 3. Enter a few operations, e.g. start, operate, and stop in the Car class.


Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            13
Step 4. Enter a few relationships, e.g. aggregation between the Car class and Motor class.


Step 5. Repeat the process adding more classes, attributes, operations, and relationships.


The steps to create the class diagram using WithClass are listed below.


Step 1. Run WithClass from Windows


Step 2. Select "File - New"


Step 3. Select "File - SaveAs" e.g. c:\car\car.omt


Step 4. Select Class Icon from the drawing toolbar or menu item


Step 5. Place the class symbol on the diagram


Step 6. Enter the class name, e.g. Car


Step 7. Enter each attribute, e.g. gasQuantity


Step 8. Enter each operation, e.g. start and operate


Step 9. Select a relationship icon, e.g. Aggregation and connect two classes


Step 10. Enter a relationship name, e.g. currentMotor


Step 11. Select "File - Save" to save the diagram


Step 12. Select "File - Print" to print the diagram


In creating a class diagram with WithClass, information may be entered directly on the diagram or into a
class form as shown in the figure below.




Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            14
Figure 5.13. Sample WithClass tool class entry form.

The class diagram is the single most important diagram in OO modeling. It graphically shows the classes
and relationships in a system. It should be understandable to developers and programmers. It is the
basis to automatically generate source code. It shows the structure of a system. It is useful to evaluate
the software quality factors of correctness, reliability, modifiability, and reusability. It is useful to show
the client and describe the system in terms of classes and relationships. The complete class diagram for
the Car Simulation System is shown in Chapter 3.



CREATING THE CLASS SPECIFICATION AND REPORTS


The class specification lists important text information for each class. It is a key documentation product
to document a class. Its purpose is to state adequate information to document and to program each
class. We will first present WithClass’s specification input form, define the specification information, and
present a sample report. The steps to create the class specification using WithClass are listed below.


Step 1. Run WithClass from Windows


Step 2. Select "File - Open" e.g. c:\car.omt


Step 3. Double click on a class, e.g. Car


Step 4. Select "Class Specification" to bring up the class specification form




Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            15
Step 5. Enter class documentation, e.g. description, access, concurrency, cardinality, persistence, etc.


Step 6. Select "Generate - Generate Class Report" and select a report script, e.g. RPTCLASS.SCT or
TABCLASS.SCT


Step 7. Fill in additional information on topics not listed in the remarks section.


Step 8. Select "File - Edit File" and enter the report file name to view the report


The class specification is important to capture and organize important information to document the
system. It requires that you examine each class from many points of view, such as access, cardinality,
persistence, etc. that you might overlook without the class specification. It assists you to investigate
these different aspects, such as access and persistence. It promotes communication with the client to
resolve ambiguous requirements.


Class Specification Input Form


The figure below is a sample class specification input form from WithClass for the Car class.




Figure 5.14. Sample class specification form in WithClass.

    Class Specification Information


    The description of the each information item is listed below. form of the class specification used in
    this tutorial is adapted from the Booch class specification presented in Object-Oriented Design with
    Applications by Grady Booch [Booch-94].


               Description: general class information, purpose, roles, essential behavior, and responsibilities.

               Access: indicates the visibility or export control of a class, e.g. exported, private, or imported
                relative to the enclosing system or subsystem. Access is also known as export control or



Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            16
                visibility.

               Cardinality: indicates how many objects (instances) of the class are permitted, e.g. 0, 1, or N
                (a number).

               Concurrency: documents if objects of the class are sequential or concurrent. Alternatives are
                sequential, concurrent, blocking or active. A sequential class defines objects that operate in a
                sequential fashion, e.g. first one object is active (operating) then another object is active,
                etc. A concurrent class defines objects that may operate concurrently in parallel with each
                other. For example, several concurrent objects may be operating at the same time.
                Concurrent objects may be created with a concurrent language, e.g. Ada or Concurrent
                C/C++ or with an operating system. Concurrent objects are especially important in real time
                systems and client server applications.

               Persistence: indicates if objects of the class are transitory or persistent. Transitory objects lose
                their values when the program ends. Persistent objects retain their values when the program
                ends. Persistence may be implemented by writing an object to a file or a relational data base
                management system. Persistence may be directly implemented with an object-oriented data
                base management system.

               File Name: indicates the file name for the class. If blank, then the class name becomes the file
                name. This field is useful to reduce a long class name to eight characters as required by the
                MS/DOS file system.

               Author's Initials: indicates the initials of the author or developer responsible for the class.
               Library Base Class: indicates the name of the Library Base Class that is the superclass of the
                current car. For example, in Visual C++ it is often necessary to make the class a derived
                class (subclass) of the MFC class CObject for use with the CObList and other collection
                classes.

               Invariant: indicates the invariant of the class. An invariant is a rule or constraint that specifies
                the correctness of the class. Invariants are a part of the Eiffel Language.

               User1, User2, and User3 provide user defined fields for comments or other information.


          The following are additional information items that may be collected for each class.

               System/Subsystem states the enclosing system or subsystem.

               Superclasses states the superclasses of the class.

               Implementation denotes language specific implementation information such as names of keys
                or generic parameters.
               Space documents the execution size of the objects of the class, e.g. relative units (small,
                large) or actual memory units (bytes).

               Abstract/Concrete documents the form of the class, e.g. abstract or concrete. An abstract class
                has no direct instances (objects). A concrete class has direct instances (objects).

               Generic Parameterized (Yes/No) documents if the class is a generic parameterized (template)
                class. A generic parameterized class serves as a template to create other classes. A generic
                parameterized class must have a generic parameter before it is instantiated, e.g.
                ParameterizedStack <int>.
               Generic Parameter documents the generic parameter of a generic class, e.g. int, float, String,
                etc.

               Indexed (Yes/No) documents if the class is indexed. An indexed class is a collection class with a
                lookup index, e.g. EmployeeList with the index of employeeNumber.
               Index Attributes (Type and Name) documents indexes.



Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            17
               Applicable Documents states file names and other references for drawings, block diagrams,
                class diagrams, state diagrams, source code files, etc.

               Remarks includes other relevant information. If applicable, include requirements, user
                interface, storage, and remote connectivity information.



Class Specification WithClass Tool Script Variables


To access the class specification information, you need to know the script variable for each piece of
information in the class specification input form. The following are sample script variables from the class
specification in WithClass. A brief description states what occurs when the script variable appears in a
script. Class variables access class information.


CLASS_NAME Prints class name

CLASS_CARDINALITY Prints the class cardinality

CLASS_PERSISTENCE Prints the class persistence, e.g. Persistent or Transitory

CLASS_CONCURRENCY Prints the class concurrency

CLASS_ACCESS Prints the class access

CLASS_DESCRIPTION Prints the class description

CLASS_FILENAME Prints the class file name if assigned

CLASS_AUTHOR_INITIALS Prints the author initials if assigned

CLASS_INVARIANT Prints the invariant (rule)

CLASS_LIBRARY_BASE_CLASS Prints name of library base class

CLASS_PERSISTENT Prints the keyword persistent if the class is persistent for

generating code for Object-Oriented DBMS's

CLASS_USER1 Prints a user comment

CLASS_USER2 Prints a user comment



Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            18
CLASS_USER3 Prints a user comment


Class Specification Report and Script


The Car class specification report is shown below.


Class Specification Report

Class Name: Car

Class Cardinality: 1

Class Persistence: transitory

Class Concurrency: sequential

Class Access: private

Class File Name: Car

Class Author Initials: RCF

Class Invariants: speed <= maximumSpeed

Class Description: Car Class stores car information for a user to
start a car and get and set car information.

Class Include Files: N/A

Class User1: N/A

Class User2: N/A

Class User3: N/A


Figure 5.15. Car class specification report

An abbreviated script (RPTCLASS.SCT) to generate this report from the class diagram is shown below.




Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            19
Script to Generate a Class Report


Class Name: CLASS_NAME

Class Cardinality: CLASS_CARDINALITY

Class Persistence: CLASS_PERSISTENCE

Class Concurrency: CLASS_CONCURRENCY

Class Access: CLASS_ACCESS

Class File Name: CLASS_FILENAME

Class Author Initials: CLASS_AUTHOR_INITIALS

Class Invariant: CLASS_INVARIANT

Class Description: CLASS_DESCRIPTION

Class Include Files: [NO_RETURN INCLUDE_FILE ,]

Class User1: CLASS_USER1

Class User2: CLASS_USER2

Class User3: CLASS_USER3

The following is sample additional information that could be collected for the Car class. This information
could be stored in the Class User fields.


System/Subsystem: Car Simulation System (Program)

Superclasses: Vehicle

Implementation: C++ class

Space: small

Abstract/Concrete: abstract

Generic (Yes/No): no


Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            20
Generic Parameter: N/A

Indexed (Yes/No): no

Index Attributes (Type and Name): N/A

Index: N/A

Applicable Documents: Block Diagram - c:\car\carblock.omt; Class Diagram -
c:\car\car.omt; State Diagram - c:\car\carstate.sm; C++ source code c:\car\car.cpp and
c:\car\car.h

Remarks: Transformation and correctness assertions are to be added.


Class Table and Script


The table listing classes, attributes, operations, and relationship traversal names is shown below.


Class Table
Class Name                            Attribute Name                         Relationship                           Operation Names
                                                                             Traversal Path
                                                                             Names
Car                                   gasQuantity speed                      --                                     start operate stop
Motor                                 rpm displacement                       --                                     start operate stop
Passenger                             name seatBeltStatus                    --                                     fastenSeatBelt
                                                                                                                    unfastenSeatBelt


The script (TABCLASS.SCT) to create this table from the class diagram is shown below. This generates a
comma delineated text file to be imported into a Windows word processor.


Script to Generate a Class Table


SCRIPT_NOREPEAT_HEADER_BEGIN

Class Name, Attribute Names, Relationship Traversal Path Names, Operation Names
   SCRIPT_NOREPEAT_HEADER_END

CLASS_NAME,[NO_RETURN ATTRIBUTE_NAME],[NO_RETURN
  ASSOCIATION_ONE_NAME][NO_RETURN
  ASSOCIATION_MANY_NAME][NO_RETURN
  AGGREGATION_ONE_NAME][NO_RETURN
  AGGREGATION_MANY_NAME],[NO_RETURN OPERATION_NAME]


Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            21
CREATING THE DATA DICTIONARY LISTING TERMS


The data dictionary lists and describes key OO entities and terms. The purpose of the data dictionary is
to aid the model builder or model user to lookup and understand names and terms used in the model.
As a minimum the data dictionary lists and describes the classes in a system. The data dictionary may
list and describe all modeling entities in the system including classes, objects, attributes, operations,
relationships, etc. The data dictionary listing terms may be created using WithClass or any word
processor. The steps to create the data dictionary listing terms using WithClass are listed below.


Step 1. Run WithClass or text editor from Windows


Step 2. Open a class diagram, e.g. CAR.OMT


Step 3. Select "Generate - Generate Report" and select the data dictionary report script, e.g.
DDCLASS.SCT


Step 4. Select "File - Edit File" and enter the report name to view the data dictionary


Step 5. If desired, import the report into a Windows word processor to sort and format the report.


The initial data dictionary for the Car Simulation System is as follows. Updates to the data dictionary will
be provided in each of the models.

Data Dictionary from the Object Model

Entity Name                    Type of Entity                                Enclosing Class                        Entity Description
Car                            Class                             --                                  Car transports people and items
gasQuantity                    Attribute                         Car                                 Amount of gasoline in the car
                                                                                                     available for use
speed                          Attribute                         Car                                 Velocity of the car
Start                          Operation                         Car                                 Starts the car
Operate                        Operation                         Car                                 Operates the car at a speed
Stop                           Operation                         Car                                 Stops the car
Motor                          Class                             _-                                  Motor runs the car
Rpm                            Attribute                         Motor                               Revolutions per minute
Displacement                   Attribute                         Motor                               Size of motor
Start                          Operation                         Motor                               Starts the motor
Operate                        Operation                         Motor                               Operates the motor at a set rpm
Stop                           Operation                         Motor                               Stops the motor
Passenger                      Class                             __                                  Passenger in the car
Name                           Attribute                         Passenger                           Name of the passenger
SeatBeltStatus                 Attribute                         Passenger                           Status of the seat belt—fastened
                                                                                                     or unfastened
FastenSeatBelt                 Operation                         Passenger                           Fastens the seat belt
UnfastenSeatBelt               Operation                         Passenger                           Unfastens the seat belt


Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            22
The abbreviated script (DDCLASS.SCT) to generate the data dictionary from the class diagram is shown
below. This script prints out class, attribute, operation, and relationship information from a class
diagram. Literals are lower case. Variables are upper case. The repeat [ ] operation is to repeat printing
multiple values.


Script to Generate a Data Dictionary


SCRIPT_NOREPEAT_HEADER_BEGIN

Entity Name, Type of Entity, Enclosing Class, Entity Description

SCRIPT_NOREPEAT_HEADER_END

CLASS_NAME, Class, --,CLASS_DESCRIPTION

[ATTRIBUTE_NAME, Attribute, CLASS_NAME, ATTRIBUTE_DESCRIPTION]

[OPERATION_NAME, Operation, CLASS_NAME, OPERATION_COMMENT1]

[ASSOCIATION_ONE_NAME, Traversal Path for 1:1 Association, CLASS_NAME,]

[ASSOCIATION_MANY_NAME, Traversal Path for 1:M Association, CLASS_NAME,]

[AGGREGATION_ONE_NAME, Traversal Path for 1:1 Aggregation, CLASS_NAME,]

[AGGREGATION_MANY_NAME, Traversal Path for 1:M Aggregation, CLASS_NAME,]

In this tutorial there will be a separate data dictionary script for each major model. To create a system
wide data dictionary, you must consolidate the separate data dictionary reports into a single file in a
Windows word processor. The following are the various data dictionary reports used in this tutorial.

Table 5.1. Listing Diagrams and Scripts in OO Models

Model                                              Diagram
System Object Model                                System Diagram (OMT Notation)                       DDSYS.SCT
System Dynamic Model (Events)                      System Interaction Diagram                          DDSYSIN.SCT
System Dynamic Model (States)                      System State Diagram                                DDSTATE.SCT
System Functional Model                            N/A                                                 N/A
Object Model                                       Class Diagram                                       DDCLASS.SCT
Dynamic Model (Messages)                           Object Interaction Diagram                          DDOBJ.SCT
Dynamic Model (States)                             State Diagram                                       DDSTATE.SCT
Functional Model                                   N/A                                                 N.A




Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            23
CODING CLASSES AND OBJECTS IN C++


In this section we will present C++ classes and objects. We'll present how to generate C++ code with
WithClass. Then we'll present how to compile, link, and execute the C++ source code in a C++
development environment. To understand C++, we need to transition from the OO modeling terms to
C++ terms. The table below is the comparison of the OO modeling terms and C++ terms.

Table 5.2. OO modeling and C++ terms.


OO Modeling C++
system program with a single main function
class class
object object
attribute data member
operation function member


A program in C++ consists of a single main function with preprocessor directives (include statements),
global variable and function declarations, and classes. The simplest C++ program is shown in the listing
below. Later we'll show many programs with classes.


Listing 5.1. Simplest C++ program.

//Sample comment - Comments are placed after the //

#include <iostream.h> //preprocessor directive to include the file //iostream.h this file is required for
   C++ //input and output with

//the cout << "Hello, world" statement

int main ()

{ cout << "Hello, world";

return 0;

}

----------------------Sample Output---------------------------

Hello, world

(a) C++ Classes


A class in C++ is an entity that encapsulates data members and function members. A data member


Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            24
implements an attribute or an association/aggregation relationship. A function member implements an
operation. The C++ form (syntax) of data members and function members will be presented in the next
chapter.


A class in C++ consists of a class declaration and a member function definitions. A class declaration
introduces data members and function members. It is typically placed in a header file (.h file). A member
function definition is the complete C++ source code for the function. Member function definitions are
typically placed in a source code file (.cpp file). WithClass tools generate function definitions for very
simple functions such as functions to get and set attribute values. WithClass tools generate a fill-in code
template for other function definitions. A programmer must fill in C++ statements to complete the
function definition. The form of a class declaration is as follows.

General Form of a Class Declaration

class <class name>

{

private: // private keyword may be omitted

<private data and

function member declarations>

protected:

<protected data and

function member declaration>

public:

<public data and function

member declarations>

};

An example of a very simple class declaration is shown in the listing below.


Listing 5.2. Very simple class in C++.

class Car

{




Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            25
private: // private keyword may be omitted

float gasQuantity;

public:

Car (); //Constructor member function to create objects

//of the car class

void start (); //Member function declaration

~Car (); //Destructor member function to delete objects

//of the car class

};

The class name identifies the name of the class, such as Car. The class declaration has data members to
represent attribute and function members to represent operations. Data members and function members
have private, protected, or public access (visibility). A private member declaration may be a data
member representing an attribute or function member representing an operation. A private member is
accessible from within the class only. "float gasQuantity" is a private data member for the attribute
gasQuantity. A public member is accessible from outside the class. A public member may be a data
member or a function member. "start ()" is a public data member that is accessible both inside and
outside the class. A protected member is accessible from within the class or from a subclass.


A C++ class has two special types of member functions, constructors and destructors. A constructor
initializes an object of a class. It is invoked whenever an object of the class is created. The constructor
has the same name as the class, such as Car (). A destructor deletes an object. WithClass generates
default C++ constructors and destructors.


(a) Objects in C++


A C++ object is an identifiable region of memory that can hold a value or set of values. An object has a
name (ID), a data type or class, and a value or set of values. The following are examples of objects of
fundamental data types such as integer, float, and character: int anInterger, float aFloat, char
aCharacter. The following are examples of objects of user defined classes: Car car1, Motor motor1,
Passenger passenger1.


There are three primary ways to create an object in C++: automatic (local) object, dynamic object, and
static object. An automatic C++ object exists within a scope of an enclosing block ( {} ), function, class, or
file (global). Memory is allocated as soon as the object is created. Memory is deallocated automatically
when the object goes out of scope. This occurs when the execution of the program leaves the enclosing
block ( {} ), function, class, or file. An automatic object is the simplest to create. It is also known as a



Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            26
local object. The guideline is to create an automatic object unless you specifically require a dynamic or
static object. For example, use a dynamic object not an automatic object if you need to dynamically
create and delete objects at runtime. An automatic object declaration has the following form (syntax)
with examples:


<class or type name> <object name> ;

Car car1;

Motor motor1;

The second way to create objects in C++ is to create dynamic objects. A dynamic C++ object is an object
with a pointer identifier. The object name is a pointer. A pointer is a C++ variable that stores the address
of an object or other stored entity. The pointer points to the object (region of storage) that it identifies.
Dynamic objects are important to be able to create and delete objects at runtime (dynamically) during
the program execution. For example, a car may have one to six passengers. Using dynamic objects, we
can create each passenger as needed during program execution. A dynamic object declaration has the
following form (syntax) with examples:


<class or type name> * <object name> = new <class name>;

Passenger *passenger1 = new Passenger;

Passenger *passenger2 = new Passenger;

The programmer has the responsibility to allocate and deallocate memory. Allocation is done with the
new keyword. Deallocation is done with the delete keyword. An example of deleting a dynamic object is
delete passenger2;


The third way to create objects in C++ is to create static objects. A static object exists for the duration of
the program. Memory is allocated as soon as the object is created. Static objects are used to create class
attributes which store information applicable to all objects of the class. A static object declaration has the
following form (syntax) with an example:


static <class or type name> <object name>;

static int maximumNumberPassengers;

The listing below shows a very simple main function that declares two objects.


Listing 5.3. Simple main function that declares two objects.

----------------Main Function Definition - main.cpp ----------

#include "car.h"


Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            27
int main ()

{

Car car1; //invokes constructor to create

//automatic car1 object.

Car *car2 = new Car; //invokes constructor to create

//dynamic car2 object.

delete car2; //deletes car2 object by

//deallocating memory.

return 0;

}

(a) Coding and Testing a Class in C++


WithClass may be used to automatically generate class declarations (.h files) and fill-in templates for
function member definitions (.cpp files). WithClasss greatly facilitates coding because it generates the
C++ class declarations and fill-in code templates for function member definitions. WithClass uses the
class diagram and class specifications to generate the code. The steps to generate C++ code with
WithClass are as follows:


>> Run the WithClass tool.


>> Complete the class diagram and class specification.


>> Select C++ code generation from a menu or tool bar.


>> Save the generated C++ files in a designated directory.


To code a class in C++ code the class declaration in the header file, code the function definitions in the
source code file, and code a main (test) function in a source code file. The listing has an #ifndef
conditional directive that was generated by the WithClass tool. The #ifndef directive tests whether an
identifier is currently in force. These directives are required for compilation to avoid "multiple
declaration" compilation errors. In the diagram below, key aspects of the class declaration are shown on
the left. This information class diagram shows that a class declaration should have a #define statement,
a class name, data members, a constructor, function members, and a destructor. The class diagram on



Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            28
the right shows sample C++ identifiers that are also in the code listings.




Figure 5.16. Class Diagram showing class name, attribute names, and operation names.

The following are key points for the class declaration:


- provide #ifdefs, #define, and #endif compiler directives to avoid multiple class
declarations, e.g. #define __CAR_H,

- declare the class, e.g. class Car

- declare data members, e.g. int speed;

- declare function members, e.g. void operate (int aSpeed)

- define a default constructor to initialize an object with default data member values in the
initializer list, e.g. Car () : speed (0) { }

- define a default destructor to deinitialize an object, ~Car () {}

In the function definitions, define functions, e.g. void Car::operate (int aSpeed) { speed =
aSpeed; }. In the main (test) function definition, define a main (test) function to include the class
declaration header file, create an object of the class and to invoke an operation. The complete code
listing is shown below.




Figure 5.17. Car class diagram.

Listing 5.4. Complete compiled version of the car program.

(a) C++ Class Declaration - Header File

//////////////////////////.h file//////////////////////////////////////////////

#ifndef __CAR_H



Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            29
#define __CAR_H

class Car

{ int speed; //Data member

public:

Car () : speed(0) { } //Default constructor uses Initializer List

void operate (int aSpeed) ; //Function member

~ Car ( ) { } //Destructor

};

#endif

(b) C++ Source Code File


//////////////////////////.cpp file//////////////////////////////////////////////


#include "Car.h"


void Car::operate(int aSpeed)


{ speed = aSpeed; //Statement added after code generation


}


(c) C++ Main Function

//////////////////////////main.cpp file/////////////////////////////////////////


#include "Car.h"


int main ()


{


Car car1; //Invokes default constructor


car1.operate(55); //Invokes operation function


return 0; //Default destructor is invoked


Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            30
}


WithClass generated two files from the class diagram: car.h and car.cpp. The car.h file contains the car
class declaration with the data member speed and the function member operate. The car.cpp file
contains the function definition for the operate function member. It is a fill-in code template that may be
updated with C++ statements. WithClass is helpful for generating class declarations (.h files) and fill-in
templates for function member definitions (.cpp files). However, it is just a start. All the algorithmic
details must be coded in function member definitions.


Generating C++ Code Using a Code Generation Script


To generate the C++ class declaration and function definitions, we used two scripts. A class declaration
script generated the Car.h file and a function definition script generated the Car.cpp file. We created the
main.cpp manually. The two scripts are shown below.

Script to Generate the C++ Class Declaration - Header File


//////////////////////TRUNCATE_EIGHT$CLASS_NAME.h file/////////////////////////

#ifndef __$CAPITALIZE_ALL$TRUNCATE_EIGHT$CLASS_NAME$_H

#define __$CAPITALIZE_ALL$TRUNCATE_EIGHT$CLASS_NAME$_H

class CLASS_NAME[NO_RETURN NO_REPEAT: NO_REPEAT public BASE_CLASS
   ,DELETE_LAST_SYMBOL]

{ [ATTRIBUTE_TYPE ATTRIBUTE_NAME$; //Data member]

public:

CLASS_NAME ()

[NO_RETURN NO_REPEAT:
  ATTRIBUTE_NAME(ATTRIBUTE_INITIAL_VALUE),DELETE_LAST_SYMBOL]

{

}

[OPERATION_RETURN_TYPE OPERATION_NAME (CPP_OPERATION_PARAMETERS);

]

~ CLASS_NAME ( ) { } //Destructor



Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            31
};

#endif

Script to Generate the C++ Function Definitions -.cpp File


////////////////////TRUNCATE_EIGHT$CLASS_NAME.cpp file///////////////////////////

#include "TRUNCATE_EIGHT$CLASS_NAME.h"

[

OPERATION_RETURN_TYPE
  CLASS_NAME::OPERATION_NAME(CPP_OPERATION_PARAMETERS)

{

OPERATION_CODE

}

]

Using a C++ Development Environment


Once the source code has been automatically generated, then the source code can be compiled, linked,
and executed in a C++ environment, such as the Borland C++ environment. With this environment it is
necessary to create a project file which is a list of the source code files (.cpp files) in the program.
WithClass automatically creates the project file. A simple project file consists of the main.cpp and car.cpp
files.


The steps compile, link, and execute the program in a C++ environment (Borland C++) are as follows:


>> Run the C++ environment from Windows (Borland C++ BCW).


>> Create or select the project file, e.g. carproj.prj


>> Add the applicable source code files, e.g. car.cpp and main.cpp.


>> Check the environment options - directories to ensure all included files can be accessed.


>> Update the generated C++ code as required to add function calls, other #include directives, member
function definitions, etc prior to compiling.




Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            32
>> Compile, link, and execute to test the program.


WithClass automatically generates a project file or make file. A project is a set of files needed to create
an executable file. With Borland C++ you must have a project file when you have multiple source code
files (.cpp files). The project file is the list of the source files (.cpp files) in the project. In the very simple
program below, the project file has car.cpp and main.cpp in it. Projects that use the Borland
ObjectWindows Library (OWL) must have owl.def in the project file. Projects that use resource files (.rc
files) from a GUI builder such as the Borland Resource Workshop must have the resource files (.rc files)
in the project file. Resource files are used to create GUI elements such as dialog boxes.


After automatic code generation, the C++ code must be updated to execute in a C++ development
environment. A main function must be added and the function definitions must be updated with C++
statements.


Using Include File Directives


Additionally, it is necessary to ensure that all the appropriate files are included. To include a file, enter
the precompiler include directive such as #include "car.h" in the main function file. The preprocessor
removes the #include line and conceptually replaces it with the entire text of the included file at that
point in the source code. WithClass will automatically generate include directives. However, if you use
library functions or classes, then you must enter the appropriate include directive. Examples are
#include <iostream.h> and #include <strng.h> to include the appropriate header files. The following
are several general guidelines to include files.


          a source code file (.cpp file) of function member definitions must include the class declaration
           header file (.h file), such as car.cpp includes car.h;

          a header file (.h file) must include the header file of user defined or library data members, such
           as passenge.h includes strng.h and car.h includes motor.h. See the chapter on relationships.

          a header file (.h file) of a C++ derived class (subclass) must include the header file of all C++
           base classes (superclasses). See chapter on relationships.

          any file must include the header file of any library classes or functions, such as main.cpp
           includes iostream.h for basic IO.



          The following files are in the CPP program shown in the figure below:
          main.cpp for the main function
          car.h for the car class declaration
          car.cpp for the function definitions of the car class

          motor.h for the motor class declaration
          motor.cpp for the function definitions of the motor class.




Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            33
Figure 5.18. Diagram showing the files in the preliminary program and the include directives.


SUMMARY


Modeling the system and classes and objects in a system is an important part of OO modeling. A system
is an entity that can be treated as a unit that is composed of simpler components that work together to
perform a function. A system may have other systems as subsystems. A system has classes and objects.
A class is a description of a group of objects with similar attributes, common operations, common
relations (association, aggregation, and generalization specialization), and a common semantic purpose.
An object is an instance of a class. An object represents a person, place, or thing. An object has a value
for each of its attributes. Modeling classes and objects consists of creating documentation products
including a drawing, data dictionary, class specification, class diagram, and source code. WithClass is
useful to create documentation products and to generate the initial source code. With C++, the
generated source code must be updated with main function and C++ statements in function definitions.


Please email all comments and questions to techsales@microgold.com




Modeling and Programming OO Entities: Car Simulation System – Modeling the System and Classes - Microgold Software Inc. www.microgold.com
                                                                                                                                            34

				
DOCUMENT INFO
Description: Class Diagram for a Hospital Management System document sample