MORE NEW TERMINOLOGY
As with most new technologies, developers seem to delight in makingup new names for all aspects of their new pet. Object orientedprogramming is no different, so we must learn new names for someof our old familiar friends if we are going to learn how toeffectively use it. To help you learn this new programmingterminology, we will list a few of them here and begin using themin 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, itis only a pattern to be used to create a variable whichcan be manipulated in a program.
An object is an instance of a class, which is similar toa variable defined as an instance of a type. An objectis what you actually use in a program since it has valuesand can be changed.
A method is a function contained within the class. Youwill find the functions used within a class referred toas methods.
A message is the same thing as a function call. Inobject oriented programming, we send messages instead ofcalling functions. For the time being, you can think ofthem as identical. Later in this tutorial we will seethat they are in fact slightly different.
With all the new terminology, we will continue our study of theprogram named CLAS.CPP and show you how to use the class. We cannow say that we have a class composed of one variable and twomethods. The methods operate on the variable contained in theclass when they receive messages to do so. In this tutorial wewill use the terms object and variable interchangeably because bothnames 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 twomethods, and is our first example of the use of a prototype withina class. This is the reason we spent so much time on prototypesin the last chapter. You will notice line 7 which says that themethod named set requires one parameter of type int and returnsnothing, hence the return type is void. The method namedget_value() however, according to line 8, has no input parametersbut returns an int type value to the caller.
SENDING A MESSAGE
Following all of the definitions in lines 1 through 19, we finallycome to the program where we actually use the class. In line 23we declare three objects of the class one_datum and name theobjects dog1, dog2, and dog3. Each object contains a single datapoint which we can set through use of one method or read its valuethrough use of the other method, but we cannot directly set or readthe value of the data point because it is hidden within the “blockwall” 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 properlycalled sending a message to a method. Remember that the objectnamed dog1 has a method associated with it called set() that setsits internal value to the actual parameter included within themessage. You will notice that the form is very much like the meansof accessing the elements of a structure. You mention the name ofthe object with a dot connecting it to the name of the method. Ina similar manner, we send a message to each of the other twoobjects dog2 and dog3 to set their values to those indicated.
Lines 31 and 32 have been commented out because the operations areillegal since the variable named data_store is private and notavailable to the code outside of the object itself. It should beobvious, but it will be pointed out that the data contained withinthe object named dog1 is not available within the methods of dog2or dog3 because they are different objects. These rules are alldevised to help you develop better code more quickly and you willsoon 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, anothermessage is sent to each object and the returned result is outputto the monitor via the stream library.