📄 chap06.txt
字号:
operator. Any number of overloadings can be used provided the
parameters are different for each particular overloading.
The header in line 16 illustrates the first overloading where the
+ operator is overloaded by giving the return type followed by the
keyword operator and the operator we wish to overload. The two
formal parameters and their types are then listed in the
parentheses and the normal function operations are given in the
implementation of the function in lines 18 through 21. The
observant student will notice that the implementation of the friend
functions are not actually a part of the class because the class
name is not prepended onto the method name in line 16. There is
nothing unusual about this implementation, it should be easily
understood by you at this point. For purposes of illustration,
some silly mathematics are performed in the method implementation,
but any desired operations can be done.
Page 6-9
Chapter 6 - More Encapsulation
The biggest difference occurs in line 56 where this method is
called by using the infix notation instead of the usual message
sending format. Since the variables small and medium are objects
of the box class, the system will search for a way to use the +
operator on two objects of class box and will find it in the
overloaded operator+ method we have just discussed. The operations
within the method implementation can be anything we need them to
be, and they are usually much more meaningful than the silly math
included here.
In line 58 we ask the system to add an int type constant to an
object of class box, so the system finds the other overloading of
the + operator beginning in line 25 to perform this operation.
Also in line 60 we ask the system to use the * operator to do
something to an int constant and an object of class box, which it
satisfies by finding the method in lines 34 through 40. Note that
it would be illegal to attempt to use the * operator the other way
around, namely large * 4 since we did not define a method to use
the two types in that order. Another overloading could be given
with reversed types, and we could use the reverse order in a
program.
You will notice that when using operator overloading, we are also
using function name overloading since some of the function names
are the same.
When we use operator overloading in this manner, we actually make
our programs look like the class is a natural part of the language
since it is integrated into the language so well. C++ is therefore
an extendible language and can be molded to fit the mechanics of
the problem at hand.
OPERATOR OVERLOADING CAVEATS
_________________________________________________________________
Each new topic we study has its pitfalls which must be warned
against and the topic of operator overloading seems to have the
record for pitfalls since it is so prone to misuse and has several
problems. The overloading of operators is only available for
classes, you cannot redefine the operators for the predefined
simple types. This would probably be very silly anyway since the
code could be very difficult to read if you changed some of them
around.
The logical and (&&) and the logical or (||) operators can be
overloaded for the classes you define, but they will not operate
as short circuit operators. All members of the logical
construction will be evaluated with no regard concerning the
outcome. Of course the normal predefined logical operators will
continue to operate as short circuit operators as expected, but not
the overloaded ones.
Page 6-10
Chapter 6 - More Encapsulation
If the increment (++) or decrement (--) operators are overloaded,
the system has no way of telling whether the operators are used as
preincrement or postincrement (or predecrement or postdecrement)
operators. Which method is used is implementation dependent, so
you should use them in such a way that it doesn't matter which is
used.
Be sure to compile and execute OPOVERLD.CPP before continuing on
to the next example program.
FUNCTION OVERLOADING IN A CLASS
_________________________________________________________________
Examine the program named FUNCOVER.CPP for an ================
example of function name overloading within a FUNCOVER.CPP
class. In this program the constructor is ================
overloaded as well as one of the methods to
illustrate what can be done.
This file illustrates some of the uses of overloaded names and a
few of the rules for their use. You will recall that the function
selected is based on the number and types of the formal parameters
only. The type of the return value is not significant in overload
resolution.
In this case there are three constructors. The constructor which
is actually called is selected by the number and types of the
parameters in the definition. In line 77 of the main program the
three objects are declared, each with a different number of
parameters and inspection of the results will indicate that the
correct constructor was called based on the number of parameters.
In the case of the other overloaded methods, the number and type
of parameters is clearly used to select the proper method. You
will notice that one method uses a single integer and another uses
a single float type variable, but the system is able to select the
correct one. As many overloadings as desired can be used provided
that all of the parameter patterns are unique.
You may be thinking that this is a silly thing to do but it is, in
fact, a very important topic. Throughout this tutorial we have
been using an overloaded operator and you haven't been the least
confused over it. It is the cout operator which operates as an
overloaded function since the way it outputs data is a function of
the type of its input variable or the field we ask it to display.
Many programming languages have overloaded output functions so you
can output any data with the same function name.
Be sure to compile and execute this program.
Page 6-11
Chapter 6 - More Encapsulation
SEPARATE COMPILATION
_________________________________________________________________
Separate compilation is available with C++ and it follows the
identical rules as given for ANSI-C separate compilation. As
expected, separately compiled files can be linked together.
However, since classes are used to define objects, the nature of
C++ separate compilation is considerably different from that used
for ANSI-C. This is because the classes used to create the objects
are not considered as external variables, but as included classes.
This makes the overall program look different from a pure ANSI-C
program. Your programs will take on a different appearance as you
gain experience in C++.
ANOTHER PRACTICAL EXAMPLE
_________________________________________________________________
Once again we come to the practical part of this lesson where we
study a practical class that can actually be used in a program but
is still simple enough for the student to completely understand.
In the last chapter we studied the date class ================
and in this chapter we will study a simple time TIME.H
class. You should begin by studying the file ================
named TIME.H which will look very similar to the
date class header. The only major difference in
this class from the date class is the overloaded constructors and
methods. The program is a very practical example that illustrates
very graphically that many constructor overloadings are possible.
The implementation for the time class is given ================
in the file named TIME.CPP. Once again, the TIME.CPP
code is very simple and you should have no ================
problem understanding this example in its
entirety. It should be pointed out that three
of the four overloadings actually call the fourth so that the code
did not have to be repeated four times. This is a perfectly good
coding practice and illustrates that other member functions can be
called from within the implementation.
The example program named USETIME.CPP is a very ===============
simple program that uses the time class in a USETIME.CPP
very rudimentary way as an illustration for you. ===============
You should be able to understand this program in
a very short time. It will be to your advantage
to completely understand the practical example programs given at
the end of the last chapter and the end of this chapter. As
mentioned above, we will use the time class and the date class as
the basis for both single and multiple inheritance in the next
three chapters.
Page 6-12
Chapter 6 - More Encapsulation
WHAT SHOULD BE THE NEXT STEP?
_________________________________________________________________
At this point you have learned enough C++ to write meaningful
programs and it would be to your advantage to stop studying and
begin using the knowledge you have gained. Because C++ is an
extension to ANSI-C, it can be learned in smaller pieces than would
be required if you are learning a completely new language. You
have learned enough to study and completely understand the example
program given in chapter 12, the Flyaway adventure game. You
should begin studying this program now.
One of your biggest problems is learning to think in terms of
object oriented programming. It is not a trivial problem if you
have been programming in procedural languages for any significant
length of time. However, it can be learned by experience, so you
should begin trying to think in terms of classes and objects
immediately. Your first project should use only a small number of
objects and the remainder of code can be completed in standard
procedural programming techniques. As you gain experience, you
will write more of the code for any given project using classes and
objects but every project will eventually be completed in
procedural code.
After you have programmed for a while using the techniques covered
up to this point in the tutorial, you can continue on to the next
few chapters which will discuss inheritance and virtual functions.
PROGRAMMING EXERCISES
_________________________________________________________________
1. Modify OBJDYNAM.CPP to make the objects named small and medium
pointers, then dynamically allocate them prior to using them.
2. Modify the loop in line 61 of OBJLINK.CPP so that the loop
will store 1000 elements in the list before stopping. You
will probably wish to remove the printout from line 74 so the
program will stop in a reasonable time. You may also get an
integer overflow indicated by wrong answers if you send a
message to get_area() with such large numbers. That will
depend upon your compiler. You should also add a test to
assure that the memory did not become exhausted after each
dynamic allocation.
3. Write a program that uses both the date and time classes in
a meaningful manner. No answer will be given in the ANSWERS
directory for this exercise since it is so straight forward.
These classes can be used in all of your future C++ programs
to time stamp the time and date of execution.
Page 6-13
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -