Home » Archimedes archive » Acorn User » AU 1997-10 A.adf » Extras » Apple][e/PD/BOB/ARMBOB/doc/Tutorial/02
Apple][e/PD/BOB/ARMBOB/doc/Tutorial/02
This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.
Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.
Tape/disk: | Home » Archimedes archive » Acorn User » AU 1997-10 A.adf » Extras |
Filename: | Apple][e/PD/BOB/ARMBOB/doc/Tutorial/02 |
Read OK: | ✔ |
File size: | 170F bytes |
Load address: | 0000 |
Exec address: | 0000 |
File contents
ArmBob v.1.02 Tutorial 2 GCW 06/06/94 Accounts 1 ------------ We will now try something more ambitious for our second program - modelling a bank account. This time, instead of writing the program as a single file, we will write it as three textfiles, one for defining the class of accounts, one for providing the notion of "hidden input", useful for entering passwords, and another for the main program. When you double-click on ArmBob a filer window opens displaying the root directory of the 'Bob' pseudo-filing-system. In it you will see the icon for the World program of tutorial 1, a BobTask icon. You should also see a different sort of icon called Account. This has filetype BobProj. You should also see directories called 'h' and 'main'. These can be referred to as Bob:h and Bob:main. They are for storing partial programs. Those which have a main function should go in main, those which do not should go in h. If you SHIFT double-click on the BobProj file Account you will see that it contains three lines Bob:h.hidden library file defining 'hidden_input(c)' Bob:h.account definition of the class 'account' Bob:main.account main program These are the three files that make up our program. They are text files. None of them constitutes a program in its own right. BobPTask files work just like BobProj files except that the program is run in a taskwindow. BobProj and BobPTask files provide a convenient way of organising larger ArmBob programs, which may be made up of pieces that you might want to reuse in other programs. The following rules apply to them. # Lines whose first non blank character is | are comments. # Non comment lines should have the pathname of a file for first word. Blank lines are permitted (version 2.1 onwards). # Files which create an instance object of a class must be preceded by files defining the class. Look at the file Bob:h.account. It begins with a comment. The C convention for comments, enclosed in /* ... */, is used. Beware! You cannot nest C comments. You can also put comments in ArmBob following //. Such comments simply take up the rest of the line. Following the comment is the definition of a class called account. It is defined by four pieces of data: balance; owner; password; starting_date; and four methods withdraw(x); statement(); change_password(); has_owner(name); The next definition account::account(amount,name) { balance = (amount>0)?amount:0; owner = name; print("Enter ",name,"'s password here: "); password = hidden_input('-'); starting_date = sysvar("Sys$Date"); return this; } describes how to create a particular account. A statement of the form slushfund = new account(1000,"Croesus"); in a program would use this definition to create an instance object of the account class, called slushfund, with an initial balance of 1000 and an owner called "Croesus". The execution of the statement would produce as a side-effect a request for a password, and would use the operating system variable Sys$Date to establish slushfund's starting date. The variables balance, owner, password and starting_date have no meaning except in the context of a particular instance object of account. Each instance object has its own copy of these variables. To access them, for reading or writing, we have to use a method. The next definition in Bob:h.account defines the withdraw method. account::withdraw(x) { local sum; print("Please enter your password :"); if (password == hidden_input('-')) { sum = (x < balance)?x:balance; balance -= sum; print("\n",owner,"'s account has been debited ",sum,".\n"); } else { sum = 0; print("\nSorry! Wrong password.\n"); } return sum; } If we wanted to withdraw, say 100, from Croesus' slushfund, we would use the statement slushfund->withdraw(100); When this statement is executed, the computer asks for a password. If it does not get the right reply, no withdrawal takes place. The expression slushfund->withdraw(100) evaluates to the sum actually withdrawn. This may be different from the sum requested (100 in this case), if the wrong password is given or the current balance cannot fully meet the request. You can interpret the '->' as an operator which joins an expression for an instance object of a class with a method of that class to give an actual function. In the same way, you can think of the '::' symbol as an operator that "opens up" the class that appears to the left of it so that the data in the class is visible in the body of the method whose name appears to the right of it. It is a convention borrowed from C++, that the method for creating an instance object always has the same name as the class itself. The word 'this' in a method refers to the instance object itself. The word 'new' is used to allocate memory for a new instance object. The first line in the body of the withdraw method local sum; declares 'sum' to be a local variable. The keyword 'local' can only occur once in a method or function definition, and then it has to appear as the first word. To declare lots of variables local one must write local x, y, ...... ; with the variables separated by commas. Local variables are private to the function definition that they appear in. The statement and change_password methods are straightforward. The has_owner method is used to look up which instance objects of the account class belong to a given owner. Next we will look at Bob:main.account.
00000000 41 72 6d 42 6f 62 20 76 2e 31 2e 30 32 20 54 75 |ArmBob v.1.02 Tu| 00000010 74 6f 72 69 61 6c 20 32 20 20 20 20 20 20 20 20 |torial 2 | 00000020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 | | 00000030 20 20 20 20 20 20 20 47 43 57 20 30 36 2f 30 36 | GCW 06/06| 00000040 2f 39 34 0a 0a 41 63 63 6f 75 6e 74 73 20 20 20 |/94..Accounts | 00000050 31 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 57 |1.------------.W| 00000060 65 20 77 69 6c 6c 20 6e 6f 77 20 74 72 79 20 73 |e will now try s| 00000070 6f 6d 65 74 68 69 6e 67 20 6d 6f 72 65 20 61 6d |omething more am| 00000080 62 69 74 69 6f 75 73 20 66 6f 72 20 6f 75 72 20 |bitious for our | 00000090 73 65 63 6f 6e 64 20 70 72 6f 67 72 61 6d 20 2d |second program -| 000000a0 0a 6d 6f 64 65 6c 6c 69 6e 67 20 61 20 62 61 6e |.modelling a ban| 000000b0 6b 20 61 63 63 6f 75 6e 74 2e 20 54 68 69 73 20 |k account. This | 000000c0 74 69 6d 65 2c 20 69 6e 73 74 65 61 64 20 6f 66 |time, instead of| 000000d0 20 77 72 69 74 69 6e 67 20 74 68 65 20 70 72 6f | writing the pro| 000000e0 67 72 61 6d 0a 61 73 20 61 20 73 69 6e 67 6c 65 |gram.as a single| 000000f0 20 66 69 6c 65 2c 20 77 65 20 77 69 6c 6c 20 77 | file, we will w| 00000100 72 69 74 65 20 69 74 20 61 73 20 74 68 72 65 65 |rite it as three| 00000110 20 74 65 78 74 66 69 6c 65 73 2c 20 6f 6e 65 20 | textfiles, one | 00000120 66 6f 72 20 64 65 66 69 6e 69 6e 67 0a 74 68 65 |for defining.the| 00000130 20 63 6c 61 73 73 20 6f 66 20 61 63 63 6f 75 6e | class of accoun| 00000140 74 73 2c 20 6f 6e 65 20 66 6f 72 20 70 72 6f 76 |ts, one for prov| 00000150 69 64 69 6e 67 20 74 68 65 20 6e 6f 74 69 6f 6e |iding the notion| 00000160 20 6f 66 20 22 68 69 64 64 65 6e 20 69 6e 70 75 | of "hidden inpu| 00000170 74 22 2c 0a 75 73 65 66 75 6c 20 66 6f 72 20 65 |t",.useful for e| 00000180 6e 74 65 72 69 6e 67 20 70 61 73 73 77 6f 72 64 |ntering password| 00000190 73 2c 20 61 6e 64 20 61 6e 6f 74 68 65 72 20 66 |s, and another f| 000001a0 6f 72 20 74 68 65 20 6d 61 69 6e 20 70 72 6f 67 |or the main prog| 000001b0 72 61 6d 2e 0a 0a 57 68 65 6e 20 79 6f 75 20 64 |ram...When you d| 000001c0 6f 75 62 6c 65 2d 63 6c 69 63 6b 20 6f 6e 20 41 |ouble-click on A| 000001d0 72 6d 42 6f 62 20 61 20 66 69 6c 65 72 20 77 69 |rmBob a filer wi| 000001e0 6e 64 6f 77 20 6f 70 65 6e 73 20 64 69 73 70 6c |ndow opens displ| 000001f0 61 79 69 6e 67 20 74 68 65 0a 72 6f 6f 74 20 64 |aying the.root d| 00000200 69 72 65 63 74 6f 72 79 20 6f 66 20 74 68 65 20 |irectory of the | 00000210 27 42 6f 62 27 20 70 73 65 75 64 6f 2d 66 69 6c |'Bob' pseudo-fil| 00000220 69 6e 67 2d 73 79 73 74 65 6d 2e 20 49 6e 20 69 |ing-system. In i| 00000230 74 20 79 6f 75 20 77 69 6c 6c 20 73 65 65 0a 74 |t you will see.t| 00000240 68 65 20 69 63 6f 6e 20 66 6f 72 20 74 68 65 20 |he icon for the | 00000250 57 6f 72 6c 64 20 70 72 6f 67 72 61 6d 20 6f 66 |World program of| 00000260 20 74 75 74 6f 72 69 61 6c 20 31 2c 20 61 20 42 | tutorial 1, a B| 00000270 6f 62 54 61 73 6b 20 69 63 6f 6e 2e 0a 59 6f 75 |obTask icon..You| 00000280 20 73 68 6f 75 6c 64 20 61 6c 73 6f 20 73 65 65 | should also see| 00000290 20 61 20 64 69 66 66 65 72 65 6e 74 20 73 6f 72 | a different sor| 000002a0 74 20 6f 66 20 69 63 6f 6e 20 63 61 6c 6c 65 64 |t of icon called| 000002b0 20 41 63 63 6f 75 6e 74 2e 20 54 68 69 73 0a 68 | Account. This.h| 000002c0 61 73 20 66 69 6c 65 74 79 70 65 20 42 6f 62 50 |as filetype BobP| 000002d0 72 6f 6a 2e 20 59 6f 75 20 73 68 6f 75 6c 64 20 |roj. You should | 000002e0 61 6c 73 6f 20 73 65 65 20 64 69 72 65 63 74 6f |also see directo| 000002f0 72 69 65 73 20 63 61 6c 6c 65 64 20 27 68 27 20 |ries called 'h' | 00000300 0a 61 6e 64 20 27 6d 61 69 6e 27 2e 20 54 68 65 |.and 'main'. The| 00000310 73 65 20 63 61 6e 20 62 65 20 72 65 66 65 72 72 |se can be referr| 00000320 65 64 20 74 6f 20 61 73 20 42 6f 62 3a 68 20 61 |ed to as Bob:h a| 00000330 6e 64 20 42 6f 62 3a 6d 61 69 6e 2e 20 54 68 65 |nd Bob:main. The| 00000340 79 20 61 72 65 0a 66 6f 72 20 73 74 6f 72 69 6e |y are.for storin| 00000350 67 20 70 61 72 74 69 61 6c 20 70 72 6f 67 72 61 |g partial progra| 00000360 6d 73 2e 20 54 68 6f 73 65 20 77 68 69 63 68 20 |ms. Those which | 00000370 68 61 76 65 20 61 20 6d 61 69 6e 20 66 75 6e 63 |have a main func| 00000380 74 69 6f 6e 20 73 68 6f 75 6c 64 20 0a 67 6f 20 |tion should .go | 00000390 69 6e 20 6d 61 69 6e 2c 20 74 68 6f 73 65 20 77 |in main, those w| 000003a0 68 69 63 68 20 64 6f 20 6e 6f 74 20 73 68 6f 75 |hich do not shou| 000003b0 6c 64 20 67 6f 20 69 6e 20 68 2e 20 0a 0a 49 66 |ld go in h. ..If| 000003c0 20 79 6f 75 20 53 48 49 46 54 20 64 6f 75 62 6c | you SHIFT doubl| 000003d0 65 2d 63 6c 69 63 6b 20 6f 6e 20 74 68 65 20 42 |e-click on the B| 000003e0 6f 62 50 72 6f 6a 20 66 69 6c 65 20 41 63 63 6f |obProj file Acco| 000003f0 75 6e 74 20 79 6f 75 20 77 69 6c 6c 20 73 65 65 |unt you will see| 00000400 20 74 68 61 74 20 0a 69 74 20 63 6f 6e 74 61 69 | that .it contai| 00000410 6e 73 20 74 68 72 65 65 20 6c 69 6e 65 73 0a 0a |ns three lines..| 00000420 42 6f 62 3a 68 2e 68 69 64 64 65 6e 20 20 20 20 |Bob:h.hidden | 00000430 20 20 6c 69 62 72 61 72 79 20 66 69 6c 65 20 64 | library file d| 00000440 65 66 69 6e 69 6e 67 20 27 68 69 64 64 65 6e 5f |efining 'hidden_| 00000450 69 6e 70 75 74 28 63 29 27 0a 42 6f 62 3a 68 2e |input(c)'.Bob:h.| 00000460 61 63 63 6f 75 6e 74 20 20 20 20 20 64 65 66 69 |account defi| 00000470 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 6c |nition of the cl| 00000480 61 73 73 20 27 61 63 63 6f 75 6e 74 27 0a 42 6f |ass 'account'.Bo| 00000490 62 3a 6d 61 69 6e 2e 61 63 63 6f 75 6e 74 20 20 |b:main.account | 000004a0 6d 61 69 6e 20 70 72 6f 67 72 61 6d 0a 0a 54 68 |main program..Th| 000004b0 65 73 65 20 61 72 65 20 74 68 65 20 74 68 72 65 |ese are the thre| 000004c0 65 20 66 69 6c 65 73 20 74 68 61 74 20 6d 61 6b |e files that mak| 000004d0 65 20 75 70 20 6f 75 72 20 70 72 6f 67 72 61 6d |e up our program| 000004e0 2e 20 54 68 65 79 20 61 72 65 20 74 65 78 74 20 |. They are text | 000004f0 66 69 6c 65 73 2e 0a 4e 6f 6e 65 20 6f 66 20 74 |files..None of t| 00000500 68 65 6d 20 63 6f 6e 73 74 69 74 75 74 65 73 20 |hem constitutes | 00000510 61 20 70 72 6f 67 72 61 6d 20 69 6e 20 69 74 73 |a program in its| 00000520 20 6f 77 6e 20 72 69 67 68 74 2e 20 42 6f 62 50 | own right. BobP| 00000530 54 61 73 6b 20 66 69 6c 65 73 0a 77 6f 72 6b 20 |Task files.work | 00000540 6a 75 73 74 20 6c 69 6b 65 20 42 6f 62 50 72 6f |just like BobPro| 00000550 6a 20 66 69 6c 65 73 20 65 78 63 65 70 74 20 74 |j files except t| 00000560 68 61 74 20 74 68 65 20 70 72 6f 67 72 61 6d 20 |hat the program | 00000570 69 73 20 72 75 6e 20 69 6e 20 61 20 0a 74 61 73 |is run in a .tas| 00000580 6b 77 69 6e 64 6f 77 2e 0a 0a 42 6f 62 50 72 6f |kwindow...BobPro| 00000590 6a 20 61 6e 64 20 42 6f 62 50 54 61 73 6b 20 66 |j and BobPTask f| 000005a0 69 6c 65 73 20 70 72 6f 76 69 64 65 20 61 20 63 |iles provide a c| 000005b0 6f 6e 76 65 6e 69 65 6e 74 20 77 61 79 20 6f 66 |onvenient way of| 000005c0 20 6f 72 67 61 6e 69 73 69 6e 67 20 6c 61 72 67 | organising larg| 000005d0 65 72 20 0a 41 72 6d 42 6f 62 20 70 72 6f 67 72 |er .ArmBob progr| 000005e0 61 6d 73 2c 20 77 68 69 63 68 20 6d 61 79 20 62 |ams, which may b| 000005f0 65 20 6d 61 64 65 20 75 70 20 6f 66 20 70 69 65 |e made up of pie| 00000600 63 65 73 20 74 68 61 74 20 79 6f 75 20 6d 69 67 |ces that you mig| 00000610 68 74 20 77 61 6e 74 20 74 6f 20 0a 72 65 75 73 |ht want to .reus| 00000620 65 20 69 6e 20 6f 74 68 65 72 20 70 72 6f 67 72 |e in other progr| 00000630 61 6d 73 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 |ams. The followi| 00000640 6e 67 20 72 75 6c 65 73 20 61 70 70 6c 79 20 74 |ng rules apply t| 00000650 6f 20 74 68 65 6d 2e 0a 0a 20 23 20 4c 69 6e 65 |o them... # Line| 00000660 73 20 77 68 6f 73 65 20 66 69 72 73 74 20 6e 6f |s whose first no| 00000670 6e 20 62 6c 61 6e 6b 20 63 68 61 72 61 63 74 65 |n blank characte| 00000680 72 20 69 73 20 7c 20 61 72 65 20 63 6f 6d 6d 65 |r is | are comme| 00000690 6e 74 73 2e 0a 0a 20 23 20 4e 6f 6e 20 63 6f 6d |nts... # Non com| 000006a0 6d 65 6e 74 20 6c 69 6e 65 73 20 73 68 6f 75 6c |ment lines shoul| 000006b0 64 20 68 61 76 65 20 74 68 65 20 70 61 74 68 6e |d have the pathn| 000006c0 61 6d 65 20 6f 66 20 61 20 66 69 6c 65 20 66 6f |ame of a file fo| 000006d0 72 20 66 69 72 73 74 0a 20 20 20 77 6f 72 64 2e |r first. word.| 000006e0 20 42 6c 61 6e 6b 20 6c 69 6e 65 73 20 61 72 65 | Blank lines are| 000006f0 20 70 65 72 6d 69 74 74 65 64 20 28 76 65 72 73 | permitted (vers| 00000700 69 6f 6e 20 32 2e 31 20 6f 6e 77 61 72 64 73 29 |ion 2.1 onwards)| 00000710 2e 0a 0a 20 23 20 46 69 6c 65 73 20 77 68 69 63 |... # Files whic| 00000720 68 20 63 72 65 61 74 65 20 61 6e 20 69 6e 73 74 |h create an inst| 00000730 61 6e 63 65 20 6f 62 6a 65 63 74 20 6f 66 20 61 |ance object of a| 00000740 20 63 6c 61 73 73 20 6d 75 73 74 20 62 65 20 70 | class must be p| 00000750 72 65 63 65 64 65 64 0a 20 20 20 62 79 20 66 69 |receded. by fi| 00000760 6c 65 73 20 64 65 66 69 6e 69 6e 67 20 74 68 65 |les defining the| 00000770 20 63 6c 61 73 73 2e 0a 0a 4c 6f 6f 6b 20 61 74 | class...Look at| 00000780 20 74 68 65 20 66 69 6c 65 20 42 6f 62 3a 68 2e | the file Bob:h.| 00000790 61 63 63 6f 75 6e 74 2e 20 49 74 20 62 65 67 69 |account. It begi| 000007a0 6e 73 20 77 69 74 68 20 61 20 63 6f 6d 6d 65 6e |ns with a commen| 000007b0 74 2e 20 54 68 65 20 43 20 63 6f 6e 76 65 6e 74 |t. The C convent| 000007c0 69 6f 6e 0a 66 6f 72 20 63 6f 6d 6d 65 6e 74 73 |ion.for comments| 000007d0 2c 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 2f 2a |, enclosed in /*| 000007e0 20 2e 2e 2e 20 2a 2f 2c 20 69 73 20 75 73 65 64 | ... */, is used| 000007f0 2e 20 42 65 77 61 72 65 21 20 59 6f 75 20 63 61 |. Beware! You ca| 00000800 6e 6e 6f 74 20 6e 65 73 74 0a 43 20 63 6f 6d 6d |nnot nest.C comm| 00000810 65 6e 74 73 2e 20 59 6f 75 20 63 61 6e 20 61 6c |ents. You can al| 00000820 73 6f 20 70 75 74 20 63 6f 6d 6d 65 6e 74 73 20 |so put comments | 00000830 69 6e 20 41 72 6d 42 6f 62 20 66 6f 6c 6c 6f 77 |in ArmBob follow| 00000840 69 6e 67 20 2f 2f 2e 20 53 75 63 68 0a 63 6f 6d |ing //. Such.com| 00000850 6d 65 6e 74 73 20 73 69 6d 70 6c 79 20 74 61 6b |ments simply tak| 00000860 65 20 75 70 20 74 68 65 20 72 65 73 74 20 6f 66 |e up the rest of| 00000870 20 74 68 65 20 6c 69 6e 65 2e 0a 0a 46 6f 6c 6c | the line...Foll| 00000880 6f 77 69 6e 67 20 74 68 65 20 63 6f 6d 6d 65 6e |owing the commen| 00000890 74 20 69 73 20 74 68 65 20 64 65 66 69 6e 69 74 |t is the definit| 000008a0 69 6f 6e 20 6f 66 20 61 20 63 6c 61 73 73 20 63 |ion of a class c| 000008b0 61 6c 6c 65 64 20 61 63 63 6f 75 6e 74 2e 20 49 |alled account. I| 000008c0 74 0a 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 |t.is defined by | 000008d0 66 6f 75 72 20 70 69 65 63 65 73 20 6f 66 20 64 |four pieces of d| 000008e0 61 74 61 3a 0a 0a 20 20 20 20 20 20 20 20 20 20 |ata:.. | 000008f0 20 62 61 6c 61 6e 63 65 3b 20 20 20 20 20 20 20 | balance; | 00000900 0a 20 20 20 20 20 20 20 20 20 20 20 6f 77 6e 65 |. owne| 00000910 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 70 61 |r;. pa| 00000920 73 73 77 6f 72 64 3b 0a 20 20 20 20 20 20 20 20 |ssword;. | 00000930 20 20 20 73 74 61 72 74 69 6e 67 5f 64 61 74 65 | starting_date| 00000940 3b 0a 0a 61 6e 64 20 66 6f 75 72 20 6d 65 74 68 |;..and four meth| 00000950 6f 64 73 0a 0a 20 20 20 20 20 20 20 20 20 20 20 |ods.. | 00000960 77 69 74 68 64 72 61 77 28 78 29 3b 20 0a 20 20 |withdraw(x); . | 00000970 20 20 20 20 20 20 20 20 20 73 74 61 74 65 6d 65 | stateme| 00000980 6e 74 28 29 3b 0a 20 20 20 20 20 20 20 20 20 20 |nt();. | 00000990 20 63 68 61 6e 67 65 5f 70 61 73 73 77 6f 72 64 | change_password| 000009a0 28 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 68 |();. h| 000009b0 61 73 5f 6f 77 6e 65 72 28 6e 61 6d 65 29 3b 0a |as_owner(name);.| 000009c0 0a 54 68 65 20 6e 65 78 74 20 64 65 66 69 6e 69 |.The next defini| 000009d0 74 69 6f 6e 0a 0a 61 63 63 6f 75 6e 74 3a 3a 61 |tion..account::a| 000009e0 63 63 6f 75 6e 74 28 61 6d 6f 75 6e 74 2c 6e 61 |ccount(amount,na| 000009f0 6d 65 29 0a 20 20 20 20 20 20 20 20 20 20 7b 0a |me). {.| 00000a00 20 20 20 20 20 20 20 20 20 20 20 62 61 6c 61 6e | balan| 00000a10 63 65 20 3d 20 28 61 6d 6f 75 6e 74 3e 30 29 3f |ce = (amount>0)?| 00000a20 61 6d 6f 75 6e 74 3a 30 3b 0a 20 20 20 20 20 20 |amount:0;. | 00000a30 20 20 20 20 20 6f 77 6e 65 72 20 3d 20 6e 61 6d | owner = nam| 00000a40 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 70 72 |e;. pr| 00000a50 69 6e 74 28 22 45 6e 74 65 72 20 22 2c 6e 61 6d |int("Enter ",nam| 00000a60 65 2c 22 27 73 20 70 61 73 73 77 6f 72 64 20 68 |e,"'s password h| 00000a70 65 72 65 3a 20 22 29 3b 0a 20 20 20 20 20 20 20 |ere: ");. | 00000a80 20 20 20 20 70 61 73 73 77 6f 72 64 20 3d 20 68 | password = h| 00000a90 69 64 64 65 6e 5f 69 6e 70 75 74 28 27 2d 27 29 |idden_input('-')| 00000aa0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 73 74 61 |;. sta| 00000ab0 72 74 69 6e 67 5f 64 61 74 65 20 3d 20 73 79 73 |rting_date = sys| 00000ac0 76 61 72 28 22 53 79 73 24 44 61 74 65 22 29 3b |var("Sys$Date");| 00000ad0 0a 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 |. retu| 00000ae0 72 6e 20 74 68 69 73 3b 0a 20 20 20 20 20 20 20 |rn this;. | 00000af0 20 20 20 7d 0a 0a 64 65 73 63 72 69 62 65 73 20 | }..describes | 00000b00 68 6f 77 20 74 6f 20 63 72 65 61 74 65 20 61 20 |how to create a | 00000b10 70 61 72 74 69 63 75 6c 61 72 20 61 63 63 6f 75 |particular accou| 00000b20 6e 74 2e 20 41 20 73 74 61 74 65 6d 65 6e 74 20 |nt. A statement | 00000b30 6f 66 20 74 68 65 20 66 6f 72 6d 0a 0a 20 20 20 |of the form.. | 00000b40 20 20 20 20 20 20 20 73 6c 75 73 68 66 75 6e 64 | slushfund| 00000b50 20 3d 20 6e 65 77 20 61 63 63 6f 75 6e 74 28 31 | = new account(1| 00000b60 30 30 30 2c 22 43 72 6f 65 73 75 73 22 29 3b 0a |000,"Croesus");.| 00000b70 0a 69 6e 20 61 20 70 72 6f 67 72 61 6d 20 77 6f |.in a program wo| 00000b80 75 6c 64 20 75 73 65 20 74 68 69 73 20 64 65 66 |uld use this def| 00000b90 69 6e 69 74 69 6f 6e 20 74 6f 20 63 72 65 61 74 |inition to creat| 00000ba0 65 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 62 |e an instance ob| 00000bb0 6a 65 63 74 0a 6f 66 20 74 68 65 20 61 63 63 6f |ject.of the acco| 00000bc0 75 6e 74 20 63 6c 61 73 73 2c 20 63 61 6c 6c 65 |unt class, calle| 00000bd0 64 20 73 6c 75 73 68 66 75 6e 64 2c 20 77 69 74 |d slushfund, wit| 00000be0 68 20 61 6e 20 69 6e 69 74 69 61 6c 20 62 61 6c |h an initial bal| 00000bf0 61 6e 63 65 20 6f 66 0a 31 30 30 30 20 61 6e 64 |ance of.1000 and| 00000c00 20 61 6e 20 6f 77 6e 65 72 20 63 61 6c 6c 65 64 | an owner called| 00000c10 20 22 43 72 6f 65 73 75 73 22 2e 20 54 68 65 20 | "Croesus". The | 00000c20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 |execution of the| 00000c30 20 73 74 61 74 65 6d 65 6e 74 0a 77 6f 75 6c 64 | statement.would| 00000c40 20 70 72 6f 64 75 63 65 20 61 73 20 61 20 73 69 | produce as a si| 00000c50 64 65 2d 65 66 66 65 63 74 20 61 20 72 65 71 75 |de-effect a requ| 00000c60 65 73 74 20 66 6f 72 20 61 20 70 61 73 73 77 6f |est for a passwo| 00000c70 72 64 2c 20 61 6e 64 20 77 6f 75 6c 64 0a 75 73 |rd, and would.us| 00000c80 65 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 |e the operating | 00000c90 73 79 73 74 65 6d 20 76 61 72 69 61 62 6c 65 20 |system variable | 00000ca0 53 79 73 24 44 61 74 65 20 74 6f 20 65 73 74 61 |Sys$Date to esta| 00000cb0 62 6c 69 73 68 20 73 6c 75 73 68 66 75 6e 64 27 |blish slushfund'| 00000cc0 73 0a 73 74 61 72 74 69 6e 67 20 64 61 74 65 2e |s.starting date.| 00000cd0 20 54 68 65 20 76 61 72 69 61 62 6c 65 73 20 62 | The variables b| 00000ce0 61 6c 61 6e 63 65 2c 20 6f 77 6e 65 72 2c 20 70 |alance, owner, p| 00000cf0 61 73 73 77 6f 72 64 20 61 6e 64 20 73 74 61 72 |assword and star| 00000d00 74 69 6e 67 5f 64 61 74 65 0a 68 61 76 65 20 6e |ting_date.have n| 00000d10 6f 20 6d 65 61 6e 69 6e 67 20 65 78 63 65 70 74 |o meaning except| 00000d20 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 78 74 20 | in the context | 00000d30 6f 66 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 |of a particular | 00000d40 69 6e 73 74 61 6e 63 65 20 6f 62 6a 65 63 74 0a |instance object.| 00000d50 6f 66 20 61 63 63 6f 75 6e 74 2e 20 45 61 63 68 |of account. Each| 00000d60 20 69 6e 73 74 61 6e 63 65 20 6f 62 6a 65 63 74 | instance object| 00000d70 20 68 61 73 20 69 74 73 20 6f 77 6e 20 63 6f 70 | has its own cop| 00000d80 79 20 6f 66 20 74 68 65 73 65 20 76 61 72 69 61 |y of these varia| 00000d90 62 6c 65 73 2e 0a 54 6f 20 61 63 63 65 73 73 20 |bles..To access | 00000da0 74 68 65 6d 2c 20 66 6f 72 20 72 65 61 64 69 6e |them, for readin| 00000db0 67 20 6f 72 20 77 72 69 74 69 6e 67 2c 20 77 65 |g or writing, we| 00000dc0 20 68 61 76 65 20 74 6f 20 75 73 65 20 61 20 6d | have to use a m| 00000dd0 65 74 68 6f 64 2e 0a 54 68 65 20 6e 65 78 74 20 |ethod..The next | 00000de0 64 65 66 69 6e 69 74 69 6f 6e 20 69 6e 20 42 6f |definition in Bo| 00000df0 62 3a 68 2e 61 63 63 6f 75 6e 74 20 64 65 66 69 |b:h.account defi| 00000e00 6e 65 73 20 74 68 65 20 77 69 74 68 64 72 61 77 |nes the withdraw| 00000e10 20 6d 65 74 68 6f 64 2e 0a 0a 20 20 20 20 20 20 | method... | 00000e20 20 20 20 20 61 63 63 6f 75 6e 74 3a 3a 77 69 74 | account::wit| 00000e30 68 64 72 61 77 28 78 29 0a 20 20 20 20 20 20 20 |hdraw(x). | 00000e40 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 | {. | 00000e50 6c 6f 63 61 6c 20 73 75 6d 3b 0a 20 20 20 20 20 |local sum;. | 00000e60 20 20 20 20 20 20 70 72 69 6e 74 28 22 50 6c 65 | print("Ple| 00000e70 61 73 65 20 65 6e 74 65 72 20 79 6f 75 72 20 70 |ase enter your p| 00000e80 61 73 73 77 6f 72 64 20 3a 22 29 3b 0a 20 20 20 |assword :");. | 00000e90 20 20 20 20 20 20 20 20 69 66 20 28 70 61 73 73 | if (pass| 00000ea0 77 6f 72 64 20 3d 3d 20 68 69 64 64 65 6e 5f 69 |word == hidden_i| 00000eb0 6e 70 75 74 28 27 2d 27 29 29 0a 20 20 20 20 20 |nput('-')). | 00000ec0 20 20 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 | {. | 00000ed0 20 20 20 20 20 20 20 20 73 75 6d 20 3d 20 28 78 | sum = (x| 00000ee0 20 3c 20 62 61 6c 61 6e 63 65 29 3f 78 3a 62 61 | < balance)?x:ba| 00000ef0 6c 61 6e 63 65 3b 0a 20 20 20 20 20 20 20 20 20 |lance;. | 00000f00 20 20 20 20 20 62 61 6c 61 6e 63 65 20 2d 3d 20 | balance -= | 00000f10 73 75 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 |sum;. | 00000f20 20 20 20 70 72 69 6e 74 28 22 5c 6e 22 2c 6f 77 | print("\n",ow| 00000f30 6e 65 72 2c 22 27 73 20 61 63 63 6f 75 6e 74 20 |ner,"'s account | 00000f40 68 61 73 20 62 65 65 6e 20 64 65 62 69 74 65 64 |has been debited| 00000f50 20 22 2c 73 75 6d 2c 22 2e 5c 6e 22 29 3b 0a 20 | ",sum,".\n");. | 00000f60 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 | }. | 00000f70 20 20 20 20 20 20 20 20 20 65 6c 73 65 0a 20 20 | else. | 00000f80 20 20 20 20 20 20 20 20 20 20 7b 0a 20 20 20 20 | {. | 00000f90 20 20 20 20 20 20 20 20 20 73 75 6d 20 3d 20 30 | sum = 0| 00000fa0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 70 |;. p| 00000fb0 72 69 6e 74 28 22 5c 6e 53 6f 72 72 79 21 20 57 |rint("\nSorry! W| 00000fc0 72 6f 6e 67 20 70 61 73 73 77 6f 72 64 2e 5c 6e |rong password.\n| 00000fd0 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 |");. | 00000fe0 7d 0a 20 20 20 20 20 20 20 20 20 20 20 72 65 74 |}. ret| 00000ff0 75 72 6e 20 73 75 6d 3b 0a 20 20 20 20 20 20 20 |urn sum;. | 00001000 20 20 20 7d 0a 0a 49 66 20 77 65 20 77 61 6e 74 | }..If we want| 00001010 65 64 20 74 6f 20 77 69 74 68 64 72 61 77 2c 20 |ed to withdraw, | 00001020 73 61 79 20 31 30 30 2c 20 66 72 6f 6d 20 43 72 |say 100, from Cr| 00001030 6f 65 73 75 73 27 20 73 6c 75 73 68 66 75 6e 64 |oesus' slushfund| 00001040 2c 20 77 65 20 77 6f 75 6c 64 0a 75 73 65 20 74 |, we would.use t| 00001050 68 65 20 73 74 61 74 65 6d 65 6e 74 0a 0a 20 20 |he statement.. | 00001060 20 20 20 20 20 20 20 73 6c 75 73 68 66 75 6e 64 | slushfund| 00001070 2d 3e 77 69 74 68 64 72 61 77 28 31 30 30 29 3b |->withdraw(100);| 00001080 0a 0a 57 68 65 6e 20 74 68 69 73 20 73 74 61 74 |..When this stat| 00001090 65 6d 65 6e 74 20 69 73 20 65 78 65 63 75 74 65 |ement is execute| 000010a0 64 2c 20 74 68 65 20 63 6f 6d 70 75 74 65 72 20 |d, the computer | 000010b0 61 73 6b 73 20 66 6f 72 20 61 20 70 61 73 73 77 |asks for a passw| 000010c0 6f 72 64 2e 20 49 66 0a 69 74 20 64 6f 65 73 20 |ord. If.it does | 000010d0 6e 6f 74 20 67 65 74 20 74 68 65 20 72 69 67 68 |not get the righ| 000010e0 74 20 72 65 70 6c 79 2c 20 6e 6f 20 77 69 74 68 |t reply, no with| 000010f0 64 72 61 77 61 6c 20 74 61 6b 65 73 20 70 6c 61 |drawal takes pla| 00001100 63 65 2e 20 54 68 65 0a 65 78 70 72 65 73 73 69 |ce. The.expressi| 00001110 6f 6e 0a 0a 20 20 20 20 20 20 20 20 20 20 73 6c |on.. sl| 00001120 75 73 68 66 75 6e 64 2d 3e 77 69 74 68 64 72 61 |ushfund->withdra| 00001130 77 28 31 30 30 29 0a 0a 65 76 61 6c 75 61 74 65 |w(100)..evaluate| 00001140 73 20 74 6f 20 74 68 65 20 73 75 6d 20 61 63 74 |s to the sum act| 00001150 75 61 6c 6c 79 20 77 69 74 68 64 72 61 77 6e 2e |ually withdrawn.| 00001160 20 54 68 69 73 20 6d 61 79 20 62 65 20 64 69 66 | This may be dif| 00001170 66 65 72 65 6e 74 20 66 72 6f 6d 0a 74 68 65 20 |ferent from.the | 00001180 73 75 6d 20 72 65 71 75 65 73 74 65 64 20 28 31 |sum requested (1| 00001190 30 30 20 69 6e 20 74 68 69 73 20 63 61 73 65 29 |00 in this case)| 000011a0 2c 20 69 66 20 74 68 65 20 77 72 6f 6e 67 20 70 |, if the wrong p| 000011b0 61 73 73 77 6f 72 64 20 69 73 20 67 69 76 65 6e |assword is given| 000011c0 0a 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 |.or the current | 000011d0 62 61 6c 61 6e 63 65 20 63 61 6e 6e 6f 74 20 66 |balance cannot f| 000011e0 75 6c 6c 79 20 6d 65 65 74 20 74 68 65 20 72 65 |ully meet the re| 000011f0 71 75 65 73 74 2e 0a 0a 59 6f 75 20 63 61 6e 20 |quest...You can | 00001200 69 6e 74 65 72 70 72 65 74 20 74 68 65 20 27 2d |interpret the '-| 00001210 3e 27 20 61 73 20 61 6e 20 6f 70 65 72 61 74 6f |>' as an operato| 00001220 72 20 77 68 69 63 68 20 6a 6f 69 6e 73 20 61 6e |r which joins an| 00001230 20 65 78 70 72 65 73 73 69 6f 6e 0a 66 6f 72 20 | expression.for | 00001240 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 62 6a 65 |an instance obje| 00001250 63 74 20 6f 66 20 61 20 63 6c 61 73 73 20 77 69 |ct of a class wi| 00001260 74 68 20 61 20 6d 65 74 68 6f 64 20 6f 66 20 74 |th a method of t| 00001270 68 61 74 20 63 6c 61 73 73 20 74 6f 20 67 69 76 |hat class to giv| 00001280 65 20 0a 61 6e 20 61 63 74 75 61 6c 20 66 75 6e |e .an actual fun| 00001290 63 74 69 6f 6e 2e 20 49 6e 20 74 68 65 20 73 61 |ction. In the sa| 000012a0 6d 65 20 77 61 79 2c 20 79 6f 75 20 63 61 6e 20 |me way, you can | 000012b0 74 68 69 6e 6b 20 6f 66 20 74 68 65 20 27 3a 3a |think of the '::| 000012c0 27 20 73 79 6d 62 6f 6c 0a 61 73 20 61 6e 20 6f |' symbol.as an o| 000012d0 70 65 72 61 74 6f 72 20 74 68 61 74 20 22 6f 70 |perator that "op| 000012e0 65 6e 73 20 75 70 22 20 74 68 65 20 63 6c 61 73 |ens up" the clas| 000012f0 73 20 74 68 61 74 20 61 70 70 65 61 72 73 20 74 |s that appears t| 00001300 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20 69 74 |o the left of it| 00001310 0a 73 6f 20 74 68 61 74 20 74 68 65 20 64 61 74 |.so that the dat| 00001320 61 20 69 6e 20 74 68 65 20 63 6c 61 73 73 20 69 |a in the class i| 00001330 73 20 76 69 73 69 62 6c 65 20 69 6e 20 74 68 65 |s visible in the| 00001340 20 62 6f 64 79 20 6f 66 20 74 68 65 20 6d 65 74 | body of the met| 00001350 68 6f 64 0a 77 68 6f 73 65 20 6e 61 6d 65 20 61 |hod.whose name a| 00001360 70 70 65 61 72 73 20 74 6f 20 74 68 65 20 72 69 |ppears to the ri| 00001370 67 68 74 20 6f 66 20 69 74 2e 0a 0a 49 74 20 69 |ght of it...It i| 00001380 73 20 61 20 63 6f 6e 76 65 6e 74 69 6f 6e 20 62 |s a convention b| 00001390 6f 72 72 6f 77 65 64 20 66 72 6f 6d 20 43 2b 2b |orrowed from C++| 000013a0 2c 20 74 68 61 74 20 74 68 65 20 6d 65 74 68 6f |, that the metho| 000013b0 64 20 66 6f 72 20 63 72 65 61 74 69 6e 67 20 61 |d for creating a| 000013c0 6e 0a 69 6e 73 74 61 6e 63 65 20 6f 62 6a 65 63 |n.instance objec| 000013d0 74 20 61 6c 77 61 79 73 20 68 61 73 20 74 68 65 |t always has the| 000013e0 20 73 61 6d 65 20 6e 61 6d 65 20 61 73 20 74 68 | same name as th| 000013f0 65 20 63 6c 61 73 73 20 69 74 73 65 6c 66 2e 20 |e class itself. | 00001400 54 68 65 20 77 6f 72 64 0a 27 74 68 69 73 27 20 |The word.'this' | 00001410 69 6e 20 61 20 6d 65 74 68 6f 64 20 72 65 66 65 |in a method refe| 00001420 72 73 20 74 6f 20 74 68 65 20 69 6e 73 74 61 6e |rs to the instan| 00001430 63 65 20 6f 62 6a 65 63 74 20 69 74 73 65 6c 66 |ce object itself| 00001440 2e 20 54 68 65 20 77 6f 72 64 20 27 6e 65 77 27 |. The word 'new'| 00001450 0a 69 73 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f |.is used to allo| 00001460 63 61 74 65 20 6d 65 6d 6f 72 79 20 66 6f 72 20 |cate memory for | 00001470 61 20 6e 65 77 20 69 6e 73 74 61 6e 63 65 20 6f |a new instance o| 00001480 62 6a 65 63 74 2e 0a 0a 54 68 65 20 66 69 72 73 |bject...The firs| 00001490 74 20 6c 69 6e 65 20 69 6e 20 74 68 65 20 62 6f |t line in the bo| 000014a0 64 79 20 6f 66 20 74 68 65 20 77 69 74 68 64 72 |dy of the withdr| 000014b0 61 77 20 6d 65 74 68 6f 64 0a 0a 20 20 20 20 20 |aw method.. | 000014c0 20 20 20 20 20 20 6c 6f 63 61 6c 20 73 75 6d 3b | local sum;| 000014d0 0a 0a 64 65 63 6c 61 72 65 73 20 27 73 75 6d 27 |..declares 'sum'| 000014e0 20 74 6f 20 62 65 20 61 20 6c 6f 63 61 6c 20 76 | to be a local v| 000014f0 61 72 69 61 62 6c 65 2e 20 54 68 65 20 6b 65 79 |ariable. The key| 00001500 77 6f 72 64 20 27 6c 6f 63 61 6c 27 20 63 61 6e |word 'local' can| 00001510 20 6f 6e 6c 79 0a 6f 63 63 75 72 20 6f 6e 63 65 | only.occur once| 00001520 20 69 6e 20 61 20 6d 65 74 68 6f 64 20 6f 72 20 | in a method or | 00001530 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 |function definit| 00001540 69 6f 6e 2c 20 61 6e 64 20 74 68 65 6e 20 69 74 |ion, and then it| 00001550 20 68 61 73 20 74 6f 0a 61 70 70 65 61 72 20 61 | has to.appear a| 00001560 73 20 74 68 65 20 66 69 72 73 74 20 77 6f 72 64 |s the first word| 00001570 2e 20 54 6f 20 64 65 63 6c 61 72 65 20 6c 6f 74 |. To declare lot| 00001580 73 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20 6c |s of variables l| 00001590 6f 63 61 6c 20 6f 6e 65 20 6d 75 73 74 0a 77 72 |ocal one must.wr| 000015a0 69 74 65 0a 0a 20 20 20 20 20 20 20 20 20 20 6c |ite.. l| 000015b0 6f 63 61 6c 20 78 2c 20 79 2c 20 2e 2e 2e 2e 2e |ocal x, y, .....| 000015c0 2e 20 3b 0a 0a 77 69 74 68 20 74 68 65 20 76 61 |. ;..with the va| 000015d0 72 69 61 62 6c 65 73 20 73 65 70 61 72 61 74 65 |riables separate| 000015e0 64 20 62 79 20 63 6f 6d 6d 61 73 2e 20 4c 6f 63 |d by commas. Loc| 000015f0 61 6c 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 |al variables are| 00001600 20 70 72 69 76 61 74 65 0a 74 6f 20 74 68 65 20 | private.to the | 00001610 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 |function definit| 00001620 69 6f 6e 20 74 68 61 74 20 74 68 65 79 20 61 70 |ion that they ap| 00001630 70 65 61 72 20 69 6e 2e 0a 0a 54 68 65 20 73 74 |pear in...The st| 00001640 61 74 65 6d 65 6e 74 20 61 6e 64 20 63 68 61 6e |atement and chan| 00001650 67 65 5f 70 61 73 73 77 6f 72 64 20 6d 65 74 68 |ge_password meth| 00001660 6f 64 73 20 61 72 65 20 73 74 72 61 69 67 68 74 |ods are straight| 00001670 66 6f 72 77 61 72 64 2e 20 54 68 65 0a 68 61 73 |forward. The.has| 00001680 5f 6f 77 6e 65 72 20 6d 65 74 68 6f 64 20 69 73 |_owner method is| 00001690 20 75 73 65 64 20 74 6f 20 6c 6f 6f 6b 20 75 70 | used to look up| 000016a0 20 77 68 69 63 68 20 69 6e 73 74 61 6e 63 65 20 | which instance | 000016b0 6f 62 6a 65 63 74 73 20 6f 66 20 74 68 65 20 61 |objects of the a| 000016c0 63 63 6f 75 6e 74 0a 63 6c 61 73 73 20 62 65 6c |ccount.class bel| 000016d0 6f 6e 67 20 74 6f 20 61 20 67 69 76 65 6e 20 6f |ong to a given o| 000016e0 77 6e 65 72 2e 0a 0a 4e 65 78 74 20 77 65 20 77 |wner...Next we w| 000016f0 69 6c 6c 20 6c 6f 6f 6b 20 61 74 20 42 6f 62 3a |ill look at Bob:| 00001700 6d 61 69 6e 2e 61 63 63 6f 75 6e 74 2e 0a 0a |main.account...| 0000170f