home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Education
/
collectionofeducationcarat1997.iso
/
COMPUSCI
/
CPPTUT.ZIP
/
CHAP10.TXT
< prev
next >
Wrap
Text File
|
1990-07-20
|
18KB
|
412 lines
Chapter 10
VIRTUAL FUNCTIONS
Once again we are into a completely new topic with terminology
which will be completely new to you. If you are new to object
oriented programming, you should follow along in this chapter very
carefully because every attempt has been made to define every
detail of this new and somewhat intimidating topic. However, if
you are well versed in object oriented programming, simply learning
to use C++, you may wish to skip the first four programs in this
chapter and go directly to the example program named VIRTUAL5.CPP
and continue from there to the end of the chapter.
One term which must be defined is polymorphism, a rather large word
that simply means similar when used in the context of object
oriented programming. Objects are polymorphic if they have some
similarities but are still somewhat different. We will see how it
is used in the context of object oriented programming as we proceed
through this chapter.
We have already studied operator overloading and function
overloading in this tutorial, and they are a subtle form of
polymorphism since in both cases, a single entity is used to refer
to two or more things. The use of virtual functions can be a great
aid in programming some kinds of projects as you will see in the
next two chapters.
A SIMPLE PROGRAM WITH INHERITANCE
_________________________________________________________________
Examine the example program named VIRTUAL1.CPP ================
for the basic program outline we will use for VIRTUAL1.CPP
all discussion in this chapter. Since this ================
program has nothing to do with virtual
functions, the name may be somewhat misleading.
It is named VIRTUAL1.CPP because it is part of a series of programs
intended to illustrate the use of virtual functions. The last
program in this chapter will illustrate the proper use of virtual
functions.
The first program is very simple and you will recognize it as being
somewhat similar to the programs studied in the last chapter except
that this program is greatly simplified in order to effectively
instruct you in the use of a virtual function. You will notice
that many of the methods from the last chapter have been completely
dropped from this example for simplicity, and a new method has been
added to the parent class, the method named message() in line 8.
Throughout this chapter we will be studying the operation of the
method named message() in the parent class and the derived classes.
For that reason, there is a method named message() in the car class
as well as in the new class named boat in lines 27 through 32.
Page 10-1
Chapter 10 - Virtual Functions
You will also notice that there is a lack of a method named
message() in the truck class. This has been done on purpose to
illustrate the use of the virtual method, or if you prefer, you can
refer to it as a virtual function. You will recall that the method
named message() from the parent class is available in the truck
class because the method from the parent class is inherited with
the keyword public included in line 19. You will also notice that
the use of the keyword public in lines 12 and 27 actually do
nothing because the only method available in the parent class is
also available in the derived classes. There are no methods
actually inherited. Leaving the keyword in the header poses no
problem however, so it will be left there for your study.
The method named message() in the parent class and in the derived
classes has been kept very simple on purpose. Once again, we are
interested in the technique of the virtual method rather than a
long complicated example.
The main program is as simple as the classes, one object of each
of the classes is declared in lines 37 through 40 and the method
named message() is called once for each object. The result of
executing the program indicates that the method for each is called
except for the object named semi, which has no method named
message(). As discussed in the last chapter, the method named
message() from the parent class is called and the data output to
the monitor indicates that this did happen since it displays
"Vehicle message" for the object named semi.
The data for the objects is of no concern in this chapter so all
data is allowed to default to private type and none is inherited
into the derived classes. Some of the data is left in the example
program simply to make the classes look like classes. Based on
your experience with C++ by now, you realize that the data could
be removed since it is not used.
After you understand this program, compile and execute it to see
if your compiler gives the same result of execution.
ADDING THE KEYWORD VIRTUAL
_________________________________________________________________
As you examine the next example program named ================
VIRTUAL2.CPP, you will notice that there is one VIRTUAL2.CPP
small change in line 8. The keyword virtual has ================
been added to the declaration of the method
named message() in the parent class.
It may be a bit of a disappointment to you to learn that this
program operates no differently than the last example program.
This is because we are using objects directly and virtual methods
have nothing to do with objects, only with pointers to objects as
we will see soon. There is an additional comment in line 46
illustrating that since all four objects are of different classes,
Page 10-2
Chapter 10 - Virtual Functions
it is impossible to assign any object to any other object in this
program. We will soon see that some pointer assignments are
permitted between objects.
After you are convinced that virtual functions, or methods, have
nothing to do with actual objects, compile and execute this example
program to see if your compiler results in the same output as that
listed.
USING OBJECT POINTERS
_________________________________________________________________
Examine the example program named VIRTUAL3.CPP ================
and you will find a repeat of the first program VIRTUAL3.CPP
but with a different main program. ================
In this program the keyword virtual has been
removed from the method declaration in the parent class in line 8,
and the main program declares pointers to the objects rather than
declaring the objects themselves in lines 37 through 40. Since we
only declared pointers to the objects we find it necessary to
allocate the objects before using them by using the new operator
in lines 42 through 49. Upon running the program, we find that
even though we are using pointers to the objects we have done
nothing different than what we did in the first program. Upon
execution, we find that the program operates in exactly the same
manner as the first example program in this chapter. This should
not be surprising because a pointer to a method can be used to
operate on an object in the same manner as an object can be
manipulated.
Be sure to compile and execute this program before continuing on
to the next example program. The observant student will notice
that we failed to deallocate the objects prior to terminating the
program. As always, in such a simple program, it doesn't matter
because the heap will be cleaned up automatically when we return
to the operating system.
A POINTER AND A VIRTUAL FUNCTION
_________________________________________________________________
The example program named VIRTUAL4.CPP is ================
identical to the last program except for the VIRTUAL4.CPP
addition of the keyword virtual to line 8 once ================
again.
I hope you are not terribly disappointed to find that this program,
including the keyword virtual, is still iden