⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap06.htm

📁 Since the field of object oriented programming is probably new to you, you will find that there is a
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<P>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.

<P>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 78 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.

<P>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.

<P>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 &lt;&lt; operator which is part of the <B>cout </B>class, 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.

<P>Be sure to compile and execute this program.

<P><B>SEPARATE COMPILATION</B>

<P>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++.

<P><B>YOU GET SOME METHODS BY DEFAULT</B>

<P>Example program ------> <B><A HREF="DEFMETHS.CPP">DEFMETHS.CPP</A></B>

<P>Even if you include no constructors or operator overloadings you get
a few defined automatically by the compiler. Examine the file named DEFMETHS.CPP
which will illustrate those methods provided by the compiler, and why you
sometimes can't use the defaults but need to write your own to do the job
the defaults were intended to do for you.

<P>Before we actually look at the program, we will list a few rules that
all compiler writers must follow in order to deliver a useful implementation
of C++. First we will state the rules, then take a closer look at them
and the reason for their existence.
<OL>
<LI>
If no constructors are defined by the writer of a class, the compiler will
automatically generate a default constructor and a copy constructor. Both
of these constructors will be defined for you shortly.</LI>

<LI>
If the class author includes any constructor in the class, the default
constructor will not be supplied by the constructor.</LI>

<LI>
If the class author does not include a copy constructor, the compiler will
generate one, but if the writer includes a copy constructor, the compiler
will not generate one automatically.</LI>

<LI>
If the class author includes an assignment operator, the compiler will
not include one automatically, otherwise it will generate a default assignment
operator.</LI>
</OL>
Any class declared and used in a C++ program must have some way to construct
an object because the compiler, by definition, must call a constructor
when we define an object. If we don't provide a constructor, the compiler
itself will generate one that it can call during construction of the object.
This is the default constructor and we have used it unknowingly in a lot
of our example programs. The default constructor does not initialize any
of the member variables, but it sets up all of the internal class references
it needs, and calls the base constructor or constructors if they exist.
We haven't studied inheritance yet, but we will in the next chapter of
this tutorial so we will know then what base classes are all about. Line
12 of the present program declares a default constructor which is called
when you define an object with no parameters. In this case, the constructor
is necessary because we have an embedded string in the class that requires
a dynamic allocation and an initialization of the string to the null string.
It will take little thought to see that our constructor is much better
than the default constructor which would leave us with an uninitialized
pointer.

<P>The default constructor is used in line 79 of this example program.

<P><B>THE COPY CONSTRUCTOR</B>

<P>The copy constructor is generated automatically for you by the compiler
if you fail to define one yourself. It is used to copy the contents of
an object to a new object during construction of that new object. If the
compiler generates it for you, it will simply copy the contents of the
original into the new object as a byte by byte copy, which may not be what
you want. For simple classes with no pointers, that is usually sufficient,
but in the present example program, we have a pointer as a class member
so a byte by byte copy would copy the pointer from one to the other and
they would both be pointing to the same allocated member. For this program,
we declared our own copy constructor in line 15 and implemented it in lines
35 to 40. A careful study of the implementation will reveal that the new
class will indeed be identical to the original, but the new class has its
own string to work with. Since both constructors contain dynamic allocation,
we must assure that the allocated data is destroyed when we are finished
with the objects, so a destructor is mandatory as implemented in lines
51 through 54 of the present example program. The copy constructor is used
in line 85 of the current example program.

<P><B>THE ASSIGNMENT OPERATOR</B>

<P>It is not too obvious, but an assignment operator is required for this
program also, because the default assignment operator simply copies the
source object to the destination object byte by byte. This would result
in the same problem we had with copy constructor. The assignment operator
is declared in line 18 and defined in lines 42 through 49 where we deallocate
the old string in the existing object prior to allocating room for the
new text and copying the text from the source object into the new object.
The assignment operator is used in line 92.

<P>It should be fairly obvious to the student that when a class is defined
which includes any sort of dynamic allocation, the above three methods
should be included in addition to the proper destructor. If any of the
four entities are omitted, the program may have terribly erratic behavior.
Be sure to compile and execute this example program.

<P><B>A PRACTICAL EXAMPLE</B>

<P>Example program ------> <B><A HREF="PHRASE.H">PHRASE.H</A></B>

<P>Using the <B>inline </B>keyword with a class member can cause a bit
of difficulty unless you understand how the compiler uses the inline code
definition to perform the inline code insertion. Examine the header file
named PHRASE.H which includes some inline methods. These are included as
an illustration of one means of defining the inline methods in a clean
way that the compiler can use efficiently.

<P>When any implementation uses this class, it must have access to the
inline implementation in order to insert the proper inline code for the
member functions. One way to do this is to put all of the inline methods
in a separate file named with the INL extension, then including that file
into the end of the .H file as shown here. This makes all of the inline
code available for the compiler while compiling files that use this class.

<P>Example program ------> <B><A HREF="PHRASE.INL">PHRASE.INL</A></B>

<P>The example file named PHRASE.INL contains all of the inline code for
this class.

<P>Example program ------> <B><A HREF="PHRASE.CPP">PHRASE.CPP</A></B>

<P>Note that the only reason for this file to exist is to define the static
string variable <B>full_phrase</B>. Since this is a definition, and therefore
some memory is defined, it cannot be placed in the header file. If it were
placed there, it would seem to work all right in this program because the
header file is only used once, but using a bad technique like that would
lead to problems later. For illustrative purposes, all of the methods were
declared inline, so there are no member definitions in this class.

<P>Example program ------> <B><A HREF="USEPHRAS.CPP">USEPHRAS.CPP</A></B>

<P>The file named USEPHRAS.CPP uses the <B>phrase </B>class defined in
the last two example files. It is plain to see that this class is no different
than any others we have studied. It simply illustrates a way to package
inline code in a simple and very efficient manner.

<P><B>ANOTHER PRACTICAL EXAMPLE</B>

<P>We come again 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.

<P>Example program ------> <B><A HREF="TIME.H">TIME.H</A></B>

<P>In the last chapter we studied the <B>date </B>class and in this chapter
we will study a simple <B>time </B>class. You should begin by studying
the file named TIME.H which will look very similar to the <B>date </B>class
header. The only major difference in this class from the <B>date </B>class
is the overloaded constructors and methods. The program is a very practical
example that illustrates very graphically that many constructor overloadings
are possible.

<P>Example program ------> <B><A HREF="TIME.CPP">TIME.CPP</A></B>

<P>The implementation for the <B>time </B>class is given in the file named
TIME.CPP. Once again, the 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.

<P>As we have mentioned before, this code contains calls that are specific
to DOS and are therefore not portable to other platforms. If you are using
some other platform, you will need to change the code to make valid calls
to your operating system, or simply assign default values to the member
variables.

<P>Example program ------> <B><A HREF="USETIME.CPP">USETIME.CPP</A></B>

<P>The example program named USETIME.CPP is a very simple program that
uses the <B>time </B>class in a 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 <B>time </B>class and the
<B>date </B>class as the basis for both single and multiple inheritance
in the next three chapters.

<P><B>WHAT SHOULD BE THE NEXT STEP?</B>

<P>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.

<P>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.

<P>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.

<P><B>PROGRAMMING EXERCISES</B>
<OL>
<LI>
Modify OBJDYNAM.CPP to make the objects named <B>small </B>and <B>medium
</B>pointers, then dynamically allocate them prior to using them.</LI>

<LI>
Modify the loop in line 62 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 81 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 <B>get_area()</B> with such large numbers. That will
depend upon your compiler.</LI>

<LI>
Write a program that uses both the <B>date </B>and <B>time </B>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.</LI>
</OL>
<A HREF="chap07.htm">Advance to Chapter 7</A>

<P><A HREF="cpplist.htm">Return to Table of Contents</A>
</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -