QM  4.1.2
Working with Classes

A class groups together attributes and operations available to all instances of the class (objects). A class can also inherit from another class (called the superclass).

Classes are the main entities from which you build your models in QM™. Also, in QM™, only a class can have a State Machine.

QM™ supports classes and inheritance regardless of the target programming language, which currently can be either C or C++. The Application Note "Simple Object-Oriented Programming in C" describes how QP frameworks and the QM code generator implement classes and inheritance in portable ANSI C.
A Class in Model Explorer

Adding a Class

A Class can only be added to a Package. To add a Class, in the Model Explorer right-click on the Package to which you want to add a new Class and select Add Class from the popup menu.

Adding a Class

Class Property Sheet

A QM™ Class item can be configured by the Class-Specific Property Sheet.

Class Property Sheet

The class-item property sheet allows you to set the following properties:

Class Name

The class name should be a valid name of a C++ class or a C struct. Typically, you should strive for a short and punchy name, typically a noun that captures the nature of the objects of this class. For example, a class that encapsulates a motor controller could be named Motor. (For more ideas about naming classes, see the discussion of the StackOverflow question "What's the best approach to naming classes?").

Superclass of a Class

A class can optionally inherit a superclass. A superclass can be any of the already defined classes, which specifically include classes provided in the QP Framework Item. The following screen shot shows an expanded drop-down box of existing classes, from which you can choose the superclass. Please note that the classes listed in the drop-box use the Fully-Qualified Names, that is, names that are prefixed with the package name(s), such as qpc::QMActive.

Selecting a Superclass

Class Documentation

The documentation box in the Class Property Sheet allows you to provide documentation to the class. The QM code generator parses the documentation box and can generate comments from it in the auto-generated code.

Class Attributes

Static (Class-Wide) Attributes

Class Operations

Class Constructor

A constructor is a kind of class operation that initializes an instance of its class. QM supports class constructors both for C and C++.

Class Constructors in C

In C, a constructor should be named ctor, or ctor1, ctor2, etc. if you have more constructors with different signatures (different parameters). The ctor function should have return type void. It can have any number of parameters.

Class constructor in C

This piece of the model generates the following code:

void Missile_ctor(Missile * const me) {
QMActive_ctor(&me->super, Q_STATE_CAST(&Missile_initial));
The C constructor must always call the superclass' constructor explicitly.

Class Constructors in C++

In C++, a constructor has the same name as the class and no return value. A constructor can have any number of parameters and a class may have any number of overloaded constructors. Constructors may have any accessibility, public, protected or private.

To provide the constructor initializer list, you put the list in the code panel starting with the colon (':') optionally preceded by any number of spaces. You can continue the list for as many lines as you wish.

The constructor initializer list might be followed by the body of the constructor. To start the body, you simply leave one (or more) empty lines after the initializer. QM will interpret all lines of code after such an empty line as the body of the constructor.

Finally, you might skip the constructor initializer list altogether by simply not placing the colon (':') at the beginning.

The following screen shot shows a C++ constructor with a constructor initializer list and a body (just a comment).

Class constructor in C++

This piece of the model generates the following code:

: QMActive(Q_STATE_CAST(&Missile::initial))
// body of code

Class Destructor

Static (Class-Wide) Operations

Next: Working with Attributes