Software Design Using C++Using ObjectsIntroductionAn object is typically used to hold some data. Unlike an array, however, an object has associated with it some functions for manipulating the data. Both data and functions are packaged up together. A class is the name given to the description of all possible
objects of a given type. Thus, a class contains a description of both
the kinds of data that can be stored and the functions that can be used to manipulate that data.
A Simple ExampleLet's go through an example. Suppose that we have a class named Rectangle . This class tells us that each
Rectangle object represents a rectangle
with a certain length and width. Furthermore, the class
tells us that it has functions available to manipulate
Rectangle objects. For example, there might be an
Area function that calculates the area of a given
rectangle. There might also be a Perimeter
function to find the perimeter of a rectangle. The following section
of code shows how we might use Rectangle objects:
This is not a complete program, of course. It is just a section of code
that shows how we might use a couple of
This is the syntax to use whenever you call a class function on an object. Note that the name of the object comes first. Thus the emphasis is on the object (which contains the data), more than on the function (which contains the executable code). In a similar way, the above example calculates the area of the other triangle and then finds the perimeter of each rectangle.
The last line in the above example is the only one that has a non-empty
parameter list. The idea (which we could verify by reading the comments
for the class) is that the
It is very common to have functions with names starting with "Set". These
are used to place data into an object or to replace data already in an
object. Functions that do the opposite are often named starting with "Get".
A
We are assuming of course that Nowhere have we given any code for the class functions, nor have we detailed exactly what is in an object of the class. That will be studied later in the intermediate section on objects and classes. For now, we will just use classes that have already been written for us. In the later section you will learn to write some of your own. There are large libraries of classes that are available for use by programmers. For example, there is the Microsoft Foundation Class (MFC) library and the Standard Template Library (STL). The Standard Template Library is studied later in the advanced section of these Web pages. Both the MFC and STL are included with Visual C++. A Complete ProgramFor a complete program illustrating the use of objects, read through list1.cpp. Actually, in reading this example, only read the comments and the main function at the bottom. The code in the middle section which sets up the class and its functions will not be studied here. As in the above example, we will simply use a class that has already been written for us. The comments will tell us enough about how to use the class to write a useful program. As the comments indicate, this example program creates a list of integers. A list holds a sequence of data items, all of the same type. This sounds a lot like an array, but there is no index with a list and there is no way to go directly to the item at "index" 4 (or position 4 in some more general sense). A list is very much a sequential data structure. One cannot jump around in it or go directly to a particular data item. The only way to access data in a list is to go through the data in order (in sequence).
The "public" functions of class
The
The main function, which is all we are really interested in here, is pretty simple.
The code has been copied in below. It begins by creating an empty list
named
The first thing we do with the new list is to insert some data into it. We insert 40 at the front of the original, empty list. Then we insert 5 in front of that. Then we insert 72 at the rear of the list and finally insert 100 after that. Note that if all works properly the numbers should now be in the list in numerical order. This is not necessary, but it makes it easy for us to tell if the numbers were correctly placed. Note once again the standard syntax for calling a class function on an object. All 4 insertions use the same syntax:
Next, the program uses the
Finally, we have a WHILE loop where we keep removing an item from the front
of the list and printing the item. This is done as long as the list is
not empty. Since the Well-designed classes of objects can be very useful to the programmer. Their ability to package up data and function code help a lot in designing large programs. They also make it much easier to reuse code. A class can often be used in several programs, so that the programmer can start out on a new project with a fair amount of useful code already written and well-documented. Related Items
|