📄 chap09.htm
字号:
A default constructor is available for each of these classes.
<P>The second constructor is more interesting since it does not simply
use the default constructor, but instead passes some of the input parameters
to the inherited class constructors. Following the colon in line 14 are
two member initializers which are used to initialize members of this class.
Since the two parent classes are inherited, they are also members of this
class and can be initialized as shown. Each of the member initializers
is actually a call to a constructor of the base classes and it should be
evident that there must be a constructor in each base class with the proper
number of input parameters to respond to the messages given. You will note
that in line 15, we are actually calling the default constructor of the
<B>new_date</B> class since no parameters are given explicitly. If we chose,
we could simply let the system call the default constructor automatically,
but this gives us an explicit comment on what is happening.
<P>Following all member initialization, the normal constructor code for
the derived class is executed which in this case is given in line 17.
<P><B>ORDER OF MEMBER INITIALIZATION</B>
<P>The order of member initialization seems a bit strange, but it does
follow a few simple rules. You will recall from the last chapter that the
order of member initialization does not follow the order given by the initialization
list, but another very strict order over which you have complete control.
All inherited classes are initialized first in the order they are listed
in the class header. If lines 15 and 16 were reversed, class <B>new_date</B>
would still be initialized first because it is mentioned first in line
8. We mentioned that C++ respects its elders and initializes its parents
prior to itself. That should be a useful memory aid in the use of member
initializers.
<P>Next, all local class members, if there are any, are initialized in
the order in which they are declared in the class, not the order in which
they are declared in the initialization list.
<P>Finally, after the member initializers are all executed in the proper
order, the main body of the constructor is executed in the normal manner.
<P><B>USING THE NEW CLASS</B>
<P>Example program ------> <B><A HREF="USEDTTM.CPP">USEDTTM.CPP</A></B>
<P>The example program named USEDTTM.CPP uses the <B>datetime </B>class
we just built, and like our previous examples, the main program is kept
very simple and straight forward. You will note that the default constructor
is used for the object named <B>now</B>, and the constructor with the member
initializers is used with the objects named <B>birthday </B>and <B>special</B>.
<P>The diligent student should have no trouble understanding the remaining
code in this very simple example. You will note that the class, once we
are finished with it, is very easy to use because of the effort we put
into properly declaring it.
<P><B>FUTURE DIRECTIONS OF C++</B>
<P>An ANSI committee has been formed to write an ANSI standard for C++.
They first met in the Spring of 1990 and have released a preliminary draft
of the standard. The goal for the release of the standard is 1998, but
until the new standard is released, the C++ language was supposed to stay
fairly stable. However, due to the nature of compiler writers and their
desire to improve their offerings over their competitors, the language
has not remained static during this period.
<P>Many changes have been added in recent years that affect the C++ programmer
in a big way. You can be sure that the language will evolve into a very
usable and reliable language. In the meantime, however, the language is
changing and we must follow in order to stay effective and competitive.
<P>There are two areas, however, that should be discussed in a little detail
because they will add so much to the language in future years. Those two
topics are parameterized types and exception handling, both of which are
now available with good C++ compilers.
<P><B>PARAMETERIZED TYPES</B>
<P>Many times, when developing a program, you wish to perform some operation
on more than one data type. For example you may wish to sort a list of
integers, another list of floating point numbers, and a list of alphabetic
strings. It seems silly to have to write a separate sort function for each
of the three types when all three are sorted in the same logical way. With
parameterized types, you will be able to write a single sort routine that
is capable of sorting all three of the lists.
<P>This is already available in the Ada language as the generic package
or procedure. Because it is available in Ada, there is a software components
industry that provides programmers with prewritten and thoroughly debugged
software routines that work with many different types. When this is generally
available in C++, there will be a components industry for C++ and precoded,
debugged and efficient source code will be available off the shelf to perform
many of the standard operations. These operations will include such things
as sorts, queues, stacks, lists, etc. There is already a library of these
components available as a part of the ANSI-C++ standard. It is called the
Standard Template Library, usually referred to as the STL. Even though
studying this library is beyond the scope of this tutorial, it will be
very beneficial for you to study it and learn how to use it in your programs.
<P>Most compiler writers have included the ability to use templates in
their newest compilers. The next three example programs will illustrate
the use of templates according to the proposed ANSI standard. Since some
compiler writers did not follow that standard initially, these programs
may not work with all compilers. Eventually, all C++ compilers will have
to follow the standard in order to remain competitive, so it would pay
you to study these examples.
<P><B>THE FIRST TEMPLATE</B>
<P>Example program ------> <B><A HREF="TEMPLAT1.CPP">TEMPLAT1.CPP</A></B>
<P>The example program named TEMPLAT1.CPP is the first example of the use
of a template. This program is so simple it seems silly to even bother
with it but it will illustrate the use of the parameterized type.
<P>The template is given in lines 4 through 8 with the first line indicating
that it is a template with a single type to be replaced, the type ANY_TYPE.
This type can be replaced by any type which can be used in the comparison
operation in line 7. If you have defined a class, and you have overloaded
the operator ">", then this template can be used with objects of your class.
Thus, you do not have to write a <B>maximum </B>function for each type
or class in your program.
<P>This function is included automatically for each type it is called with
in the program, and the code itself should be very easy to understand.
<P>The diligent student should realize that nearly the same effect can
be achieved through use of a macro, except that when a macro is used, the
strict type checking is not done. Because of this and because of the availability
of the inline method capability in C++, the use of macros is essentially
non-existent by experienced C++ programmers.
<P><B>A CLASS TEMPLATE</B>
<P>Example program ------> <B><A HREF="TEMPLAT2.CPP">TEMPLAT2.CPP</A></B>
<P>The example program named TEMPLAT2.CPP is a little more involved since
it provides a template for an entire class rather than a single function.
The template code is given in lines 6 through 16 and a little study will
show that this is an entire class definition. The diligent student will
recognize that this is a very weak stack class since there is nothing to
prevent popping data from an empty stack, and there is no indication of
a full stack. Our intent, however, is to illustrate the use of the parameterized
type and to do so using the simplest class possible.
<P>In the main program we create an object named <B>int_stack</B> in line
25 which will be a stack designed to store integers, and another object
named <B>float_stack</B> in line 26 which is designed to store <B>float
</B>type values. In both cases, we enclose the type we desire this object
to work with in "<>" brackets, and the system creates the object by
first replacing all instances of <B>ANY_TYPE</B> with the desired type,
then creating the object of that type. You will note that any type can
be used that has an assignment capability since lines 13 and 14 use the
assignment operator on the parameterized type. The assignment operator
is used in line 14 because it returns an object of that type which must
be assigned to something in the calling program.
<P>Even though the strings are all of differing lengths, we can even use
the stack to store a stack of strings if we only store a pointer to the
strings and not the entire string. This is illustrated in the object named
<B>string_stack</B> defined in line 27 and used later in the program.
<P>This program should be fairly easy for you to follow if you spend a
bit of time studying it. You should compile and run it if you have a compiler
that will handle this new construct.
<P><B>REUSING THE STACK CLASS</B>
<P>Example program ------> <B><A HREF="TEMPLAT3.CPP">TEMPLAT3.CPP</A></B>
<P>The program named TEMPLAT3.CPP uses the same class with the template
as defined in the last program but in this case, it uses the <B>date </B>class
developed earlier as the stack members. More specifically, it uses a pointer
to the <B>date </B>class as the stack member.
<P>Because class assignment is legal, you could also store the actual class
in the stack rather than just the pointer to it. To do so however, would
be very inefficient since the entire class would be copied into the stack
each time it is pushed and the entire class would be copied out again when
it was popped. Use of the pointer is a little more general, so it was illustrated
here for your benefit.
<P>All three of the previous programs can be compiled and executed if you
have a compiler that supports templates. Parameterized types are a part
of the C++ specification, and are included in most of the newest implementations.
<P><B>EXCEPTION HANDLING</B>
<P>A future version of C++ will have some form of exception handling to
allow the programmer to trap errors and prevent the system from completely
shutting down when a fatal error occurs. The Ada language allows the programmer
to trap any error that occurs, even system errors, execute some recovery
code, and continue on with the program execution in a very well defined
way.
<P><B><I>(Note - Exception handling will be added to this tutorial very
soon.)</I></B>
<P><B>WHAT SHOULD BE YOUR NEXT STEP?</B>
<P>Once again, we have reached a major milestone in C++ programming. With
the ability to use inheritance, you have nearly all of the tools you need
to effectively use the object oriented programming techniques of C++ and
you would do well to stop studying again and begin programming. The only
topic left with C++ is virtual methods which are used for dynamic binding
or polymorphism. This will be covered in the next two chapters. The vast
majority of all programming can be done without dynamic binding, and in
attempting to force it into every program, you could wind up with an unreadable
mess, so you should approach it slowly.
<P><A HREF="chap10.htm">Advance to Chapter 10</A>
<P><A HREF="cpplist.htm">Return to the Table of Contents</A>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -