22.1 Classes, objects and constructors

In object-oriented programming (OOP) we use a special kind of data structure called a class. A class is quite similar to an ordinary C struct. Particular instances of some class type are called objects. As well as having members which are data fields, a class also has members that are functions. If SomeClass were the name of a class and SomeFunction(…) happened to be the name of one of the class's member functions, then it would make sense to have two lines like:

SomeClass K; 

The first line says that K is an object of the class type SomeClass. You can also say that 'K is a SomeClass object' or you can say 'K is an instance of the SomeClass class.' Supposing K has a bunch of data fields, what gets put into these fields when you make the 'SomeClass K;' declaration? It turns out that when you define any class you also define a constructor function which serves to initialize new members of the class.

The second line says to let the object K call the class member function SomeFunction . SomeFunction might act on K's data members, do something to the SomeFunction arguments, possibly return a value, or do any combination of these three actions.

Sometimes a class's constructor function takes arguments. MFC includes, for instance, a CPoint class which has a constructor that can take two arguments, one for each of the point's coordinates. In this case you might have a declaration of the following form, creating a CPoint object with coordinates 3 and 7.

CPoint cpdot(3,7); 

It is allowable for a class to have several kinds of constructors. The constructors can be thought of as functions that you can use to create objects of the class type.

We need to mention still another way of initializing an object. Suppose that you want to have, say, a global SomeClass variable, but that you won't know what numbers to initialize the SomeClass with until well into your code. One approach that you could use would be to use a global pointer to a SomeClass.

SomeClass *sc_ptr; 

When you declare a pointer to a class like this, no initialization happens. No constructor gets called. Depending on the context, a junk value like CDCDCDCD0x or the NULL (zero) value gets put into the pointer, and no effort is made to create a legitimate class object for the pointer to point to.

The way that you make *sc_ptr correspond to a legitimate object is that later, down in the code when you find out what parameters, say x and y, you want to give to the SomeClass constructor, you put a line like the following.

sc_ptr = new SomeClass(x, y); 

In C++, new is a special operator which (a) allocates space for a new object of the specified class, (b) calls the class's constructor on the indicated arguments in order to initialize the object, and then (c) returns a pointer to this newly constructed object.

When you create an object of a given class type the constructor gets called; the constructor encapsulates the initialization and the allocation code. You can 'create' an object either by declaring it as a variable or by using the new operator to create an object and return a pointer to it.

Note that in Java, all class instance variables are of the pointer type. Java doesn't explicitly use the * symbol to indicate pointers, but the variables for objects are indeed pointer variables. This is why in Java you need to call new whenever you want to initialize an object variable. Something that makes the situation a little confusing is that Java also has primitive type variables such as integers that are not pointers.

Saying the same thing again in a different way:

in the Java language every class instance is a pointer.

Sometimes beginning Java programmers have the impression that 'Java has no pointers.' But exactly the opposite is true. Everything in Java is a pointer, other than primitives like int and char. The Java compiler will remind you of this if you try and compile code with a line like SomeClass nogood. You'll get an error message saying something about a NULL pointer. Java requires you to rewrite the offending line as SomeClass goodnow = new SomeClass().

    Part I: Software Engineering and Computer Games
    Part II: Software Engineering and Computer Games Reference