📄 chap11.txt
字号:
Chapter 11 - More Virtual Functions
present example program by changing line 10 of PERSON.H to read as
follows;
virtual void display(void) = 0;
You must then eliminate PERSON.CPP from the project or make
sequence. An implementation for a pure virtual function cannot
exist in the base class.
Every derived class must include a function for each pure virtual
function which is inherited into the derived class. This assures
that there will be a function available for each call and none will
ever need to be answered by the base class. You are not permitted
to create an object of any class which contains one or more pure
virtual functions because there is nothing to answer a message if
one is sent to the pure virtual method. The compiler will enforce
the two rules mentioned in this paragraph.
THE LINKED LIST CLASS
_________________________________________________________________
Examination of the file named ELEMLIST.H will ================
reveal the definition of two more classes which ELEMLIST.H
will be used to build a linked list of employees ================
to illustrate a more practical way to use the
dynamic binding we have been studying in this
chapter.
The two classes were put in the same file because they work
together so closely and neither is of much value without the other.
You will notice that the elements of the linked list do not contain
any data, only a pointer to the person class that we developed for
the last program, so that the linked list will be composed of
elements of the person class without modifying the class itself.
There are two interesting constructs used here that must be pointed
out before going on to the next program. The first is the partial
declaration given in line 8 which allows us to refer to the class
named employee_list before we actually define it. The complete
declaration for the class is given in lines 22 through 29. The
second construct of interest is the friend class listed in line 17
where we give the entire class named employee_list free access to
the variables which are a part of the employee_element class. This
is necessary because the method named add_person() must access the
pointers contained in employee_element. We could have defined an
additional method as a part of employee_element and used this
method to refer to the pointers but it was felt that these two
classes work so well together that it is not a problem to open a
window between the classes. We still have complete privacy from
all other programs and classes declared as parts of this program.
Page 11-4
Chapter 11 - More Virtual Functions
Note that the single method included in the employee_element class
is implemented in inline code. Two of the methods of employee_list
are still open so we need an implementation for this class.
THE LINKED LIST IMPLEMENTATION
_________________________________________________________________
The file named ELEMLIST.CPP is the ================
implementation for the linked list classes and ELEMLIST.CPP
should be self explanatory if you understand how ================
a singly linked list operates. All new elements
are added to the end of the current list. This
was done to keep it simple but a sorting mechanism could be added
to sort the employees by name if desired.
The method to display the list simply traverses the list and calls
the method named display() in line 30 once for each element of the
list.
It is important for you to take notice that in this entire class,
there is no mention made of the existence of the three derived
classes, only the base class named person is mentioned. The linked
list therefore has no hint that the three subclasses even exist,
but in spite of that, we will see this class send messages to the
three subclasses as they are passed through this logic. That is
exactly what dynamic binding is, and we will have a little more to
say about it after we examine the calling program.
THE LINKED LIST IMPLEMENTATION
_________________________________________________________________
At this time you should examine the final ================
example program in this chapter named EMPLOYE2.CPP
EMPLOYE2.CPP for our best example of dynamic ================
binding in this tutorial, yet the program is
kept very simple.
This program is very similar to the example program named
EMPLOYEE.CPP with a few changes to better illustrate dynamic
binding. In line 7 we declare an object of the class employee_list
to begin our linked list. This is the only copy of the list we
will need for this program. For each of the elements, we allocate
the data, fill it, and send it to the linked list to be added to
the list where we allocate another linked list element to point to
the new data, and add it to the list. The code is very similar to
the last program down through line 40.
In line 43 we send a message to the display_list() method which
outputs the entire list of personnel. You will notice that the
linked list class defined in the files named ELEMLIST.H and
Page 11-5
Chapter 11 - More Virtual Functions
ELEMLIST.CPP are never informed in any way that the subclasses even
exist but they dutifully pass the pointers to these subclasses to
the correct methods and the program runs as expected.
If you changed PERSON.H to use a pure virtual function, it will
still work with this program just as we discussed earlier.
WHAT GOOD IS ALL OF THIS
_________________________________________________________________
Now that we have the program completely debugged and working,
suppose that we wished to add another class to the program, for
example a class named consultant because we wished to include some
consultants in our business. We would have to write the class of
course and the methods within the classes, but the linked list
doesn't need to know that the new class is added, so it does not
require any changes in order to update the program to handle
consultant class objects. In this particular case, the linked list
is very small and easy to understand, but suppose the code was very
long and complex as with a large database. It would be very
difficult to update every reference to the subclasses and add
another subclass to every list where they were referred to, and
this operation would be very error prone. In the present example
program, the linked list would not even have to be recompiled in
order to add the new functionality.
It should be clear to you that it would be possible to actually
define new types, dynamically allocate them, and begin using them
even while the program was executing if we properly partitioned the
code into executable units operating in parallel. This would not
be easy, but it could be done for a large database that was
tracking the inventory for a large retail store, or even for an
airlines reservation system. You probably have little difficulty
understanding the use of dynamically allocated memory for data, but
dynamically allocating classes or types is new and difficult to
grasp, but the possibility is there with dynamic binding.
PROGRAMMING EXERCISES
_________________________________________________________________
1. Add a new class named consultant to the files named SUPERVSR.H
and SUPERVSR.CPP, then add code to EMPLOYE2.CPP to exercise
the new class. Note that you do not need to recompile the
linked list class in order to execute the new code and use the
new class. Even without recompiling the linked list class it
is capable of storing and passing the new class of data
provided of course that the new class is referred to using a
pointer to the parent class.
Page 11-6
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -