Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
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

included in the code as well. With Visual C++, use the Model Assistant to determine whether or not to generate these methods.

Code Generated for Attributes

Aside from the class itself, Rose will generate the attributes for the class. For each attribute, Rose will include:

Visibility

Data type

Default value

Get operation (optional)

Set operation (optional)

For a given attribute, Rose will generate code similar to the following:

Class TheClass

{

public:

int PublicAttribute;

int GetPublicAttribute(); int GetProtectedAttribute(); int GetPrivateAttribute();

void set_PublicAttribute (int value); void set_ProtectedAttribute (int value); void set_PrivateAttribute (int value);

protected:

int ProtectedAttribute;

private:

int PrivateAttribute;

};

A great deal more, including comments and Rose identifiers, will be generated in a full header and implementation file. Let's look in detail at the code generated for the following class:

439

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

In this example, we opened the ANSI C++ Class Customization window and selected the option to generate Get and Set methods for the FlightNumber attribute. Using Visual C++, you can use the Model Assistant to control whether Get and Set methods are created. The header file now reads as follows:

#ifndef FLIGHTHINCLUDEDC6ABFE6A #define FLIGHTHINCLUDEDC6ABFE6A

//##ModelId=39541274036B class Flight

{

public:

//##ModelId=3954129803B3

boolean AddPassenger(int PassengerID);

//##ModelId=395412A00093

boolean RemovePassenger(int PassengerID);

//##ModelId=395412A80121 int CancelFlight();

//##ModelId=395413D80055

int get_FlightNumber() const;

//##ModelId=395413D800BA

void set_FlightNumber(int left);

private:

//##ModelId=3954127801F4 int FlightNumber;

//##ModelId=3954128202DF date DepartureDate;

//##ModelId=395412860122 string DepartureCity;

//##ModelId=3954128E0097 string ArrivalCity;

};

#endif /* FLIGHT_H_INCLUDED_C6ABFE6A */

440

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

The implementation file also includes the Get and Set methods. Note that Rose will include more than just the method signature; it will actually code these methods for you. The implementation file for the Flight class is shown here:

#include "c:/Flight.h"

//##ModelId=3954129803B3

boolean Flight::AddPassenger(int PassengerID)

{

}

//##ModelId=395412A00093

boolean Flight::RemovePassenger(int PassengerID)

{

}

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

{

}

//##ModelId=395413D80055

int Flight::get_FlightNumber() const

{

return FlightNumber;

}

//##ModelId=395413D800BA

void Flight::set_FlightNumber(int left)

{

FlightNumber = left;

}

Code Generated for Operations

Rose generates code for each of the operations in the class. For each operation, the generated code includes the operation name, the parameters, the parameter data types, and the return type. Each operation will generate code similar to the following:

Class TheClass

{

public:

void PublicOperation();

protected:

void ProtectedOperation();

private:

void PrivateOperation(); };

We'll examine the code generated for the following class:

441

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

In the header file, Rose will generate the signatures for the operations:

#ifndef FLIGHT_H_INCLUDED_C6ABEA96 #define FLIGHT_H_INCLUDED_C6ABEA96

//##ModelId=39541274036B class Flight

{

public:

//##ModelId=3954129803B3

boolean AddPassenger(int PassengerID);

//##ModelId=395412A00093

boolean RemovePassenger(int PassengerID);

//##ModelId=395412A80121

//##Documentation

//## The CancelFlight operation will cancel all reservations for the //## flight, notify all passengers with reservations, and disable future //## reservations for the flight.

int CancelFlight();

private:

//##ModelId=3954127801F4 int FlightNumber;

//##ModelId=3954128202DF date DepartureDate;

//##ModelId=395412860122 string DepartureCity;

//##ModelId=3954128E0097 string ArrivalCity;

};

#endif /* FLIGHT_H_INCLUDED_C6ABEA96 */

As you can see, the full operation signature is generated in the code. Any documentation you entered for the operation is also generated, as a comment in the code. If you enter information for the operation protocol, qualifications, exceptions, time, space, preconditions, semantics, or post−conditions, this information will not appear in the generated code.

442