home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / a / armbob / doc / Tutorial / 02 < prev    next >
Encoding:
Text File  |  1994-06-06  |  6.0 KB  |  160 lines

  1. ArmBob v.1.02 Tutorial 2                               GCW 06/06/94
  2.  
  3. Accounts   1
  4. ------------
  5. We will now try something more ambitious for our second program -
  6. modelling a bank account. This time, instead of writing the program
  7. as a single file, we will write it as three textfiles, one for defining
  8. the class of accounts, one for providing the notion of "hidden input",
  9. useful for entering passwords, and another for the main program.
  10.  
  11. When you double-click on ArmBob a filer window opens displaying the
  12. root directory of the 'Bob' pseudo-filing-system. In it you will see
  13. the icon for the World program of tutorial 1, a BobTask icon.
  14. You should also see a different sort of icon called Account. This
  15. has filetype BobProj. You should also see directories called 'h' 
  16. and 'main'. These can be referred to as Bob:h and Bob:main. They are
  17. for storing partial programs. Those which have a main function should 
  18. go in main, those which do not should go in h. 
  19.  
  20. If you SHIFT double-click on the BobProj file Account you will see that 
  21. it contains three lines
  22.  
  23. Bob:h.hidden      library file defining 'hidden_input(c)'
  24. Bob:h.account     definition of the class 'account'
  25. Bob:main.account  main program
  26.  
  27. These are the three files that make up our program. They are text files.
  28. None of them constitutes a program in its own right. BobPTask files
  29. work just like BobProj files except that the program is run in a 
  30. taskwindow.
  31.  
  32. BobProj and BobPTask files provide a convenient way of organising larger 
  33. ArmBob programs, which may be made up of pieces that you might want to 
  34. reuse in other programs. The following rules apply to them.
  35.  
  36.  # Each line must start with the name of a file. This must be followed
  37.    either by a newline, or by at least one blank space. The rest of
  38.    the line is comment.
  39.  
  40.  # Files which create an instance object of a class must be preceded
  41.    by files defining the class.
  42.  
  43. Make sure that the last line of a BobProj or BobPTask file either has
  44. a comment or a newline at the end, or else it will not run. Also make
  45. sure that there are no blank lines, because these produce the error
  46. message "Cannot open file".
  47.  
  48. Look at the file Bob:h.account. It begins with a comment. The C convention
  49. for comments, enclosed in /* ... */, is used. Beware! You cannot nest
  50. C comments. You can also put comments in ArmBob following //. Such
  51. comments simply take up the rest of the line.
  52.  
  53. Following the comment is the definition of a class called account. It
  54. is defined by four pieces of data:
  55.  
  56.            balance;       
  57.            owner;
  58.            password;
  59.            starting_date;
  60.  
  61. and four methods
  62.  
  63.            withdraw(x); 
  64.            statement();
  65.            change_password();
  66.            has_owner(name);
  67.  
  68. The next definition
  69.  
  70. account::account(amount,name)
  71.           {
  72.            balance = (amount>0)?amount:0;
  73.            owner = name;
  74.            print("Enter ",name,"'s password here: ");
  75.            password = hidden_input('-');
  76.            starting_date = sysvar("Sys$Date");
  77.            return this;
  78.           }
  79.  
  80. describes how to create a particular account. A statement of the form
  81.  
  82.           slushfund = new account(1000,"Croesus");
  83.  
  84. in a program would use this definition to create an instance object
  85. of the account class, called slushfund, with an initial balance of
  86. 1000 and an owner called "Croesus". The execution of the statement
  87. would produce as a side-effect a request for a password, and would
  88. use the operating system variable Sys$Date to establish slushfund's
  89. starting date. The variables balance, owner, password and starting_date
  90. have no meaning except in the context of a particular instance object
  91. of account. Each instance object has its own copy of these variables.
  92. To access them, for reading or writing, we have to use a method.
  93. The next definition in Bob:h.account defines the withdraw method.
  94.  
  95.           account::withdraw(x)
  96.           {
  97.            local sum;
  98.            print("Please enter your password :");
  99.            if (password == hidden_input('-'))
  100.              {
  101.               sum = (x < balance)?x:balance;
  102.               balance -= sum;
  103.               print("\n",owner,"'s account has been debited ",sum,".\n");
  104.              }
  105.            else
  106.             {
  107.              sum = 0;
  108.              print("\nSorry! Wrong password.\n");
  109.             }
  110.            return sum;
  111.           }
  112.  
  113. If we wanted to withdraw, say 100, from Croesus' slushfund, we would
  114. use the statement
  115.  
  116.          slushfund->withdraw(100);
  117.  
  118. When this statement is executed, the computer asks for a password. If
  119. it does not get the right reply, no withdrawal takes place. The
  120. expression
  121.  
  122.           slushfund->withdraw(100)
  123.  
  124. evaluates to the sum actually withdrawn. This may be different from
  125. the sum requested (100 in this case), if the wrong password is given
  126. or the current balance cannot fully meet the request.
  127.  
  128. You can interpret the '->' as an operator which joins an expression
  129. for an instance object of a class with a method of that class to give 
  130. an actual function. In the same way, you can think of the '::' symbol
  131. as an operator that "opens up" the class that appears to the left of it
  132. so that the data in the class is visible in the body of the method
  133. whose name appears to the right of it.
  134.  
  135. It is a convention borrowed from C++, that the method for creating an
  136. instance object always has the same name as the class itself. The word
  137. 'this' in a method refers to the instance object itself. The word 'new'
  138. is used to allocate memory for a new instance object.
  139.  
  140. The first line in the body of the withdraw method
  141.  
  142.            local sum;
  143.  
  144. declares 'sum' to be a local variable. The keyword 'local' can only
  145. occur once in a method or function definition, and then it has to
  146. appear as the first word. To declare lots of variables local one must
  147. write
  148.  
  149.           local x, y, ...... ;
  150.  
  151. with the variables separated by commas. Local variables are private
  152. to the function definition that they appear in.
  153.  
  154. The statement and change_password methods are straightforward. The
  155. has_owner method is used to look up which instance objects of the account
  156. class belong to a given owner.
  157.  
  158. Next we will look at Bob:main.account.
  159.  
  160.