A DYNAMICALLY ALLOCATED OBJECT

Examine the file named OBJDYNAM.CPP for our first look at a dynamically allocated object. This is not any different than any other dynamically allocated object, but an example is always helpful.

// objdynam.cpp
#include <iostream.h>

class box {
int length;
int width;
public:
box(void); //Constructor
void set(int new_length, int new_width);
int get_area(void);
};

box::box(void) //Constructor implementation
{
length = 8;
width = 8;
}

// 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);
}

main()
{
box small, medium, large; //Three boxes to work with
box *point; //A pointer to a box
small.set(5, 7);
large.set(15, 20);

point = new box; // Use the defaults supplied by the constructor

cout << "The small box area is " << small.get_area() << "\n";
cout << "The medium box area is " << medium.get_area() << "\n";
cout << "The large box area is " << large.get_area() << "\n";
cout << "The new box area is " << point->get_area() << "\n";
point->set(12, 12);
cout << "The new box area is " << point->get_area() << "\n";

delete point;
}

// Result of execution
//
// The small box area is 35
// The medium box area is 64
// The large box area is 300
// The new box area is 64
// The new box area is 144

In line 39 we declare a pointer to an object of type box and since it is only a pointer with nothing to point to, we dynamically allocate an object for it in line 44, with the object being created on the heap just like any other dynamically allocated variable. When the object is created in line 44, the constructor is called automatically to assign values to the two internal storage variables. Note that the constructor is not called when the pointer is declared since there is nothing to initialize. It is called when the object is allocated.

Reference to the components of the object are handled in much the same way that structure references are made, through use of the pointer operator as illustrated in lines 50 through 52. Of course you can use the pointer dereferencing method without the arrow such as (*point).set(12, 12); as a replacement for line 51 but the arrow notation is much more universal and should be used. Finally, the object is deleted in line 54 and the program terminates. If there were a destructor for this class, it would be called as part of the delete statement to clean up the object prior to deletion.

You have probably noticed by this time that the use of objects is not much different from the use of structures. Be sure to compile and execute this program after you have studied it thoroughly.

AN OBJECT WITH A POINTER TO ANOTHER OBJECT

The program named OBJLIST.CPP contains an object with an internal reference to another object of its own class. This is the standard structure used for a singly linked list and we will keep the use of it very simple in this program.

// objlist.cpp
#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 the current one points to
box *box::get_next(void)
{
return another_box;
}

main()
{
box small, medium, large; //Three boxes to work with
box *box_pointer; //A pointer to a box

small.set(5, 7);
large.set(15, 20);

cout << "The small box area is " << small.get_area() << "\n";
cout << "The medium box area is " << medium.get_area() << "\n";
cout << "The large box area is " << large.get_area() << "\n";

small.point_at_next(&medium);
medium.point_at_next(&large);

box_pointer = &small;
box_pointer = box_pointer->get_next();
cout << "The box pointed to has area " <<
box_pointer->get_area() << "\n";
}

// Result of execution
//
// The small box area is 35
// The medium box area is 64
// The large box area is 300
// The box pointed to has area 64

The constructor contains the statement in line 21 which assigns the pointer the value of NULL to initialize the pointer. This is a good idea for all of your programming, don’t allow any pointer to point off into space, but initialize all pointers to something. By assigning the pointer within the constructor, you guarantee that every object of this class will automatically have its pointer initialized. It will be impossible to overlook the assignment of one of these pointers.

Two additional methods are declared in lines 12 and 13 with the one in line 13 having a construct we have not yet mentioned in this tutorial. This method returns a pointer to an object of the box class. As you are aware, you can return a pointer to a struct in standard C, and this is a parallel construct in C++. The implementation in lines 48 through 51 returns the pointer stored within the object. We will see how this is used when we get to the actual program.

An extra pointer named box_pointer is declared in the main program for use later and in line 66 we make the embedded pointer within the small box point to the medium box, and in line 67 we make the embedded pointer within the medium box point to the large box. We have effectively generated a linked list with three elements. In line 69 we make the extra pointer point to the small box. Continuing in line 70 we use it to refer to the small box and update it to the value contained in the small box which is the address of the medium box. We have therefore traversed from one element of the list to another by sending a message to one of the objects. If line 70 were repeated exactly as shown, it would cause the extra pointer to refer to the large box, and we would have traversed the entire linked list which is only composed of three elements.

ANOTHER NEW KEYWORD this

Another new keyword is available in C++, the keyword this. The word this is defined within any object as being a pointer to the object in which it is contained. It is implicitly declared as;

class_name *this;

and is initialized 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. Use of the keyword this is not illustrated in a program at this point, but will be used in one of the larger example programs later in this tutorial.

You should study this program until you understand it completely then compile and execute it in preparation for our next example program.