Generating C++ virtual functions
This section tells you how to create C++ virtual functions from a UML model. To generate a C++ pure virtual function in a class, you need to create a UML operation within a UML class, and ensure that the Abstract Qualifier in the General Properties of the operation is selected.
For example, to create a pure virtual function such as virtual int purevirtualop(int x)=0, perform these steps:
-
Create a UML operation named
purevirtualopwith a return parameter of typeintand a parameter x of typeint. - Select the Abstract and Unique Qualifiers in the Properties tab of the operation, as shown in Figure 1. The code generated for the pure virtual function is shown in Listing 1.
Figure 1. Setting the properties for a pure virtual function
Listing 1. Generated code for a pure virtual function in header file
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
class Class1
{
//Begin section for Class1
//TODO: Add attributes that you want preserved
//End section for Class1
public:
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
virtual int purevirtualop(int x)=0;
}; //end class Class1
|
To generate a C++ virtual function in a class, you need to create a UML operation within a UML Class and apply the cpp_operation stereotype on the UML operation. This stereotype has three property/value pairs:
- isFriend
- isInline
- isVirtual
The isVirtual property needs to bet set to True. For example, to create a virtual function such as virtual int virtualop(int x), perform the following steps:
-
Create a UML operation
virtualopwith a return parameter of typeintand a parameter x of typeint. Apply thecpp_operationstereotype to it. - Set the properties of the stereotype as follows (shown in Figure 2):
-
isFriend to
False -
isInline to
False -
isVirtual to
True
- Run the UML to C++ transformation. The code generated for the virtual function in a header file is shown in Listing 2.
Figure 2. Setting the properties for a virtual function
Listing 2. Generated code for a virtual function in header file
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
class Class1
{
//Begin section for Class1
//TODO: Add attributes that you want preserved
//End section for Class1
public:
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
virtual int virtualop(int x);
}; //end class Class1
|
Generating C++ inline functions
This section tells you how to create C++ inline functions from a UML model. To create a C++ inline function in a class, you need to create a UML operation within a UML Class and apply the cpp_operation stereotype on the UML operation. The isInline property of this stereotype needs to be set to True.
For example, to create an inline function such as inline int inlineop(int x), perform these steps:
-
Create a UML operation named
inlineopwith a return parameter of typeintand a parameter x of typeint. -
Apply the
cpp_operationstereotype to it. - Set the properties of the stereotype as follows (shown in Figure 3).
-
isFriend to
False -
isInline to
True -
isVirtual to
False
Figure 3. Setting the properties for an inline function
The UML to C++ transformation provides two options for generating code for an inline function. The default option generates code for the inline function in both the header and body files. The header file contains the declaration of the function as shown in Listing 3, and the body file contains the definition of the function as shown in Listing 4.
Listing 3. Generated Code for an inline function in header file
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
class Class1
{
//Begin section for Class1
//TODO: Add attributes that you want preserved
//End section for Class1
public:
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
inline int inlineop(int x);
}; //end class Class1
|
Listing 4. Generated Code for an inline function in body file
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
int Class1::inlineop(int x)
{
//TODO Auto-generated method stub
return 0;
}
|
The transformation also provides an option to generate the function definition for an inline function in the header file below the class declaration. To activate this option, perform these steps:
-
Append the
-DINLINE_HEADER=trueVM argument to the eclipse.ini file -
Next, relaunch Rational System Developer or Rational Software Architect with the
-cleanoption. -
Alternatively, you can relaunch Rational System Developer or Rational Software Architect with these command line options:
-vmargs -DINLINE_HEADER=true.
The declaration and definition generated for the inline function with this option would all be in the header file, as shown in Listing 5.
Listing 5. Generated Code for an inline function using the
DINLINE_HEADER
option
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
class Class1
{
//Begin section for Class1
//TODO: Add attributes that you want preserved
//End section for Class1
public:
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
inline int inlineop(int x);
}; //end class Class1
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
int Class1::inlineop(int x)
{
//TODO Auto-generated method stub
return 0;
}
|
Generating C++ friend functions
This section tells you how to generate C++ friend functions from a UML model. To generate a C++ friend function that is a friend of a class, you need to create a UML operation within a UML Class and apply the cpp_operation stereotype on the UML operation. The isFriend property of this stereotype needs to be set to True.
To create a friend function such as friend int Class2::friendop (int x), where Class2 is the class in which the function friendop is defined, perform these steps:
-
Create a
Class2::friendopUML operation with a return parameter of typeintand a parameter x of typeint. - Apply the cpp_operation stereotype on it.
- Set the properties of the stereotype as follows (shown below in Figure 4):
-
isFriend to
True -
isInline to
False -
isVirtual to
False
The code generated is shown in Listing 6.
Figure 4. Setting the properties for a friend function
Listing 6. Generated Code for a friend function
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
class Class1
{
//Begin section for Class1
//TODO: Add attributes that you want preserved
//End section for Class1
public:
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
friend int Class2::friendop(int x);
}; //end class Class1
|
This section discusses modeling a C++ template class in a UML model. To model a C++ template class, you need to create a UML Class and then add template parameters of type Class (or any of the UML primitive types) to this UML Class.
To create a template class such as template class one<class T, int X> {int op(){}};, create a UML class and then add the following two template parameters and one UML operation:
- A template parameter of type Class (named T) as shown in Figure 5, by right-clicking source > Models > one and selecting Add UML > Template Parameter > Class.
- A template parameter of type Integer, named X, as shown in Figure 6, by right-clicking source > Models > one and selecting Add UML > Template Parameter > Integer.
-
A UML operation op with, an
intas the return type.
Figure 5. Creating a template parameter of type Class
Figure 6. Creating a template parameter of type integer
The generated code for the template class one has both the declaration and definition for the operation op within the template class declaration, as shown in Listing 7. This is the product behavior in Version 7.0.0.3 and later.
Listing 7. Generated Code for a template class
The transformation also provides an option to generate the keyword in place of class for template parameters of type class.
-
To activate this option, you need to append the VM argument
-DTYPENAME_TEMPLATES=truein the eclipse.ini file -
Next, relaunch Rational System Developer or Rational Software Architect with the
-cleanoption. -
Another option is that you could relaunch Rational System Developer or Rational Software Architect with the command line option
-vmargs -DTYPENAME_TEMPLATES=true. With this option, the keywordtypenameis generated for every template parameter of type class. The code generated with this option is shown in Listing 8.
Listing 8. Generated Code for a template class with DTYPENAME_TEMPLATES option
Creating different extensions for header and body Files
This section deals with creating different file extensions for the generated header and source files corresponding to a UML class. To create different file extensions for the header and source files corresponding to a UML class, apply the cpp_properties stereotype on the UML class. This stereotype has two properties
- headerFileExtension: This property is used for setting the extension of the header file.
- bodyFileExtension: This property is used for setting the extension of the body file.
To generate the code for a UML class in a header file with a .inc extension, and a source file with a .in extension, perform these steps:
-
Create a UML Class class1 and add an operation
opwith return typeintto it. -
Apply the
cpp_propertiesstereotype on the UML class and set theheaderFileExtensionproperty to .inc and the bodyFileExtension property to .in, as shown in Figure 7.
Figure 7. Setting the properties for different header and body file extensions
The generated code will have the files class1.inc and class1.in. These file extensions (.inc and .in) need to be associated to C++ header and source files, so that the C/C++ development toolkit recognizes .inc and .in as valid C++ file extensions. To do this, perform these steps:
- Go to Window > Preferences > General > Editors > File Associations.
- Click the Content Types link at the top of the page.
- Navigate to Text > C Source File > C++ Source File > C++ Header File.
- Click Add, and the dialog shown in Figure 8 displays.
-
In this dialog, specify the extension type as
*.incand click OK.
Figure 8. Setting the extension for the header file
- Scroll down the file associations section to verify that *.inc has been included as a file association for a C++ header file, as shown in Figure 9.
- Click OK. At this point, any file having a .inc extension in a C++ project is considered by the C/C++ development toolkit to be a C++ header file.
Figure 9. The file associations for C++ header file
- Navigate to Text > C Source File > C++ Source File.
- Click Add, and the dialog shown in Figure 10 displays.
-
In this dialog, specify the extension type as
*.inand click OK.
Figure 10. Setting the extension for the source file
-
Scroll down the file associations section to verify that
*.inhas been included as a file association for a C++ source file, as shown in Figure 11. - Click OK. At this point, any file having a .in extension in a C++ project is considered by the C/C++ development toolkit to be a C++ Source file.
Figure 11. The file associations for C++ source file
Controlling the visibility order within a class
The default option for generating the code is to sort by visibility order. The sort by visibility order dumps the code generated by the transform according to its visibility. By default, the order is private, protected, and public. This means that all the private members of the class element are dumped first, followed by protected and public, respectively. To view the generated code, create a UML model and then add a UML class A. To this class add the following elements:
-
A UML property a of type
intand a UML operation abc with return typevoid. Set the visibility of these two elements to private. -
A UML property b of type
intand a UML operation xyz with return typeint. Set the visibility of these two elements to protected. -
A UML property c of type
intand a UML operation pqr with return typebool. Set the visibility of these two elements to public.
The code generated for this UML model looks like that in Listing 9.
Listing 9. The code generated with the sort by visibility order
The UML To C++ transformation provides an option to generate the code in the order of public elements, followed by protected and private, respectively.
-
To activate this option, append the
-DVISIBILITY_ORDER=truevm argument in the eclipse.ini file. -
Next, relaunch Rational System Developer or Rational Software Architect with the
-cleanoption. -
Alternatively, you could relaunch Rational System Developer or Rational Software Architect with the
-vmargs -DVISIBILITY_ORDER=truecommand line option. The code generated with this option looks like that in Listing 10.
Listing 10. The code generated with the DVISIBILITY_ORDER option
The UML To C++ transformation also provides an option to generate the class attributes and operations in the same order as they are stored in the UML model.
- To enable this option, navigate to Window > Preferences > Modeling > Transformations > UML to C++ Transformation.
- Select the Edit Styles page.
- In this page, select the Sort By storage option, as shown in Figure 12. The code generated with this option is shown in Listing 11.
Figure 12. Setting the Sort By Storage option
Listing 11. The code generated with the DVISIBILITY_ORDER option
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
class Class1 {
//Begin section for Class1
//TODO: Add attributes that you want preserved
//End section for Class1
private:
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
int a;
protected:
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
int b;
public:
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
int c;
private:
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
void abc();
protected:
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
int xyz();
public:
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
bool pqr();
}; //end class Class1
|
Using the Exclude Path for Include option
The #include statements that are generated in the code from the UML model have relative paths to the included files. For example, consider a UML model that has a dependency between 2 classes (Class1 and Class2), each nested under a different package (say Package1 and Package2). In this case, the code generated in Class1.h looks like that shown in Listing 12. Note the relative path "../Package2/Class2.h" that is generated to the include file (Class2.h).
Listing 12. Generated Code for #include
The UML To C++ transformation provides an option to generate #include statements that contain only the filenames of the corresponding included files. To activate this option go to the properties page of the UML To C++ transformation configuration wizard and select the Suppress path names in include directives option as shown in Figure 13.
The Suppress path names in include directives option is available in Rational Software Architect and Rational System Developer Version 7.0.5 and later. The code generated with this option (from the UML model mentioned above) is shown in Listing 13. Note that only the filename Class2.h is generated.
Figure 13. Setting the Suppress path names in include directives option
Listing 13. Generated Code for #include with the DEXCLUDE_PATH_FOR_INCLUDES option
#include "Class2.h"
//@generated "UML to C++ (com.ibm.xtools.transform.uml2.cpp.CPPTransformation)"
class Class1
{
//Begin section for Class1
//TODO: Add attributes that you want preserved
//End section for Class1
};
|
What you have learned and what lies ahead
In this article, you have learned how to do the following:
- Generate C++ virtual functions
- Generate C++ inline functions
- Generate C++ friend functions
- Model a Template Class
- Create different extensions for header and body Files
- Control the visibility order within a class
- Use the Exclude Path for Include option
In Part 3, you will learn how to construct C++ specific models in UML to generate code accordingly.
Learn
-
For technical resources, visit the developerWorks Rational Systems Developer area. You'll find technical documentation, how-to articles, education, downloads, product information, and more.
-
Stay current with Technical events and Webcasts.
- Subscribe to the
developerWorks Rational zone newsletter.
Keep up with developerWorks Rational content. Every other week, you'll
receive updates on the latest technical resources and best practices for the
Rational Software Delivery Platform.
- Subscribe to the
Rational Edge newsletter
for articles on the concepts behind effective software development.
-
Browse the technology bookstore for books on these and other technical topics.
-
How to use the C/C++ Development Toolkit (CDT): C/C++ development with the Eclipse Platform.
-
In the Architecture area on developerWorks, get the resources you need to advance your skills in the architecture arena.
Get products and technologies
- Download
trial versions of IBM Rational software.
-
Download IBM product evaluation versions and get your hands on application development tools and middleware products from DB2®, Lotus®, Rational®, Tivoli®, and WebSphere®.
Discuss
-
Check out the author's blog on modeling insight.
-
Check out developerWorks
blogs and get involved in the developerWorks community.
-
Check out Rational Software Architect, Rational Data Architect, Rational Software Modeler, Rational Systems Developer, Rational Application Developer, and Rational Web Developer: Ask questions about Rational Systems Developer and C++ Transformation.
Comments (Undergoing maintenance)





