In this section, we describe a clerk that perform activities that involve all three account objects.
anotherClerk: obj
handle:
newBalance: var float
newBalance := account_1010.deposit(200)
newBalance := account_1022.deposit(300)
newBalance := account_1025.deposit(400)
newBalance := account_2022.withdraw(50)
account_1010.addInterest
As with the clerk in section , this clerk invokes methods in the three Account
objects.
We may assemble the clerk, class Account
and the declarations of the three Account
objects in a microBank
object:
microBank: obj
anotherClerk: obj
handle:
newBalance: var float
newBalance := account_1010.deposit(200)
-"-
class Account:
-"-
account_1010: obj Account("John Smith")
account_1022: obj Account("Liza Jones")
account_1025: obj Account("Tina Turner")
anotherClerk.handle
The microBank
description is another example of a complete program that may be compiled and executed by a computer – assuming that a qBeta compiler exists for the computer.
Comments
To improve readability of programs, it is possible to add comments in the form of text that explains the program, or elements of the program. In general, it is important that the code in a program is understandable as it is, but sometimes it is necessary to add an explanation to help the reader.
A comment has the form:
-- This is a comment
Everything from the '--'
to the end-of-line is ignored by the compiler and has no influence on the execution of the program.
In the next example we have added a comment to the microBank
example:
microBank: obj
-- This program is an example of a program that demonstrates the use of class Account
-- The objects anotherClerk, account_1010, account_1022, and account_1025
-- only play the role of demonstrating how class Account may be used
-- Author: Donald Duck
anotherClerk: obj
-"-
class Account:
-"-
account_1010: obj Account("John Smith")
account_1022: obj Account("Liza Jones")
account_1025: obj Account("Tina Turner")
anotherClerk.handle
It is good practice to describe at the start of a program what it is supposed to model/do and include the name(s) of authors, and possible other information such as dates.
For program elements like class Account
one may also write a comment explaining the intension of this class. We may e.g. write: –- This class is supposed to represent/model a bank account
. However, such a comment does not say more than the name Account
, so we recommend leaving it out. There are other situations where it may be necessary to explain the intension of a class.
There are many different ways of writing comments and people do not necessarily agree about how to do this. We gradually introduce the style of comments that we recommend during the subsequent chapters of this book.