The keyword THIS
Another new keyword available in C++ is
this. The word is definedwithin any object as a pointer to the object in which it is contained. It is
implicitly declared as
class_name *this;
and is initialised to point to the object for which the member function is
invoked. This pointer is most useful when working with pointers and
especially with a linked list when you need to reference a pointer to the
object you are inserting into the list. The keyword this is available for
this purpose and can be used in any object. Actually the proper way to
refer to any variable within a list is through use of the predefined
pointer this, by writing this->variable_name, but the compiler assumes
the pointer is used, and we can simplify every reference by omitting the
pointer. The keyword
this will be used in one of the larger exampleprograms later in this Module.
A linked list of objects
The next example program [OBJLINK.CPP] is a complete example of a
linked list written in object-oriented notation.
#include <iostream.h>
class box {
int length;
int width;
box *another_box;
public:
box(void); //Constructor
void set(int new_length, int new_width);
int get_area(void);
void point_at_next(box *where_to_point);
box *get_next(void);
};
box::box(void) //Constructor implementation
{
length = 8;
width = 8;
another_box = NULL;
}
// This method will set a box size to the two input parameters
void box::set(int new_length, int new_width)
{
length = new_length;
width = new_width;
}
// This method will calculate and return the
// area of a box instance
int box::get_area(void)
{
return (length * width);
}
// This method causes the pointer to point
// to the input parameter
void box::point_at_next(box *where_to_point)
{
another_box = where_to_point;
}
// This method returns the box that this one points to
box *box::get_next(void)
{
return another_box;
}
void main()
{
box *start = NULL; // Always points to the start of the list
box *temp; // Working pointer
box *box_pointer; // Used for box creation
// Generate the list
for (int index = 0 ; index < 10 ; index++ ) {
box_pointer = new box;
box_pointer->set(index + 1, index + 3);
if (start == NULL)
start = box_pointer; // First element
else
temp->point_at_next(box_pointer);
// Additional element
temp = box_pointer;
} // Print the list out
temp = start;
do {
cout << "The area is " << temp->get_area() << "\n";
temp = temp->get_next();
} while (temp != NULL); // Delete the list
temp = start;
do {
temp = temp->get_next();
delete start;
start = temp;
} while (temp != NULL);
}
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 33 to 39 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 44 to 46 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 48 to 57 to generate the list where line 50
dynamically allocates a new object of the box class and line 52 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 58 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 62
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 64 to 68.
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 well-written program.
Nesting objects
Examine the next program [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 desirable
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
characterised 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
Module, but for now we will look at the embedded or nested class.
#include <iostream.h>
class mail_info {
int shipper;
int postage;
public:
void set(int in_class, int in_postage)
{shipper = in_class; postage = in_postage; }
int get_postage(void) {return postage;}
};
class box {
int length;
int width;
mail_info label;
public:
void set(int l, int w, int s, int p) {
length = l;
width = w;
label.set(s, p); }
int get_area(void) {return length * width;}
};
void main()
{
box small, medium, large;
small.set(2, 4, 1, 35);
medium.set(5, 6, 2, 72);
large.set(8, 10, 4, 98);
cout << "The area is " << small.get_area() << "\n";
cout << "The area is " << medium.get_area() << "\n";
cout << "The area is " << large.get_area() << "\n";
}
This program contains a class named box which contains an object of
another class embedded within it in line 13, 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 18 where a message is sent to the label.set()
method to initialise 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 initialisation list can be given. This will be
discussed and illustrated later in this Module.
Parameterised Types, Class Template, and Exception Handling.
Links: Home C Programming Guide C++ programming Guide