A LINKED LIST OF OBJECTS

The next example program in this chapter is named OBJLINK.CPP and is a complete example of a linked list written in object oriented notation.

This program is very similar to the last one. In fact it is identical until we get to the main program. You will recall that in the last program the only way we had to set or use the embedded pointer was through use of the two methods named point_at_next() and get_next() which are listed in lines 40 through 51 of the present program. We will use these to build up our linked list then traverse and print the list. Finally, we will delete the entire list to free the space on the heap.

In lines 56 to 58 we declare three pointers for use in the program. The pointer named start will always point to the beginning of the list, but temp will move down through the list as we create it. The pointer named box_pointer will be used for the creation of each object. We execute the loop in lines 61 through 69 to generate the list where line 62 dynamically allocates a new object of the box class and line 63 fills it with nonsense data for illustration. If this is the first element in the list, the start pointer is set to point to this element, but if elements already exist, the last element in the list is assigned to point to the new element. In either case, the temp pointer is assigned to point to the last element of the list, in preparation for adding another element if there is another element to be added.

In line 72, the pointer named temp is pointed to the first element and it is used to increment its way through the list by updating itself in line 75 during each pass through the loop. When temp has the value of NULL, which it gets from the last element of the list, we are finished traversing the list.

Finally, we delete the entire list by starting at the beginning and deleting one element each time we pass through the loop in lines 79 through 84.

A careful study of the program will reveal that it does indeed generate a linked list of ten elements, each element being an object of class box. The length of this list is limited by the

practicality of how large a list we desire to print out, but it could be lengthened to many thousands of these simple elements provided you have enough memory available to store them all.

Once again, the success of the dynamic allocation is not checked as it should be in a correctly written program. Be sure to compile and execute this example program.

NESTING OBJECTS

Examine the program named NESTING.CPP for an example of nesting classes which results in nested objects.

A nested object could be illustrated with your computer in a rather simple manner. The computer itself is composed of many items which work together but work entirely differently, such as a keyboard, a disk drive, and a power supply. The computer is composed of these very dissimilar items and it is desireable to discuss the keyboard separately from the disk drive because they are so different. A computer class could be composed of several objects that are dissimilar by nesting the dissimilar classes within the computer class.

If however, we wished to discuss disk drives, we may wish to examine the characteristics of disk drives in general, then examine the details of a hard disk, and the differences of floppy disks. This would involve inheritance because much of the data about both drives could be characterized and applied to the generic disk drive then used to aid in the discussion of the other three. We will study inheritance in the next three chapters, but for now we will look at the embedded or nested class.

This example program contains a class named box which contains an object of another class embedded within it in line 16, the mail_info class. This object is available for use only within the class implementation of box because that is where it is defined. The main program has objects of class box defined but no objects of class mail_info, so the mail_info class cannot be referred to in the main program. In this case, the mail_info class object is meant to be used internally to the box class and one example is given in line 21 where a message is sent to the label.set() method to initialize the variables. Additional methods could be used as needed, but these are given as an illustration of how they can be called.

Of prime importance is the fact that there are never any objects of the mail_info class declared directly in the main program, they are inherently declared when the enclosing objects of class box are declared. Of course objects of the mail_info class could be declared and used in the main program if needed, but they are not in this example program. In order to be complete, the box class

should have one or more methods to use the information stored in the object of the mail_info class. Study this program until you understand the new construct, then compile and execute it.

If the class and the nested classes require parameter lists for their respective constructors an initialization list can be given. This will be discussed and illustrated later in this tutorial.

Share
Tweet
Share
Pin