2. Tutorial

image_pdfimage_print

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

The Web-based book An introduction to object-oriented programming as modeling may be used as a tutorial introduction to qBeta. It is intended for teaching programming to beginners, which means that people experienced with programming languages and programming may find it to be too long. However, it does explain qBeta in details.

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.