|
Expand
|
Minimize
|
ClassesSay we wanted to extend the counter example discussed
previously. Perhaps in our Modula-2 program we need three
counters. We could define an array of
The point of all this is that we are talking in terms of data structures; all of the above discussion has nothing to do with the behaviour of the counter itself. When programming with objects we can ignore anything not directly concerning the behaviour or state of an object; we instead turn our attention to classes. A class is a blueprint for an object. What this basically means is that we provide a blueprint,
or an outline of an object. This blueprint is valid whether
we have one or one thousand such objects. A class
does not represent an object; it represents
all the information a typical object should have as well as
all the methods it should have. A class can be considered to
be an extremely extended
C++As an example, lets give the C++ class definition for our counter object.
class Counter {
private:
int MyCounter
public:
Counter() {
MyCounter = 0;
}
void InitialiseCounter(int value) {
MyCounter = value;
}
void IncrementCounter() {
MyCounter++;
}
int GetCounterValue() {
return (MyCounter);
}
}
A lot to go through for this little example. You really need to understand the fundamentals of C before the example will make any sense.
InstantiationThis is an awful big word for a powerfully simple concept. All we have done so far is to create a class, i.e. a specification for our object; we have not created our object yet. To create an object that simulates a counter in C++ then, all we have to do is declare in our main program: Counter i; Although this seems just like an ordinary variable
declaration, this is much more. The variable
When we first instantiate an object (i.e. when we first
declare, or create it), the
class constructor is called. The class constructor
is the method with the same name as the class definition. This
method should contain any start-up code for the object; any
initialisation of object variables should appear within this
method. In the counter example, whenever we create a new
counter object, the first thing that happens to the object is
that the variable
Remember the question posed at the very start? The power of objects starts to kick in now. Say we require another counter within our program. All we have to do is declare a new object, say: Counter j;
Although the new counter shares the same blueprint as the previous object,
it shares none of the same data. What this means is that
Counter loads[1000]; and then initialize one of them using a call such as
JavaThe equivalent Java class definition for the counter example follows. It is remarkably similar to the C++ definition, and differs only in syntax.
class Counter extends Object {
private int MyCounter;
Counter() {
MyCounter = 0;
}
public void InitialiseCounter(int value) {
MyCounter = value;
}
public void IncrementCounter(void) {
MyCounter++;
}
public int GetCounterValue(void) {
return (MyCounter);
}
}
A few brief notes about the differences:
Instantiating objects in Java is slightly different; objects are declared differently: Counter i; i = new Counter(); Basically we define a variable to reference the object in
the first line. Then we actually create an instance of the
object by a call to
Why Bother?The process of designing and programming objects seems very cumbersome, so why bother? Well, it's difficult to see from such a small example, but for larger projects, OOP techniques allow a great deal of flexibility. OOP is used for the following reasons.
Next: Inheritance. |
|
|
©2005 Contact
|