2.1 A simple qBeta program

image_pdfimage_print

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

myProgram: obj
   "Hello world\n".print

This program may be compiled and executed, which implies the generation of an object. This object executes the statement "Hello world\n".print, which 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: var String):
      balance: var integer
      maxOverDraw: val -500 
      deposit(amount: var integer):
         balance := balance + amount
      withdraw(amount: var integer):
         if (balance - amount >= maxOverDraw) :then
            balance := balance - amount
         :else
            "Insufficient fund".print
         
   Joe: obj Account("Joe")
   Mary: ref Account
   Mary := Account("Mary")
   Joe.deposit(500)
   Mary.deposit(300)
   Joe.withdraw(150)
   Mary := Joe

Class Account has the following attributes:

  1. A parameter owner, which is a variable data-item of type String.
  2. A local data-item, balance, which is a variable of type integer.
  3. A local data-item, maxOverDraw, which is a constant data-item of type integer.
  4. Two local methods deposit and withdraw, which each has a parameter amount.
  5. The method deposit adds the value of amount to the value of balance.
  6. The method withdraw executed an if:then:else statement that checks if there is enough money on the account to withdraw amount. If not a message is printed.

The Bank object in addition to class Account has the following declarations and statements – referred to as items.

  1. Joe is a constant reference to an Account-object. This object is generated by the expression Account("Joe"), which is executed as part of the generation of the Bank object.
  2. an object generated as part of the generation of the Bank object. This means that Joe is a constant reference to an Account object.
  3. Mary is a variable reference to instances of Account
  4. The statement Mary := Account("Mary") implies that an Account-object is generated with the String "Mary" as the actual parameter and a reference to this object is assigned to Mary.
  5. The statement Joe.deposit(500)invokes the method deposit on the object referred to by Joe with 500 being the actual parameter.
  6. The statement Mary.desposit(300) in a similar way invokes deposit on the object referred to by Mary.
  7. The final statement Mary := Joe implies that the reference hold by Joe is assigned to Mary, which means that Mary now refers to the same object as Joe.

qBeta distinguishes between references and values and use the term datum to be either a reference or a value.

  1. A reference is a datum the uniquely refers to an object. Data-items declared using obj and ref may hold references.
  2. A value is a datum being a mathematical value such as an integer, a float, a Boolean, a char and a string. Data-items declared using var and val may hold values.

The reason for distingiushing references and values is that there is a fundamental difference between these.

The object myBank is the only one of its kind and is called a singular object. This is in contrast to the objects generated from class Account where an arbitrary number of objects may be generated.