Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Mastering UML with Rational Rose 2002.pdf
Скачиваний:
137
Добавлен:
02.05.2014
Размер:
9.68 Mб
Скачать

Chapter 13: ANSI C++ and Visual C++ Code Generation and Reverse Engineering

If the appropriate component already exists in the Rose model, select the C++ component and project for the C++ class. Otherwise, leave these fields blank.

5.

In the COM section, enter the name of the CoClass and interface. When you press OK, Rose will create the CoClass and interface class and set their relationships.

6.

If the appropriate component already exists in the Rose model, select the COM component and project for the COM classes. Otherwise, leave these fields blank.

Note In Rose 2002, reverse−engineered MIDL components will be added to the model as components with the <<MIDL>> stereotype. Any classes with a stereotype of <<coclass>> or <<interface>> will be automatically mapped to the MIDL components.

Generated Code

In the following sections, we'll take a look at the C++ code generated for a class, an attribute, an operation, and for the different types of relationships between classes. In each of these sections, we'll include some sample code to give you an idea of what will be generated from your Rose model.

Rose uses the information in the specifications of the model elements when generating code. For example, it will look at the different specifications for a class (visibility, attributes, operations, and so on) when generating code for the class.

Code Generated for Classes

Let's begin by looking at the code generated for a typical class. A class in your object model will become a C++ class when you generate code. Each class will generate code similar to the following:

Class TheClass

{

public:

TheClass();

~TheClass();

};

However, a great deal of additional information will also be generated in the code. (We'll look at a complete header and implementation file shortly.) All of the attributes, operations, and relationships of the class will be reflected in the generated code. The major elements generated for each class include:

The class name

The class visibility

A constructor for the class (optional)

A destructor for the class (optional)

435

 

Chapter 13: ANSI C++ and Visual C++ Code Generation and Reverse Engineering

Get() and Set() operations for each attribute (optional)

Class documentation

Attributes

Operations

Relationships

Each class in the model will generate two C++ files, a header file, and an implementation file. Each file will be named using the class name. For example, an Employee class will generate an Employee.h file and an Employee.cpp file.

When generating code with ANSI C++, Rose will use the package structure you established in the Component view of your model to generate the appropriate directories. A directory will be created for each package in the model. Within each of the directories Rose creates, there will be the .cpp and .h files for the classes in that package. If you have not created components and packages in the Component view, Rose will use the package structure in the Logical view to create the directory structure.

Much of the information in your Rose model will be used directly when generating code. For example, the attributes, operations, relationships, and class name of each class will directly affect the code generated. Other model fields, such as the documentation entered for the class, will not directly affect the code. These field values are created as comments in the generated code.

Table 13.8 lists the fields available in the class specification window and notes which of these fields will directly affect the code generated.

Table 13.8: Effect of Class Specifications on Generated Code

Fields

Effect on Code

Name

Name in model will become class name

Type

Directly affects the type of class created

Stereotype

No effect

Export Control

Directly affects the class visibility

Documentation

Becomes a comment

Cardinality

No effect

Space

No effect

Persistence

No effect

Concurrency

No effect

Abstract

Creates an abstract class

Formal Arguments

Formal arguments are included in the code for a parameterized class

Operations

Generated in code

436

Chapter 13: ANSI C++ and Visual C++ Code Generation and Reverse Engineering

Attributes

Generated in code

Relationships

Generated in code

Let's look at the code generated for the following class:

Generated Header File

The following code is the header file that was generated for this class.

#ifndef FLIGHT_H_INCLUDED_C6AD4E5A #define FLIGHT_H_INCLUDED_C6AD4E5A

//##ModelId=39528510031C

//##Documentation

//## This class holds information about airline flights. class Flight

{

public:

//##ModelId=3952853300EC

boolean AddPassenger(int PassengerID);

//##ModelId=3952853B013D

boolean RemovePassenger(int PassengerID);

//##ModelId=3952854302B1 int CancelFlight();

private:

//##ModelId=395285160108 int FlightNumber;

//##ModelId=395285190365 date DepartureDate;

//##ModelId=3952851E0037 string DepartureCity;

//##ModelId=3952852B009A string ArrivalCity;

};

#endif /* FLIGHT_H_INCLUDED_C6AD4E5A */

Let's begin by discussing the annotations that Rose inserts into the source code. Rose adds these model IDs so that code can be modified and regenerated (round−trip engineering) without overwriting any changes. Use of

437

Chapter 13: ANSI C++ and Visual C++ Code Generation and Reverse Engineering

the model IDs is optional. To turn model IDs on or off, open the ANSI C++ component specification for the component. On the Style tab, set model IDs to the Never Generate Model IDs option.

The generated file will include header information for the attributes and operations of the class. It can also include headers for a constructor, a destructor, a copy constructor, and other standard methods. To include any of these, open the ANSI C++ Class Customization window and select the method(s) to generate. Or, if you are using Visual C++, use the Model Assistant.

As you can see, Rose includes the visibility, parameters, parameter data types, and return type for each operation. Although Rose cannot code the operation itself, it does provide a "skeleton" framework for the programmers to use. You can generate code for the operation by adding default code in the operation's InitialCodeBody property.

Generated Implementation File

The other file generated by Rose is an implementation file, with the default extension .cpp. The following is the implementation file generated along with the header file we just examined.

#include "c:/Flight.h"

//##ModelId=3954129803B3

boolean Flight::AddPassenger(int PassengerID)

{

}

//##ModelId=395412A00093

boolean Flight::RemovePassenger(int PassengerID)

{

}

//##ModelId=395412A80121 int Flight::CancelFlight()

{

}

Again, notice that Rose includes the operation visibility, parameters, parameter data types, and operation return type in the generated code. Had we selected the option to generate the constructor, destructor, or other standard methods (by right−clicking the class and selecting ANSI C++ Code Customization), these would be

438