📄 chap06.txt
字号:
always helpful.
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 OBJLIST.CPP
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.
Page 6-5
Chapter 6 - More Encapsulation
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
Page 6-6
Chapter 6 - More Encapsulation
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.
A LINKED LIST OF OBJECTS
_________________________________________________________________
The next example program in this chapter is ===============
named OBJLINK.CPP and is a complete example of OBJLINK.CPP
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
Page 6-7
Chapter 6 - More Encapsulation
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 NESTING.CPP
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
Page 6-8
Chapter 6 - More Encapsulation
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.
OPERATOR OVERLOADING
_________________________________________________________________
The example file named OPOVERLD.CPP contains ================
examples of overloading operators. This allows OPOVERLD.CPP
you to define a class of objects and redefine ================
the use of the normal operators. The end result
is that objects of the new class can be used in
as natural a manner as the predefined types. In fact, they seem
to be a part of the language rather than your own add-on.
In this case we overload the + operator and the * operator, with
the declarations in lines 10 through 12, and the definitions in
lines 16 through 40. The methods are declared as friend functions
so we can use the double parameter functions as listed. If we did
not use the friend construct, the function would be a part of one
of the objects and that object would be the object to which the
message was sent. Including the friend construct allows us to
separate this method from the object and call the method with infix
notation. Using this technique, it can be written as object1 +
object2 rather than object1.operator+(object2). Also, without the
friend construct we could not use an overloading with an int type
variable for the first parameter because we can not send a message
to an integer type variable such as int.operator+(object). Two of
the three operator overloadings use an int for the first parameter
so it is necessary to declare them as friend functions.
There is no upper limit to the number of overloadings for any given
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -