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

📄 chap08.htm

📁 Since the field of object oriented programming is probably new to you, you will find that there is a
💻 HTM
📖 第 1 页 / 共 2 页
字号:
for completeness.

<P>Be sure to compile and execute this example program to see that your
compiler gives the same result. It would be a good exercise for you to
reintroduce some of the commented out lines to see what sort of an error
message your compiler issues for these errors.

<P><B>INITIALIZING ALL DATA</B>

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

<P>If you will examine the example program named INHERIT4.CPP, you will
find that we have fixed the initialization problem that we left dangling
in the last example program. We also added default constructors to each
of the classes so we can study how they are used when we use inheritance
and we have returned to the use of <B>public </B>inheritance.

<P>When we create an object of the base class <B>vehicle</B>, there is
no problem since inheritance is not a factor. The constructor for the base
class operates in exactly the same manner that all constructors have in
previous chapters. You will notice that we create the <B>unicycle </B>object
in line 47 using the default constructor and the object is initialized
to the values contained in the constructor. Line 49 is commented out because
we no longer need the initialization code for the object.

<P>When we define an object of one of the derived classes in line 57, the
<B>sedan</B>, it is a little different because not only do we need to call
a constructor for the derived class, we have to worry about how we get
the base class initialized through its constructor also. Actually, it is
no problem because the compiler will automatically call the default constructor
for the base class unless the derived class explicitly calls another constructor
for the base class. We will explicitly call another constructor in the
next example program, but for now we will only be concerned about the default
constructor for the base class that is called automatically.

<P><B>ORDER OF CONSTRUCTION</B>

<P>The next problem we need to be concerned about is the order of construction,
and it is easy to remember if you remember the following statement, "C++
classes honor their parents by calling their parents constructor before
they call their own." The base class constructor will be called before
the derived class constructor. This makes sense because it guarantees that
the base class is properly constructed when the constructor for the derived
class is executed. This allows you to use some of the data from the base
class during construction of the derived class. That was a mouthful, but
if you spend a little time with this concept, it will make a lot of sense,
and you will not easily forget it. In this case, the <B>vehicle </B>part
of the <B>sedan </B>object is constructed, then the local portions of the
<B>sedan </B>object will be constructed, so that all member variables are
properly initialized. This is why we can comment out the <B>initialize
</B>method in line 59. It is not needed.

<P>When we define a <B>semi </B>object in line 66, it will also be constructed
in the same manner. The constructor for the base class is executed, then
the constructor for the derived class will be executed. The object is now
fully defined and useable with default data in each member. Lines 68 and
69 are therefore not needed.

<P>The remainder of this program should be no problem for you to understand
except for the order of destruction of the various objects.

<P><B>HOW ARE THE DESTRUCTORS EXECUTED?</B>

<P>As the objects go out of scope, they must have their destructors executed
also, and since we didn't define any, the default destructors will be executed.
Once again, the destruction of the base class object named <B>unicycle
</B>is no problem, it's destructor is executed and the object is gone.
The <B>sedan </B>object however, must have two destructors executed to
destroy each of its parts, the base class part and the derived class part.
It should not be too much of a surprise that the destructors for this object
are executed in reverse order from the order in which they were constructed.
In other words, the object is dismantled in the opposite order from the
order in which it was assembled. The derived class destructor is executed
first, then the base class destructor is executed and the object is removed
from they system.

<P>Remember that every time an object is defined or created, every portion
of it must have a constructor executed on it. Every object must also have
a destructor executed on each of its parts when it is destroyed in order
to properly dismantle the object.

<P>Be sure to compile and execute this program following your detailed
study of it.

<P><B>INHERITANCE WHEN CONSTRUCTORS ARE USED</B>

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

<P>Examine the example program named INHERIT5.CPP for yet another variation
to our basic program, this time using constructors that are more than just
the default constructors. You will notice that each class has another constructor
declared within it. The additional constructor added to the <B>vehicle
</B>class in lines 12 through 14 is nothing special, it is just like some
of the constructors we have studied earlier in this tutorial. It is used
in line 59 of the main program where we define <B>unicycle </B>with two
values passed in to be used when executing this constructor.

<P>The constructor for the <B>car </B>class which is declared in lines
28 through 31 is a bit different, because we pass in three values. One
of the values, the one named <B>people, </B>is used within the derived
class itself to initialize the member variable named <B>passenger_load</B>.
The other two literal values however, must be passed to the base class
somehow in order to initialize the number of <B>wheels </B>and the <B>weight</B>.
This is done by using a member initializer, and is illustrated in this
constructor. The colon near the end of line 28 indicates that a member
initializer list follows, and all entities between the colon and the opening
brace of the constructor body are member initializers. The first member
initializer is given in line 29 and looks like a constructor call to the
<B>vehicle </B>class that requires two input parameters. That is exactly
what it is, and it calls the constructor for the <B>vehicle </B>class and
initializes that part of the <B>sedan </B>object that is inherited from
the <B>vehicle </B>class. We can therefore control which base class initializer
gets called when we construct an object of the derived class.

<P>The next member initializer, in line 30, acts kind of like a constructor
for a simple variable. By mentioning the name of the variable and including
a value of the correct type within the parentheses, that value is assigned
to that variable even though the variable is not a class, but a simple
predefined type. This technique can be used to initialize all members of
the derived class or any portion of them. When all of the members of the
member initializer list are executed, the code within the braces is executed.
In this case, there is no code within the executable block of the constructor.
The code within the braces would be written in a normal manner for a constructor.

<P><B>WHAT ABOUT THE ORDER OF EXECUTION?</B>

<P>This may seem to be very strange, but the elements of the member initializer
list are not executed in the order in which they appear in the list. The
constructors for the inherited classes are executed first, in the order
of their declaration in the class header. When using multiple inheritance,
several classes can be listed in the header line, but in this program,
only one is used. The member variables are then initialized, but not in
the order as given in the list, but in the order in which they are declared
in the class. Finally, the code within the constructor block is executed,
if there is any code in the block.

<P>There is a good reason for this seemingly strange order. The destructors
must be executed in reverse order from the construction order, but if there
are two constructors with different construction order defined, which should
define the destruction order? The correct answer is neither. The system
uses the declaration order for construction order and reverses it for the
destruction order.

<P>You will notice that the <B>truck </B>class uses one initializer for
the base class constructor and two member initializers, one to initialize
the<B> passenger_load, </B>and one to initialize the <B>payload</B>. The
body of the constuctor, much like the <B>car </B>class, is empty.

<P>The two constructors in the <B>car </B>class and the <B>truck </B>class
are called to construct objects in lines 69 and 78 for a <B>car </B>object
and a <B>truck </B>object as illustrations in this example program.

<P>The remainder of this program should be easy for you to folow. Be sure
to compile and execute this program before moving on.

<P><B>POINTERS TO AN OBJECT AND AN ARRAY OF OBJECTS</B>

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

<P>Examine the example program named INHERIT6.CPP for examples of the use
of an array of objects and a pointer to an object. In this program, the
objects are instantiated from an inherited class and the intent of this
program is to illustrate that there is nothing magic about a derived class.
A class acts the same whether it is a base class or a derived class.

<P>This program is identical to the first program in this chapter until
we get to the <B>main()</B> program where we find an array of 3 objects
of class <B>car </B>declared in line 53. It should be obvious that any
operation that is legal for a simple object is legal for an object that
is part of an array, but we must be sure to tell the system which object
of the array we are interested in by adding the array subscript as we do
in lines 58 through 64. The operation of this portion of the program should
be very easy for you to follow, so we will go on to the next construct
of interest.

<P>You will notice, in line 68, that we do not declare an object of type
<B>truck </B>but a pointer to an object of type <B>truck</B>. In order
to use the pointer, we must give it something to point at which we do in
line 70 by dynamically allocating an object. Once the pointer has an object
to point to, we can use the object in the same way we would use any object,
but we must use the pointer notation to access any of the methods of the
object. This is illustrated for you in lines 76 through 80, and will be
further illustrated in the example program of chapter 12 in this tutorial.

<P>Finally, we deallocate the object in line 81. You should spend enough
time with this program to thoroughly understand the new material presented
here, then compile and execute it.

<P><B>THE NEW TIME CLASS</B>

<P>We began a series of nontrivial classes in chapter 5 where we developed
a <B>date </B>class, then a <B>time </B>class, and finally a <B>newdate
</B>class in the last chapter. Now it is your turn to add to this series.
Your assignment is to develop the <B>newtime </B>class which inherits the
<B>time </B>class and adds a new member variable named <B>seconds_today</B>
and a method to calculate the value of seconds since midnight to fill the
variable.

<P>A complete solution to this problem will be found in cppans.zip available
for download. The files named NEWTIME.H, NEWTIME.CPP, and TRYNTIME.CPP
are the solution files. It would be a good exercise for you to attempt
to write this new class before you look at the example solution.

<P><B>PROGRAMMING EXERCISES</B>
<OL>
<LI>
Remove the comment delimiters from lines 57 through 59 of INHERIT3.CPP
to see what kind of errors are reported.</LI>

<LI>
Add <B>cout </B>statements to each of the constructors of INHERIT4.CPP
to output messages to the monitor so you can see the order of sending messages
to the constructors.</LI>
</OL>
<A HREF="chap09.htm">Advance to Chapter 9</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 + -