Table of Contents
Object oriented programming is integrated in URBI, with many innovative features like virtual attributes and broadcasting. This chapter introduces the most important features of objects in URBI. It can be skipped by novice programmers but it is not very complex and can be useful reading for everyone.
Like in C++, you define a class in URBI with the class keyword:
class myclass;
You can of course define what will be in the class, including three types of elements: variables, functions and events:
class myclass {
var x;
var y;
function f(a,b);
event signalmyself(s);
};
It is important to notice here that, unlike C++ classes, myclass in the above example is also an instance[4] and you can very well assign values to myclass.x and use it.
One important function that you might want to define is init, which is the class constructor (this is another difference with C++, the constructor is not named with the class name). This function should return nothing or return 0 to indicate success or any other value to indicate failure.
To define the body of a class method, you should do it outside the class definition, like this:
class myclass {
var x;
function init(a);
};
function myclass.init(a) {
x = a;
};
You can define a subclass (or instance, remember there is no difference), with a new command, like in C++:
mysubclass = new myclass(42);
This will create mysubclass and call mysubclass.init(42);
mysubclass inherits from myclass, so every attribute or method of myclass is also available in mysubclass, we will see in the next section how you can have default definition, or re-definition, and how they are handled[5].
Note that mysubclass can inherit from several classes by calling new on these different classes:
mysubclass = new myclass (42); mysubclass = new myotherclass ();
This is a very special way of dealing with multiple inheritance, compared to C++.
Calling new without parenthesis, just the class name, will execute the init constructor with no parameters:
mysubclass = new myclass; // same as new myclass();
Note that if init is not defined, or of init returns a value indicating a failure (non void and not zero), an error message will be output and the class creation will fail.
Classes can be extended at runtime, simply by creating new functions or new attributes related to them. For example:
class myclass {
var x;
};
var myclass.newattribute;
myclass.s = "hello";
...
function myclass.f(a) {
s = a;
};