CIS Logo SVC Logo

   Computing & Information Systems
   Department

 

Schoology Facebook        Search CIS Site      Tutorials

Software Design Using C++



Using Objects



Introduction


An 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 Example


Let'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:


int Area1, Area2, Perim1, Perim2;
Rectangle First(14, 5);
Rectangle Second(10, 3);

Area1 = First.Area();
Area2 = Second.Area();
Perim1 = First.Perimeter();
Perim2 = Second.Perimeter();
First.SetWidth(7);

This is not a complete program, of course. It is just a section of code that shows how we might use a couple of Rectangle objects. The second and third lines show how to create objects of type Rectangle. (You can also say that we are creating variables named First and Second of type Rectangle). The Rectangle object named First is apparently being created to have length 14 and width 5. (We would have to read the comments for the Rectangle class to know such details for sure.) The Rectangle object named Second is initialized to have length 10 and width 3. The next statement calls the Area function on the object First, with the answer assigned into the variable Area1. Note the syntax for calling a class function (also called a method) on an object:


object.function(parameter_list)

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 SetWidth function is used to change the width of the rectangle to which it is applied. Thus we are here changing the width of the rectangle named First to 7.

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 GetWidth function, for example, might be used to return the current width of the rectangle to which the function is applied. The syntax would be as follows, if we wanted to find the width of rectangle Second:


W = Second.GetWidth();

We are assuming of course that W is an integer variable and that the GetWidth function requires no parameters. Once again we would look at the comments, the documentation, for the Rectangle class in order to know what parameters are needed for this class function.

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 Program


For 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 ListClass are the ones that are available for use in writing an application program such as this. The use of these functions can be understood by reading the comments next to each.

The ListClass function contains the code to initialize a new, empty list. We have no need to examine the details here. We will not even look at the ~ListClass function; you will study that type of function later. NumItems is a useful function that tells us the number of items currently in the list to which the function is applied. Similarly, the Empty function tells us whether or not the list is empty. Note how it returns a boolean (true/false) value. The InsertFront function insert a new item (given as the parameter to the function) at the front of the list. InsertRear is similar, but inserts the new item at the rear of the list. Thus we can add data to either end of a list. Finally, the RemoveFront function removes an item from the front of the list, returning that item in the function name. See the comment section for each function for complete details.

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 ListA. The type name for the variable is ListClass. When there are no parameters to pass in creating an object, no parentheses are used. (You may have noted that this is an exception to the general rule that a parameter list -- even if it is empty -- must be given whenever a function is called.) If the ListClass constructor had parameters we would expect to see something like ListClass ListA(parameters) instead.


ListClass ListA;
int Result;

ListA.InsertFront(40);
ListA.InsertFront(5);
ListA.InsertRear(72);
ListA.InsertRear(100);

cout << "Number of items in ListA: " << ListA.NumItems()
   << endl << endl;

while (! ListA.Empty())
   {
   Result = ListA.RemoveFront();
   cout << "Removed from front of ListA " << Result << endl;
   }

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:


object.function(parameter_list)

Next, the program uses the NumItems function to look up the number of items in the list. (Clearly this should be 4.) Since this function returns the answer in the function name, the function call can be embedded directly in an output statement.

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 Empty function returns a boolean value, it does make sense to take NOT of that value and to use that as the condition in the WHILE loop. Note that we are indeed accessing the data of the list sequentially, as we are pealing off one number at a time from the front of the list.

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

Back to the main page for Software Design Using C++

Author: Br. David Carlson with contributions by Br. Isidore Minerd
Last updated: August 27, 2009
Disclaimer