• Tidak ada hasil yang ditemukan

C++ and object-oriented programming

even to understand what the program does (see Figure 10.8). Pascal avoids this by using instructions like ‘Repeat ... until’. Basic is cleaning up its act by incorporating this type of instruction into the more recent versions.

C

Apart from teaching good programming habits, Pascal was largely replaced by the language called C, invented a year after Pascal and allowing all the good practice programming methods of Pascal with a few extras (see Figure 10.9).

The main difference is that it is a lower-level language than Pascal which may seem a strange improvement. Its advantage is that it can control low-level features like memory loading that we last met in assembly without all the drawbacks of using assembly language. It has many high-level features, and low-level facilities when we require them and can produce very compact, and therefore fast, code.

the problem in terms of what processing is required to reach the required result. In object-oriented languages we have a number of objects which can be anything from some data, a diagram on a monitor screen, a block of text or a complete program. Once we have defined our objects, we can then allocate them to their own storage areas and define ways of acting on the entire object at the same time.

As an example, if we drew a square on the monitor screen and wished to move it, we could approach this in two ways. We could take each point on the screen and shift its position and hence rebuild the square in a different position. The object-oriented approach would be to define the shape as an object, then instruct the object to move. This is rather similar to our way of handling parts of the screen in a Windows environment. We use a mouse to take hold of an object, say a menu, and simply drag it to a new position. The menu is being treated as a single lump, which is an example of an object.

All the menus have similarities and differences. Similar objects are grouped into ‘classes’. A class includes the definition and type of the data together with the methods of manipulation that can be applied to an object. In our example of a class including the menus, each specific Figure 10.9

C

menu is called an ‘instance’. All of a class share some properties called

‘public’ properties and are different in some way, like different text being entered, these are called ‘private’ properties.

As we saw earlier, C++ is just standard C with the object-oriented extras added to it.

Java

Although not its original destiny, Java was found to be ideal for transmitting information over the Internet. It looks, at first glance, to be similar to C++ but it has some important advantages. It is small and does not require any particular architecture and can therefore be embedded in other applications for use in a wide range of systems.

This embedded Java code is called ‘Applets’ and is used in Internet Explorer and other browsers.

Fourth-generation languages

Fourth-generation languages are non-procedural and tend to concen- trate on what the program must do rather than the mechanics of the step-by-step approach of the procedural languages. A tempting Figure 10.10

C plus objects and Java

definition is to say they are the most recent, or the most popular or the

‘best’ languages. None of these definitions apply, so it may be better to stick to non-procedural as the most likely definition.

Lisp

Lisp was designed about the same time as Cobol and several years before Basic. It is the work of an American, John McCarthy (see Figure 10.11).

Lisp (LISt Processing) involves the manipulation of data which is held in lists or entered by keyboard and is associated with artificial intelligence. Lisp is a function-oriented language. This means that we define a function, such as add, subtract or more complex combina- tions. A list consists of a series of ‘members’ separated by spaces and enclosed in brackets. Samples of lists are (2 5 56 234) or (mother father son daughter).

A simple function defined as (PLUS 6 4) would return the answer 10 by adding the two numbers. Since it is an interpreted language, the program is executed one step at a time so inputted values are used as they are entered. In some versions of Lisp, this would have been written as (+ 6 4) using the mathematical symbol.

Figure 10.11

Lisp – dealing with lists

We can define our own function by saying

>(defun result (A B)(+A B))

defun = define function and A B are inputted numbers and the answers have been given the name ‘result’. So, if we input the numbers 4 and 5 we would receive the response 9. This has defined a function in which we enter two numbers A and B and they are added. We could use this to enter a list of values for A and B generating a list of all the results.

APL

The letters APL stand, reasonably enough, for ‘A Programming Language’. This is another interpreted language developed by IBM around 1962 and is only used for numerical data. It is a curious mixture of Lisp and Fortran. It combines the function orientation of Lisp with the terse procedural mathematics of Fortran (see Figure 10.12). It allows user-defined functions and has a large library of solutions to common problems. Most people would agree that given a choice, APL is not the language to learn if you are in a hurry. For example, the four basic functions of add, subtract, multiply and

Figure 10.12

A programming language

divide are present but even here, life is not easy. What would you expect the result of 2 * 3 to be? Well, it’s not 6. This would be written as 3 ⫻ 2 and 2 * 3 is actually 23 or 8. The statement: Value

← 4 – 2 four take away minus two giving an answer of plus six.

Notice the different symbols for minus and a negative number.

Other mathematical functions like sin, cos and tan are replaced by special symbols that do not appear on standard keyboards.

Easy, it is not. The good news is that, once mastered, it provides fast compact programs but the many cryptic statements would need to include many comments to help another person to understand your program.

Prolog

Prolog is called a ‘declarative’ language in which the program designer does not need to know exactly what the output will be when starting the design of the program. It was first developed in France in 1972 with a view to its use in the development of artificial intelligence.

Prolog stands for PROgramming by LOGic. Other versions were developed, such as DEC10, IC Prolog, which were produced in the UK and other versions from the US (see Figure 10.13).

Figure 10.13 A logic-based language

Prolog is another non-procedural language in that it is not a route to a goal but a set of information and a method from which the result can be deduced. Basically the idea is to feed in some facts and ask the program to produce some conclusions. You will remember the little logic puzzles like ‘Graham is married to Anne, Kirk is the son of Peter and the brother of Matt . . . Is Kirk the brother of . . .’ You know the sort of thing – more and more interconnected pieces of information until your head hurts. Just the job for Prolog.

The program includes facts and rules then we can ask questions. Here is a really simple example.

Facts:

coins (franc, france) coins (centime, france) coins (dollar, usa) coins (cent, usa) Rules:

french(x):–coins (x,france) american(x):–coins (x,american) Now we ask some questions:

?french (centime) answer: Yes

?french (dollar) answer: No OK so far, but:

?american (dime) answer: No

It has no data so it cannot say that dime is correct so it plays safe and says it is incorrect.

The future

Increasing microprocessor speeds and using several to share the processing tasks together with the decreasing size and cost of memory will be the key to the future. The idea of a desktop computer running at 20 GHz and having 128 Gbytes of memory is no longer ridiculous.

In fact, it is looking rather modest after looking at Table 10.1 in which the trend over the last 26 years is projected another 26 years into the future.

It cannot keep increasing’ – quote of the year 1972, 1973, 1974, 1975, 1976 . . .

A persistent occupant of my crystal ball is real spoken voice communication. A few years ago voice recognition was only a dream and is now a reality and becoming increasingly efficient. Voice synthesis is progressing nicely and is beginning to sound less robotic.

When these two technologies mature, simultaneous language transla- tion will not be far away and real dialog with the computer will begin.

Another million television programs suddenly become available without sub-titles (you see, all progress comes at a price!).

Six million, million instructions per second and 800 GHz clock speed together with total voice control of computer functions will be here in a few years.

How about that Mr Spock?

Fascinating’ That’s my guess.

At least it is something to look back and smile about in the future.

Quiz time 10

In each case, choose the best option.

1 A compiler:

(a) converts machine code to a high level language.

(b) is faster than an interpreter.

(c) is another name for a linker.

(d) is not available for the Basic language.

2 APL was largely influenced by:

(a) Cobol and Prolog.

(b) Lisp and Fortran.

(c) Fortran and Basic.

(d) Pascal and Cobol.

Table 10.1 Similarly priced microprocessor systems

Intel 4004 in 1972

Intel Pentium in 1998

Microprocessor in 2024

Clock speed 0.108 MHz 300 MHz 833 GHz

Memory 640 bytes 64 Mbytes 6.4 Tbytes

Performance (MIPS) 0.06 approx. 600 approx. 6 000 000

3 A fourth generation language can be described as a language which:

(a) is still being used.

(b) is object-oriented.

(c) was developed for artificial intelligence.

(d) is non-procedural.

4 A language designed to allow logical deductions to be made from input data is:

(a) C.

(b) Latin.

(c) Prolog.

(d) Fortran.

5 Pascal:

(a) is a low level language compared with C.

(b) is only of use if you are going to translate it to European languages.

(c) is a highly structured language.

(d) was the first popular high-level language.

11

The development of