2. Tutorial

This page is supposed to be an introduction to qBeta, but currently only a few examples are shown.

A qBeta program is an object as shown by the following very simple example:

myProgram: obj
   "Hello world\n".print

The object may be compiled and executed and prints the text "Hello world\n".

The following example shows the description of a Bank object containing a class pattern Account.

Bank: obj
   Account(owner: ref String):
      balance: var integer
      deposit(amount: var integer):
         balance := balance + amount
      withdraw(amount: var integer):
         balance := balance - amount
         
   Joe: obj Account("Joe")
   Mary: ref Account
   Mary := Account("Mary")
   Joe.deposit(500)
   Mary.deposit(300)
   Joe.withdraw(150)
   Mary := Joe

Joe is an object generated as part of the generation of the Bank object. This means that Joe is a constant reference to an Account object.

Mary is a variable reference to instances of Account and is assigned a reference to an Account object by the statement Mary := Account("Mary").

The method patterns deposit and withdraw are invoked on Joe and Mary by Joe.deposit(500), etc.

Finally a reference to Joe is assigned to Mary, which means that Mary now refers to the same object as Joe.

Hello world