MORE NEW TERMINOLOGY

As with most new technologies, developers seem to delight in making up new names for all aspects of their new pet. Object oriented programming is no different, so we must learn new names for some of our old familiar friends if we are going to learn how to effectively use it. To help you learn this new programming terminology, we will list a few of them here and begin using the min the text to get you used to seeing and using them.

A class is a grouping of data and methods (functions).A class is very much like a type as used in ANSI-C, it is only a pattern to be used to create a variable which can be manipulated in a program.

An object is an instance of a class, which is similar to a variable defined as an instance of a type. An object is what you actually use in a program since it has values and can be changed.

A method is a function contained within the class. You will find the functions used within a class referred to as methods.

A message is the same thing as a function call. In object oriented programming, we send messages instead of calling functions. For the time being, you can think of them as identical. Later in this tutorial we will see that they are in fact slightly different.

With all the new terminology, we will continue our study of the program named CLAS.CPP and show you how to use the class. We can now say that we have a class composed of one variable and two methods. The methods operate on the variable contained in the class when they receive messages to do so. In this tutorial we will use the terms object and variable interchangeably because both names are very descriptive of what the object really is.

This is a small point but it could be easily overlooked. Lines 7and 8 of this program are actually the prototypes for the two methods, and is our first example of the use of a prototype within a class. This is the reason we spent so much time on prototypes in the last chapter. You will notice line 7 which says that the method named set requires one parameter of type int and returns nothing, hence the return type is void. The method namedget_value() however, according to line 8, has no input parameters but returns an int type value to the caller.

SENDING A MESSAGE

Following all of the definitions in lines 1 through 19, we finally come to the program where we actually use the class. In line 23we declare three objects of the class one_datum and name the objects dog1, dog2, and dog3. Each object contains a single data point which we can set through use of one method or read its value through use of the other method, but we cannot directly set or read the value of the data point because it is hidden within the “block wall” around the class. In line 26, we send a message to theobject named dog1 instructing it to set its internal value to 12,and even though this looks like a function call, it is properly called sending a message to a method. Remember that the object named dog1 has a method associated with it called set() that setsits internal value to the actual parameter included within the message. You will notice that the form is very much like the means of accessing the elements of a structure. You mention the name of the object with a dot connecting it to the name of the method. Ina similar manner, we send a message to each of the other two objects dog2 and dog3 to set their values to those indicated.

Lines 31 and 32 have been commented out because the operations are illegal since the variable named data_store is private and not available to the code outside of the object itself. It should be obvious, but it will be pointed out that the data contained within the object named dog1 is not available within the methods of dog2or dog3 because they are different objects. These rules are all devised to help you develop better code more quickly and you will soon see how they help.

The other method defined for each object is used in lines 34through 36 to illustrate how it can be used. In each case, another message is sent to each object and the returned result is output to the monitor via the stream library.