This chapter describes the mapping between C++ and UML.
C++ class map to a UML class
Stereotype «C++Class» is an invisible stereotype used to include language properties for any C++ variable and belongs to Meta class Class.
C++ source code
UML model: |
A base class is a Class from which other Classes are derived. It facilitates the creation of other Classes that can reuse the code implicitly inherited from the base class (except constructors and destructors).
Base class definition is mapped to UML generalization, a generalization is created between the base class and the super class.
Access visibility (public, protected and private) and virtual properties of the base class are mapped to C++ language properties of the UML generalization.
C++ source code
UML model: Class relations |
Class Member Variable
Class member variables are mapped to UML attributes. See Variable for more info.
C++ source code
UML model: |
Class member functions are mapped to UML operations.
C++ source code
UML model: |
Class Constructor and Destructor
Stereotype «C++Constructor» is used to define C++ Constructor. This stereotype extends stereotype «C++Operation».
Name | Meta class | Constraints |
---|---|---|
C++Constructor | Operation | name = owner.name |
Tag definition | Type and default value | Description |
explicit | boolean[1]=false | Constructor Explicit explicit a(); |
initialization list | String[0..1] | Constructor initialization: a() : x(1) {} |
Stereotype «C++Destructor» is used to define C++ destructor. This stereotype extends stereotype «C++Operation».
Name | Meta Class | Constraints |
---|---|---|
C++Destructor | Operation | name = “~”+owner.name |
C++ class constructor and destructor are mapped to UML operation with stereotypes «C++Constructor» and «C++Destructor»:
C++ source code
UML model: |
C++ variable is mapped to UML attribute, the variable type is mapped to the attribute type.
C++ source code
UML model specification: |
C++ type pointer and reference is mapped to property Type Modifier of the attribute. Character $ is replaced by the type name.
C++ source code
UML model specification:
|
C++ array type is mapped to array tag value of the attribute. If array is set, then multiplicity property of UML attribute is set to “[0..*]”
C++ source code
UML model Tag specification: <<C++Attribute>> Tag arrayVar value is 5 |
Mutable variable modifiers are mapped to UML attribute’s language property Mutable.
Constraint: only member variable can be Mutable (Global variable cannot be Mutable).
C++ source code
UML model Tag specification: When variable is mutable, its Tag mutable value is true |
Bit field is mapped to tag value Bit field.
C++ source code
UML model: UML model Tag specification: Tag bit field with value 2 |
Variable Extern
C++ extern variable is mapped to stereotype «C++Extern». Tag Linkage value is used to specify the kind of linkage C or C++, if linkage is not specified (or without value), extern variable without linkage is generated.
C++ source code
UML model: |
Variable initial value is mapped to UML attribute’s default value. Variable initial value set using function style method is mapped to UML attribute’s default value and attribute’s language property Tag Abbreviated Initialization set to true.
Constraint: only static const member variables can be initialized, and they cannot be initialized using function style method.
C++ source code
UML model Tag specification: Variable initial value is mapped to UML attribute’s default valueTag Abbreviated Initialization set to true. |
C++ const and volatile modifiers for attribute/function parameter are mapped to Type Modifiers properties.
For attribute const, the property Is Read Only is set to true during reverse.
The character $ in Type Modifier value is replaced by the type name.
Constraint : If the property Is Read Only is set and Type Modifiers is not set to const or const volatile - set to const, or an error message will display during syntax check.
C++ source code
UML model: UML model Tag specification: Tag bit field with value 2 |
C++ function is mapped to UML operation, parameter of function is mapped to UML parameter with property direction set to inout, return type of function is mapped to UML parameter with property direction set to return. Type of parameter is mapped to type of UML parameter.
C++ default parameter value is mapped to defaultValue property of UML parameter.
Pointer, reference and array type of parameter are mapped to property Type Modifier of parameter.
C++ source code
UML model Tag specification: Return type of function |
C++ function variable-length parameter list is mapped to a UML parameter with name “...” and without type.
C++ source code
UML model: |
C++ void function parameter is mapped to a UML parameter without name and with type void.
C++ source code
UML model: UML model Tag specification: Operation with type void |
C++ register parameter is mapped to UML parameter language property Register Depending on the compiler, register can be limited on some types (int, char).
C++ source code
UML model: UML model Tag specification: Tag register |
C++ function modifiers are mapped to Language properties of Operation.
Virtual function is mapped to Virtual modifier property.
Inline function is mapped to Inline modifier property.
Explicit function is mapped to Explicit modifier property. Constraint: explicit is only valid for constructor.
Const function is mapped to UML operation Is Query property.
Volatile function is mapped to Tag value volatile.
C++ source code
UML model Tag specification: Tag explicit |
C++ function pointer type is mapped to attribute/parameter with «C++FunctionPtr» stereotype, a dependency with «C++BaseType» stereotype link from the attribute/parameter to the operation in a «C++FunctionSignature» class, and type modifiers of the dependency is set to *$. Member function pointer use the same mapping, and member class tag of «C++BaseType» stereotype point to a class.
C++ source code
UML model Tag specification: Tag explicit |
C++ function operator is mapped to normal function with the C++ operator name mapped to UML operation name. See C++Operator for more info.
C++ source code
UML model: |
C++ exception is mapped to UML operation’s raised exception properties.
If raisedExpression is empty, and throw exception tag is set to none a throw without parameter is generated.
If raisedExpression is empty, and throw exception tag is set to any throw keyword is not generated.
If the tag throw exception is not set, then generate specific raisedExpression, or do not generate throw if raisedExpression is empty.
C++ source code
UML model Tag specification: Property throw exception |
Variables and function visibility are mapped using the UML visibility property.
Members of C++ class without access visibility specified are private.
Members of C++ struct or union without access visibility specified are public.
Variables and functions outside a class/struct/union are public.
C++ source code
UML model: |
Static variables and functions are mapped to UML Is Static property.
C++ source code
UML model: UML model Tag specification: Property Is Static |
Pure virtual C++ function is mapped to UML operation with property Is Abstract set to true. If one or more functions are abstract in a class, the property Is Abstract of the UML class is set to true. Constraint: if no operation is abstract, the class cannot be abstract.
C++ source code
UML model: UML model Tag specification: Operation property Is Abstract |
C++ friend function is mapped with a «C++Friend» stereotyped dependency relationship between the function (an UML operation) and the friendClass. This relationship grants the friendship to the friendClass.
C++ source code
UML model: |
C++ friend member function is mapped with a «C++Friend» stereotyped dependency relationship between the member function and the friend class. This relationship grants the friendship to the friend class.
C++ source code
UML model: |
C++ friend class are mapped with a «C++Friend» stereotyped dependency relationship between the class and the friend class. This relationship grants the friendship to the friend class.
C++ source code
UML model: |
C++ struct are mapped to a UML class with stereotype «C++Struct». See C++Struct for more info.
The current version of MD use class’s language property “Class Key” |
C++ source code
UML model: |
C++ union is mapped to a UML class with stereotype «C++Union». See C++Union for more info.
The current version of MD use class’s language property “Class Key” |
C++ source code
UML model: |
C++ enum is mapped to UML enumeration. C++ enum fields are mapped to UML enumeration literals. C++ enum field with a specified value is mapped to tag value of «C++LiteralValue» stereotype.
C++ source code
UML model: |
C++ typedef is mapped to a class with «C++Typedef» stereotype. A «C++BaseType» dependency links to the original type. Type modifiers tag of «C++BaseType» dependency is used to define type modifiers. $ character is replaced by the type name. A typedef on a function pointer is mapped by linking a «C++BaseType» dependency to an operation and type modifiers tag of «C++BaseType» dependency is set to *$. Operation signature can be stored in a «C++FunctionSignature» class.
C++ source code
UML model: |
C++ namespace is mapped to a UML package with the stereotype «C++Namespace». Unnamed namespace is named unnamed+index number of unnamed namespace (start at 1), and unique namespace name tag is set to the source file path+:+index number of unnamed namespace (start at 0).
namespace n { namespace m { } } |
Global functions and variables are mapped to operations and attributes into an unnamed class with stereotype «C++Global». «C++Global» class resides in its respective namespace, or in a top package.
C++ source code
UML model: |
C++ source code
UML model: |
Variables can be created after a class/struct/union declaration. These variables are mapped to UML attribute, and placed in their respective namespace/global/class container.
C++ source code
UML model: |
C++ template class is mapped to UML class with template parameters properties added. Type of template parameter is always set to UML Class. To generate/reverse typename keyword, type keyword tag is set to typename.
C++ source code
UML model: Template ParametersTemplate Parameter Tag type keyword. |
C++ template function is mapped to UML operation with template parameters properties added. C++ template function overload is mapped to a normal function. (the same name with the same number of parameter, but different type of parameter) New style of template function overloading is mapped to a normal function. (the same name with the same number of parameter, but different type of parameter) and a template binding relationship is created between the overload operation and the template operation, with specific template parameter substitutions.
C++ source code
UML model: Template Parameters |
C++ default template parameter is mapped to UML default template parameters. Instantiation using the default template parameter is mapped using a template binding relationship with an empty actual property for the template parameter substitution.
template <class T=int> class defaultTemplate { }; |
Template instantiation are mapped to template binding relationship between the template class and the instantiate class, the template parameter substitution of the binding relationship is set using the template argument.
template <class T> class simpleTemplate { }; simpleTemplate<int> simpleTemplateInstance; |
For template argument using template instantiation as argument, an intermediate class is created with the specific binding.
template <class T> class T1Class { }; template <class T> class T2Class { }; T1Class<T2Class<int>> ... |
For template argument using multiple template instantiations in an inner class (b<int>::c<char>), the intermediate class instance is created in the outer class instance.
template <class T> class b { template <class T> class c { }; }; b<int>::c<char> ... |
Example of complex template instantiation. Containment relationship are placed on diagram for information only, these relationships are not created during a reverse process. Containment relationship is modeled by placing a class into a specific class/package. See Containment tree below the diagram.
C++ partial template instantiation use the same mapping as Template Instantiation and the unbinded parameter is binded to the specific template parameter class.
template <class T,class U,class V> class PT {}; template <class A,class B> class PT<B, int, A> {}; |
C++ Template specialization uses the same mapping as Template Instantiation.
template <class T> class TS {}; template <> class TS<int> {}; |
The example code is declared in A.h file. The file component A.h has the «use» association applied by «C++Include» stereotype with Class Forward tag value.
C++ source code
UML model: Template Parameters
|
The example code is declared in A.h file. The «use» association is also applied to C++Include stereotype shown in Forward class declaration.
C++ source code
UML model: Specification for #include "B.h": User IncludeSpecification for #include <E.h>:
|
The example code is declared in D.cpp file.
C++ source code
UML model: Specification for #include "B.h": User IncludeSpecification for #include <E.h>: System include |