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

📄 chap08.txt

📁 含有文章和源码
💻 TXT
📖 第 1 页 / 共 2 页
字号:
the fact that class members default to private by definition.

You will notice that the variables named wheels and weight are
available to use in the method named initialize() in lines 85
through 91 just as if they were declared as a part of the car class
itself.  We can now state the rules for the three means of defining
variables and methods.

     private - The variables and methods are not available to any
          outside calling routines, and they are not available to
          any derived classes inheriting this class.

     protected - The variables and methods are not available to any
          outside calling routines, but they are directly available
          to any derived class inheriting this class.

     public - All variables and methods are freely available to all
          outside calling routines and to all derived classes.

You will note that these three means of definition can also be used
in a struct type.  The only difference with a struct is that
everything defaults to public until one of the other keywords is
used.

Be sure to compile and execute this program before continuing on
to the next example program.

                                                         Page 8-4

                                     Chapter 8 - More Inheritance

WHAT IS PRIVATE DATA?
_________________________________________________________________

Examine the file named INHERIT5.CPP where the    ================
data is allowed to use the private default.  In    INHERIT5.CPP
this program, the data is not available for use  ================
in the derived classes, so the only way the data
in the base class can be used is through use of
messages to methods in the base class.

It seems a little silly to have to call methods in the base class
to get to the data which is actually a part of the derived class,
but that is the way C++ is defined to work.  This would indicate
to you that you should spend some time thinking about how any class
you define will be used.  If you think somebody may wish to inherit
your class into a new class and expand it, you should make the data
members protected so they can be easily used in the new class.  Be
sure to compile and execute this program.



INHERITING CONSTRUCTORS
_________________________________________________________________

Examine the example program named INHERIT6.CPP   ================
for yet another variation to our basic program,    INHERIT6.CPP
this time adding constructors.                   ================

The vehicle class has a constructor to
initialize the number of wheels and the weight to the indicated
values and has no surprising constructs.  The car and truck classes
each have a constructor also to initialize their unique variables
to some unique values.  If you jump ahead to the main program, you
will find that the initializing statements are commented out for
each of the objects so we must depend on the constructors to
initialize the variables.  The most important thing to glean from
this example program is the fact that when one of the constructors
is called for a derived class, the constructor is also called for
the parent class.  In fact, the constructor for the parent class
will be called before the constructor for the derived class is
called.  All of the data will be initialized, including the data
inherited from the parent class.

We will say much more about constructors used with inheritance in
the next chapter of this tutorial.  Be sure to compile and execute
this example program.


POINTERS TO AN OBJECT AND AN ARRAY OF OBJECTS
_________________________________________________________________

Examine the example program named INHERIT7.CPP for examples of the
use of an array of objects and a pointer to an object.  In this


                                                         Page 8-5

                                     Chapter 8 - More Inheritance

program, the objects are instantiated from an    ================
inherited class and the intent of this program     INHERIT7.CPP
is to illustrate that there is nothing magic     ================
about a derived class.

The program is identical to the first program in this chapter until
we get to the main program where we find an array of 3 objects of
class car declared in line 52.  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 56 through 62.  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.

You will notice, in line 65, that we do not declare an object of
type truck but a pointer to an object of type truck.  In order to
use the pointer, we must give it something to point at which we do
in line 67 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 68 through 72, and will be further illustrated in the
example program of chapter 12 in this tutorial.

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


THE NEW TIME CLASS
_________________________________________________________________

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

A complete solution to this problem will be found in the ANSWERS
directory on the distribution disk.  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.









                                                         Page 8-6

                                     Chapter 8 - More Inheritance

PROGRAMMING EXERCISES
_________________________________________________________________

1.   Remove the comment delimiters from lines 65 through 67 of
     INHERIT2.CPP to see what kind of results are returned.  Remove
     them from line 57 to see what kind of an error is reported by
     the compiler for this error.

2.   Add cout statements to each of the constructors of
     INHERIT5.CPP to output messages to the monitor so you can see
     the order of sending messages to the constructors.












































                                                         Page 8-7

⌨️ 快捷键说明

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