What is an Object Orientated Programming Language?
OOP languages are languages in which objects can be implemented.
So what is an object?
Using real world examples, a telephone in a telephone exchange system, an item in an adventure game, an invoice in a Purchase Order System, a robot in a factory etc. The list is endless, anything that can be thought of as a unit of functionality could be an object.
So what is an object in terms of OOPL?
The best definition that I can find is as follows: "An object is data combined with procedures that endow the data with behaviors." An object represents an distinct part of a program. It is complete in itself and includes the capability to act on its internal members. An object, however, still can communicate with the outside world. To make this concept complete, it is necessary for the object to bar outside access to its internal parts except through a carefully controlled interface (Encapsulation). Encapsulation extends the concept of modularity. C has function and file scope, C++ has function, file and class scope.
What makes a language an OOPL?
Everybody will argue about this but at the simplest level it may be taken to be a language which can satisfy the following three points:
Various texts on the theory of OO debate this quite hotly, but for the purpose of a starter's guide it is more than adequate. This is a simplistic view of the world of objects, i.e. not necessarily the academic world's view.
What is encapsulation?
A facility allowing access to data to be severely restricted (called information hiding). It allows for the data and the modules needed to manipulate that data to be tightly bound together. The data cannot then be accessed directly, but only through the modules defined.
What use is this?
It allows an object designer to define how an object will be used and how it may not be used. e.g. A queue can be implemented as an object. The designer allows the function PUSH to load information onto the queue and POP to remove information from the queue. He may not wish the programmers to have any other form of access to the queue, so he does not implement any other form of access in his class. The programmer does not need to be aware of how the queue works, only how to store and retrieve the data on the queue.
But I could implement such a system in my current programming language!
Yes you could. But if you needed two queues, you would need either 2 queue modules (copies of each other) or a module handling each of the queues in a complex series of arrays or by passing the queue information and its contents to the function that will handle its processing, making the programming and testing that much more difficult. In C++ a queue object would only have to be named a second time to be usable.
QUEUE Queue_One; QUEUE Till; // Array of 90 queues QUEUE* X;
(Pointer to a queue X= new QUEUE[rqd]; queues created dynamically)
What is Polymorphism?
It literally means many forms. It allows two or more modules to be called by the same name and to leave the compiler to sort out which module to use based on the differences in the parameter list. (When you have been using C++ a bit longer you will find this is an oversimplification).
position(int xpos, int ypos); position(int xpos, int ypos, int colour); position(reference georges_position);
What use is this?
Instead of having several similar procedure names, the same name can be used. The same principle can be applied to operators, such that addition between a defined pair of classes (or a class and built-in types).This is often called overloading. Procedures, operators and members of classes can all be overloaded.
What is inheritance?
An attribute of the language that describes the ability to define a new class, based on an existing class (i.e. inheriting all its features) and then adding its own custom ones (or removing undesirable ones).
What use is this?
Take for example a graphics class (for drawing shapes on the graphics screen). We could define a class called shape. In this class is held data about the shape (position, colour etc) and also a series of modules that manipulate the shape and its data. What it will not have is any ability to draw the shape as it is only a general concept of we can do with a shape. This general class (called an abstract class because it cannot be used in its own right), however, can be inherited by more specific classes such as square or circle. These classes would contain information as to the size of the specific shape as well as how to draw it. All the general stuff, such as how to move it across the screen or make it change colour are inherited from its base class.
This also means that any enhancements or changes to the capabilities of the base class are perpetuated through all the classes that inherit that base class.
Why have we jumped from the word object to the word class?
It is important to understand the differences between an object and a class. Until now, this has been ignored. A class is an abstract definition of data and processing. An object is an instance of a class. The relationship between the two is analogous to that of the keyword 'int' and a variable of type int.
And now for the silly bit!
So far all this article has demonstrated, is that an awful lot of words can be written to describe what an object is. They say a picture is worth a thousand words. So rather than write another 4,000 words I'm going to draw a piccy.
The way I visualise an object in 2 dimensions is like a doughnut. The inside of the doughnut represents the protected portion of an object. The outside represents the public interface to the object. I.E. nothing outside the doughnut can get at the centre, only things that are part of the doughnut can access the centre. The best diagrams of this sort can be found in Taylor's book (OO a Managers Guide), reviewed in this issue of Overload.
|In Search of the Silver Bullet!|
Every time I read an article that mentions the words 'Silver Bullet' with either 'C++' or 'Object-Orientation' my hackles rise. I know what is about to happen! The author is about a give a brief outline of OO theory or C++'s capability and then proceed to shoot it full of holes and proclaim that we have still not found the Silver Bullet.
Can you imagine how Wilbur and Orville Wright would have felt if they could have seen a modern Jet Fighter just after their inaugural flight in 1903. They would have thought that within 100 years of their invention, flying perfection had been attained. Some historian in the year 2100 AD will probably be pointing out to a class of students just how crude our flying machines were in the 1990s.
Software follows a similar pattern. 35 years ago, dinosaurs ruled the computer world. The use of Assembler was more prolific than those newfangled COBOL and FORTRAN programming languages, and people probably thought that they had found as near perfection as they were going to. I remember reading one of those '25 Years ago' articles, in which a mathematics professor from Oxford or Cambridge said that with his latest machine, he had virtually all the computing power he could ever envisage using. His machine had but 1K of RAM!!! Now we have advanced, and OO based languages are making inroads into what I would call conventional languages. These OO languages are not the final solution, but merely the best we have to offer at the moment.
The world moves on, and people's points of reference move with it. Todays wonderful invention is tomorrows norm and next week's old hat. I wish authors would stop comparing OO/C++ with Silver Bullets; what do they think we are - Lycanthropes?