The file named CONSPOLE.CPP introduces constructors and destructors and should be examined at this time.

This example program is identical to the last example except that a constructor has been added as well as a destructor. The constructor always has the same name as the class itself and is declared in line 8, then defined in lines 14 through18. The constructor is called automatically by the C++ system when the object is declared and can therefore be of great help in preventing the use of an uninitialized variable. When the object  named box is declared in line 46, the constructor is called automatically by the system. The constructor sets the values of height and width each to 6 in the object named box. This is printed out for reference in lines 49 and 50. Likewise, when the square is declared in line 46, the values of the height and the width of the square are each initialized to 6 when the constructoris called automatically.

A constructor is defined as having the same name as the class itself. In this case both are named rectangle. The constructor cannot have a return type associated with it since it is not permitted to have a user defined return type. It actually has a pre defined return type, a pointer to the object itself, but we will not be concerned about this until much later in this tutorial. Even though both objects are assigned values by the constructor, they are initialized in lines 58 and 59 to new values and

processing continues. Since we have a constructor that does theinitialization, we should probably rename the method namedinitialize() something else but it illustrates the concept involvedhere.

The destructor is very similar to the constructor except that it is called automatically when each of the objects goes out of scope. You will recall that automatic variables have a limited life time since they cease to exist when the enclosing block in which they were declared is exited. When an object is about to be automatically deallocated, its destructor, if one exists, is called automatically. A destructor is characterized as having the same name as the class but with a tilde prepended to the class name. A destructor has no return type.

A destructor is declared in line 11 and defined in lines 31 through35. In this case the destructor only assigns zeros to thevariables prior to their being deallocated, so nothing is reallyaccomplished. The destructor is only included for illustration ofhow it is used. If some blocks of memory were dynamicallyallocated within an object, a destructor should be used todeallocate them prior to losing the pointers to them. This wouldreturn their memory to the free store for further use later in theprogram.

It is interesting to note that if a constructor is used for anobject that is declared prior to the main program, otherwise knownas globally, the constructor will actually be executed prior to theexecution of the main program. In like manner, if a destructor isdefined for such a variable, it will execute following thecompletion of execution of the main program. This will notadversely affect your programs, but it is interesting to make noteof.


Examine the file named BOXES1.CPP for an example of how not to package an object for universal use. This packaging is actually fine for a very small program but is meant to illustrate to youhow to split your program up into smaller moremanageable files when you are developing a large program or whenyou are part of a team developing a large system. The next threeexample programs in this chapter will illustrate the proper methodof packaging a class.

This program is very similar to the last one with the pole structure dropped and the class named box. The class is defined in lines 4 through 12, the implementation of the class is given in lines 15 through 34, and the use of the class is given in lines 37through 50. With the explanation we gave about the last program, the diligent student should have no problem understanding this
program in detail.