home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / program / language / bob / doc / Tutorial / 02 < prev    next >
Encoding:
Text File  |  1994-12-30  |  5.8 KB  |  156 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.  # Lines whose first non blank character is | are comments.
  37.  
  38.  # Non comment lines should have the pathname of a file for first
  39.    word. Blank lines are permitted (version 2.1 onwards).
  40.  
  41.  # Files which create an instance object of a class must be preceded
  42.    by files defining the class.
  43.  
  44. Look at the file Bob:h.account. It begins with a comment. The C convention
  45. for comments, enclosed in /* ... */, is used. Beware! You cannot nest
  46. C comments. You can also put comments in ArmBob following //. Such
  47. comments simply take up the rest of the line.
  48.  
  49. Following the comment is the definition of a class called account. It
  50. is defined by four pieces of data:
  51.  
  52.            balance;       
  53.            owner;
  54.            password;
  55.            starting_date;
  56.  
  57. and four methods
  58.  
  59.            withdraw(x); 
  60.            statement();
  61.            change_password();
  62.            has_owner(name);
  63.  
  64. The next definition
  65.  
  66. account::account(amount,name)
  67.           {
  68.            balance = (amount>0)?amount:0;
  69.            owner = name;
  70.            print("Enter ",name,"'s password here: ");
  71.            password = hidden_input('-');
  72.            starting_date = sysvar("Sys$Date");
  73.            return this;
  74.           }
  75.  
  76. describes how to create a particular account. A statement of the form
  77.  
  78.           slushfund = new account(1000,"Croesus");
  79.  
  80. in a program would use this definition to create an instance object
  81. of the account class, called slushfund, with an initial balance of
  82. 1000 and an owner called "Croesus". The execution of the statement
  83. would produce as a side-effect a request for a password, and would
  84. use the operating system variable Sys$Date to establish slushfund's
  85. starting date. The variables balance, owner, password and starting_date
  86. have no meaning except in the context of a particular instance object
  87. of account. Each instance object has its own copy of these variables.
  88. To access them, for reading or writing, we have to use a method.
  89. The next definition in Bob:h.account defines the withdraw method.
  90.  
  91.           account::withdraw(x)
  92.           {
  93.            local sum;
  94.            print("Please enter your password :");
  95.            if (password == hidden_input('-'))
  96.              {
  97.               sum = (x < balance)?x:balance;
  98.               balance -= sum;
  99.               print("\n",owner,"'s account has been debited ",sum,".\n");
  100.              }
  101.            else
  102.             {
  103.              sum = 0;
  104.              print("\nSorry! Wrong password.\n");
  105.             }
  106.            return sum;
  107.           }
  108.  
  109. If we wanted to withdraw, say 100, from Croesus' slushfund, we would
  110. use the statement
  111.  
  112.          slushfund->withdraw(100);
  113.  
  114. When this statement is executed, the computer asks for a password. If
  115. it does not get the right reply, no withdrawal takes place. The
  116. expression
  117.  
  118.           slushfund->withdraw(100)
  119.  
  120. evaluates to the sum actually withdrawn. This may be different from
  121. the sum requested (100 in this case), if the wrong password is given
  122. or the current balance cannot fully meet the request.
  123.  
  124. You can interpret the '->' as an operator which joins an expression
  125. for an instance object of a class with a method of that class to give 
  126. an actual function. In the same way, you can think of the '::' symbol
  127. as an operator that "opens up" the class that appears to the left of it
  128. so that the data in the class is visible in the body of the method
  129. whose name appears to the right of it.
  130.  
  131. It is a convention borrowed from C++, that the method for creating an
  132. instance object always has the same name as the class itself. The word
  133. 'this' in a method refers to the instance object itself. The word 'new'
  134. is used to allocate memory for a new instance object.
  135.  
  136. The first line in the body of the withdraw method
  137.  
  138.            local sum;
  139.  
  140. declares 'sum' to be a local variable. The keyword 'local' can only
  141. occur once in a method or function definition, and then it has to
  142. appear as the first word. To declare lots of variables local one must
  143. write
  144.  
  145.           local x, y, ...... ;
  146.  
  147. with the variables separated by commas. Local variables are private
  148. to the function definition that they appear in.
  149.  
  150. The statement and change_password methods are straightforward. The
  151. has_owner method is used to look up which instance objects of the account
  152. class belong to a given owner.
  153.  
  154. Next we will look at Bob:main.account.
  155.  
  156.