CONSTRUCTORS AND DESTRUCTORS
The file named CONSPOLE.CPP introduces constructors and destructors and should be examined at this time.
This example program is identical to the lastexample except that a constructor has been added as well as adestructor. The constructor always has the same name as the classitself and is declared in line 8, then defined in lines 14 through18. The constructor is called automatically by the C++ system whenthe object is declared and can therefore be of great help inpreventing the use of an uninitialized variable. When the objectnamed box is declared in line 46, the constructor is calledautomatically by the system. The constructor sets the values ofheight and width each to 6 in the object named box. This isprinted out for reference in lines 49 and 50. Likewise, when thesquare is declared in line 46, the values of the height and thewidth of the square are each initialized to 6 when the constructoris called automatically.
A constructor is defined as having the same name as the classitself. In this case both are named rectangle. The constructorcannot have a return type associated with it since it is notpermitted to have a user defined return type. It actually has apredefined return type, a pointer to the object itself, but we willnot 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 itis called automatically when each of the objects goes out of scope.You will recall that automatic variables have a limited lifetimesince they cease to exist when the enclosing block in which theywere declared is exited. When an object is about to beautomatically deallocated, its destructor, if one exists, is calledautomatically. A destructor is characterized as having the samename 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 polestructure dropped and the class named box. The class is definedin lines 4 through 12, the implementation of the class is given inlines 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.