This tutorial shows how to create classes and methods in DRAKON-C++. See the C tutorial to learn how to build algorithms inside the method bodies.
A more advanced tutorial can be found in examples/C++ folder in the DRAKON Editor .zip file.
Contents
To create a DRAKON-C++ file:
Create a new file with DRAKON Editor.
Select C++ as the programming language. In the main menu, go to File / File properties... and choose "C++".
If you press Ctrl+B (Command-B on Mac), DRAKON Editor will generate an .h and a .cpp files in the same folder as the .drn file.
The DRAKON / Generate code menu command can be used instead.
You can also use drakon_gen.tcl command-line utility to generate the C++ files.
A DRAKON Editor file can contain one class and many free functions (functions that are not members of any class).
Creating free functions is described in the C tutorial.
Creating a class involves two actions:
A section is a block of plain text in the file description. A section starts with the section header and contains all the text below the header until the next section or end of file.
The text before the first section is the actual "file description" and is ignored by the code generator.
To edit the file description, go to File / File description...
The class section header should be:
=== class ===
Type in the class declaration without the closing bracket under the header. You could add member fields, hand-written functions, inner classes and enums.
Do not place there the declarations of the methods that are generated from diagrams. Those declarations will be put there automatically.
Here is an example of a class section. Both class and struct keywords are accepted in the section body (but not in the header).
=== class === struct Easy {
Another one:
=== class === class GoodClass {
A more elaborate class:
=== class === // This is a very good class. class GoodClass : public BadClass { private: int _count; float _width; struct Record { int First; int Second; }; public: // Manually added method declaration void DoIt();
Note that there are no closing brackets in these examples.
In order to turn the diagram into a class method, add the method keyword to the Parameters icon of the diagram.
For example, this diagram:
will produce the following definition in the .cpp file:
void Nice::Print() { /* item 6 */ cout << "Nice::print: " << _value << endl; return; }
Here is the declaration which is inserted into the class:
private: void Print();
In order to make the method public, add the public keyword to the first line of the Parameters icon. To make it protected, use protected instead.
You can also use other keywords to change the function interface:
In order to create a constructor, use the ctr keyword instead of method.
The dtr keyword turns the diagram into a destructor.
There can be several constructors per file, but only one destructor. The constructors must have different signatures.
Overloading for methods is not allowed.
Parameters icon | Generated signature |
method | private: void Foo(); |
public method | public: void Foo(); |
public method int a int b returns const char* |
public: const char* Foo(int a, int b); |
public inline method int a int b returns const char* |
public: inline const char* Foo(int a, int b) { // Generated method body. } |
public virtual method int a int b returns int |
public: virtual int Foo(int a, int b); |
public virtual const method int a int b returns int |
public: virtual int Foo(int a, int b) const; |
public virtual const abstract method int a int b returns int |
public: virtual int Foo(int a, int b) const = 0; |
protected static method int a int b |
public: static int Foo(int a, int b); |
protected ctr int a int b |
protected: explicit NiceClass(int a, int b); |
dtr | public: virtual ~NiceClass(); |
The options section is optional.
By default, DRAKON Editor generates a private copy constructor and a private assignment operator in order to prevent accidental copying.
private: // non-copyable Nice(const Nice& other) {} void operator=(const Nice& other) {}
In order to get rid of these, add a copying option with the value "yes":
=== options === copying=yes
DRAKON Editor supports a few sections intended for adding hand-written code to the generated files. Depending on the section, its text is added either to the top or to the bottom of the file. The section also determines which file will the section text go to - .h file or .cpp file.
Section | Goes to file | Position |
h_header | .h file | Start of file |
h_footer | .h file | End of file |
c_header | .cpp file | Start of file |
c_footer | .cpp file | End of file |