Chapter 10 Inheritance

来源:互联网 发布:北京网络布线收费标准 编辑:程序博客网 时间:2024/05/19 03:42
Chapter
10 Inheritance
• 10.1 A Derived Class
• 10.2 Typing Conversions
and Visibility
• 10.3 Code Reuse: A
Binary Tree Class
• 10.4 Virtual Functions
• 10.5 Abstract Base
Classes
• 10.6 Templates and
Inheritance
• 10.7 Multiple Inheritance
• 10.8 Inheritance and
Design
• 10.8.1 Sub typing Form
• 10.9 Run-Time Type
Identification
• 10.10 Pragmatics
• Summary
• Exercises
Chapter
10 Inheritance
• Inheritance
I n h e r i t an c e
is the mechanism of deriving a
new class from an old one.
• Through inheritance, a hierarchy of related
types can be created that share code and
interfaces.
• A derived class inherits the description of
the base
bas e
class; it can then be altered by
adding members, modifying existing
member functions, and modifying access
privileges.
Chapter
10 Inheritance
• C++ supports virtual member functions.
v i r t u a l m e m b e r f u n c t i o n s .
These
are functions declared in the base class and
redefined in a derived class.
• By accessing the virtual function through this
pointer, C++ selects the appropriate function
definition at run-time.
• This is a form of polymorphism called pure
p u r e
polymorphism.
p o l y m o r p h i s m .
• Inheritance should be designed into software to
maximize reuse and allow a natural modeling of
the problem domain.
Chapter
10 Inheritance
• OOD Design Methodology
– 1. Decide on an appropriate set of types.
– 2. Design in their relatedness, and use
inheritance to share code.
– 3. Use virtual functions to process related
objects polymorphically.
10.1 A Derived Class
• A
class can be derived from an existing
class using the form:
10.1 A Derived Class
10.1 A Derived Class
10.1 A Derived Class
• This subtyping relationship is called the
ISA
I S A
relationship. This is also called
interface inheritance.
• A
derived class is a modification of the base
class; it inherits the public and protected
members of the base class. Only
constructors, its destructor, and any member
function operator= ( )
cannot be inherited.
10.1 A Derived Class
• Benefits of Using a Derived Class
ilftseragovdnBCDU – Code is reused.
– The hierarchy reflects a relationship found in
the problem domain.
– Various polymorphic mechanisms will allow
client code to treat derived class
as a subtype
of base class.
10.2 Typing Conversions and
Visibility
• A publicly derived class is a subtype of its
base class.
• A
pointer whose type is pointer to base
class can point to objects that have the
derived class type.
10.2 Typing Conversions and
Visibility
• A
reference to the derived class may be
implicitly converted to a reference to the
public base class. For example:
10.2 Typing Conversions and
Visibility
10.2 Typing Conversions and
Visibility
• Demo on page 325: student2.cpp
10.3 Code Reuse: A Binary Tree
Class
• Private inheritance does not have a subtype,
or ISA,
I S A ,
relationship. In private inheritance,
we reuse a base class for its code.
• This is also called implementation
inheritance,
as opposed to interface
inheritance.
10.3 Code Reuse: A Binary Tree
Class
• Because private (and also protected)
inheritance does not create a type hierarchy,
it has more limited utility than public
inheritance.
• Templates and inheritance are reuse
techniques with different advantages.
Normally templates are a simpler design
and generate faster but larger executables.
10.3 Code Reuse: A Binary Tree
Class
10.3 Code Reuse: A Binary Tree
Class
10.3 Code Reuse: A Binary Tree
Class
10.3 Code Reuse: A Binary Tree
Class
• Demo on page 329: gentree2.cpp
10.3 Code Reuse: A Binary Tree
Class
10.3 Code Reuse: A Binary Tree
Class
• The template methodology is simpler and more
run-time efficient.
• It is simpler because instantiation requires only a
single actual type placed in the template
declaration.
• In inheritance we need to derive the whole
interface, substituting appropriate types. It is more
run-time efficient because we can often avoid
indirection.
• Inheritance allows special cases to be developed
for each type if necessary. Inheritance does not
lead to large object code modules. Remember each
template instantiation is compiled to object code.
10.4 Virtual Functions
• Overloaded member functions are invoked
by a type matching algorithm that includes
having the implicit argument matched to an
object of that class type.
• As will become apparent, it would be nice
to dynamically select at run time the
appropriate member function from among
base and derived class functions.
10.4 Virtual Functions
• The keyword virtual
v i r t u al
is a function specifier
that provides such a mechanism, but it may
be used only to modify member function
declarations. The combination of virtual
v i r t u al
functions
f u n c t i on s
and public inheritance will be our
most general and flexible way to build a
piece of software. This is a form of pure
polymorphism.
10.4 Virtual Functions
• An ordinary virtual function must be
executable code. When invoked, its
semantics are the same as those of other
functions.
• In a derived class, it can be overridden, and
the function prototype of the derived
function must have matching signature and
return type.
10.4 Virtual Functions
• The selection of which function definition to
invoke for a virtual function is dynamic. The
typical case is where a base class has a virtual
function, and derived classes have their versions
of this function.
• A pointer to base class can point at either a base
class object or a derived class object. The member
function selected will depend on the class of the
object being pointed at, not on the pointer type. In
the absence of a derived type member, the base
class virtual function is used by default.
10.4 Virtual Functions
• The overloaded member function is selected
at compile time based on signature, and it
can have distinct return types. A virtual
function is selected at run time based on the
object's type, which is passed to it as its this
pointer argument.
• Also, once declared virtual , this property is
carried along to all redefinitions in derived
classes. It is unnecessary in the derived
class to use the function modifier virtual .
10.4 Virtual Functions
• Demo on page 331: virt_sel.cpp
10.4 Virtual Functions
10.4 Virtual Functions
• Virtual functions and member function
overloading cause confusion.
• Demo on page 332: virt_err.cpp
10.4 Virtual Functions
10.4 Virtual Functions
• The declaration of an identifier in a scope
hides all declarations of that identifier in
outer scopes. A base class is an outer scope
of any class derived from it.
• Access restrictions (private, protected)
are
orthogonal to function selection.
• Only nonstatic member functions can be
virtual. The virtual characteristic is
inherited.
10.4 Virtual Functions
• Constructors cannot be virtual.
• Destructors can be virtual. As a rule of
thumb, any class having virtual functions
should have a virtual destructor.
• Virtual functions allow run-time decisions.
• Demo on page 333: shape2.cpp
10.4 Virtual Functions
10.4 Virtual Functions
10.5 Abstract Base Classes
• A
type hierarchy usually has its root class
contain a number of virtual functions.
• Virtual functions provide for dynamic
typing. Virtual functions in the root class
are often dummy functions.
• In C++, the pure virtual function
pu r e v i r t u al f u n c t i on
is
introduced for this purpose.
• A
pure virtual function is a virtual member
function whose body is normally undefined.
10.5 Abstract Base Classes
• virtual function prototype
f u n c t i on pr ot ot y pe
= 0 ;
0 ;
• In OOP terminology it is called a deferred
de f e r e d
method.
m e t h od.
• A
A
class that has at least one pure virtual
function is an abstract class.
abs t r ac t c l as s .
• Demo on page 335: predator.cpp
10.5 Abstract Base Classes
10.5 Abstract Base Classes
10.5 Abstract Base Classes
10.5 Abstract Base Classes
10.5 Abstract Base Classes
• Grass can be eaten by rabbits.
10.5 Abstract Base Classes
10.5 Abstract Base Classes
10.5 Abstract Base Classes
10.5 Abstract Base Classes
10.5 Abstract Base Classes
10.5 Abstract Base Classes
10.6 Templates and Inheritance
• Templates and inheritance are jointly an extremely
powerful reuse technique.
• Templates and inheritance are both mechanisms
for code reuse, and both can involve
polymorphism.
• A template class can derive from an ordinary class,
an ordinary class can derive from an instantiated
template class, and a template class can derive
from a template class.
• In some situations, templates lead to unacceptable
cost in the size of the object module.
10.6 Templates and Inheritance
10.6 Templates and Inheritance
• Demo on page 341 stack_t2.cpp
10.6 Templates and Inheritance
• Demo on page 342: stack_t3.cpp
10.7 Multiple Inheritance
• Multiple inheritance
M u l t i pl e i n h e r i t an c e
allows a derived class
to be derived from more than one base class.
• This chain must not be circular, however; a
class cannot have itself as an ancestor.
10.7 Multiple Inheritance
10.7 Multiple Inheritance
• This parental relationship is described by
the inheritance directed acyclic graph
di r e c t e d ac y c l i c gr aph
(DAG).
The DAG
is a graph structure
whose nodes are classes and whose directed
edges point from base to derived class.
10.7 Multiple Inheritance
10.7 Multiple Inheritance
• With multiple inheritance, two base classes
can be derived from a common ancestor.
10.7 Multiple Inheritance
10.7 Multiple Inheritance
• Order of Constructor Execution
• 1. Base classes initialized in declaration
order.
• 2. Members initialized in declaration order.
• 3. The body of the constructor.
10.7 Multiple Inheritance
• Virtual base classes are constructed before
any of their derived classes, and before any
nonvirtual base classes.
• Destructors are invoked in the reverse order
of constructors.
10.8 Inheritance and Design
• At one level, inheritance is a code sharing
technique. At another level it reflects an
understanding of the problem.
• There is no way to specify a completely
optimal design.
10.8 Inheritance and Design
• An undue amount of decomposition
imposes its own complexity, and ends up
being self-defeating.
• Single inheritance (SI) conforms to a
hierarchical decomposition of the key
objects in the domain of discourse. Multiple
inheritance (MI) is more troubling as a
modeling or problem-solving concept.
10.8 Inheritance and Design
• MI presents problems for the type theorist.
• None of this diminishes the attraction of MI
as a code reuse technique.
10.8.1 Sub typing Form
• ADTs are successful insofar as they behave
like native types.
10.8.1 Sub typing Form
10.8.1 Sub typing Form
• It is usual to leave the root of the hierarchy
abstract. This yields the most flexible
design.
• By using pure virtual functions, we are
precluded from declaring objects of this
type.
• This level of the design focuses on public
interface.
10.8.1 Sub typing Form
• Finally, virtual pub1ic
• inheritance ensures that in MI schemes, we
will not have multiple copies of the abstract
base class.
10.9 Run-Time Type Identification
• Run-time type identification (RTTI)
provides a mechanism for safely
determining the type pointed at by a base
class pointer at run time. It involves
dynamic_cast , an operator on a base class
pointer; typeid, an operator for determining
the type of an object; and type_info, a
structure providing run-time information for
the associated type.
10.9 Run-Time Type Identification
• The dynami c-cast operator has the form:
dynamic_cast
itc_<
type
t y pe
>( v )
• where type
t y pe
must be a pointer or reference to
a class type and v must be a corresponding
pointer value or reference value.
10.9 Run-Time Type Identification
• Thls is called a down-cast.
dow n - c as t .
Dynamic casts
also work with reference types.
• Demo on page 348:
typeid.cpp
10.10 Pragmatics
• A difficulty in learning C++ is the many
distinctions and rules pertaining to the use of
functions.
• Function Use in C++
– 1. A virtual function and its derived instances that have
the same signature must have the same return type with
some minor exceptions.
– 2. All member functions except constructors and
overloaded new and delete can be virtual.
– 3. Constructors, destructors, overloaded operator=, and
friends are not inherited.
10.10 Pragmatics
– 4. Overloading the operators =, 0, [I, and -> can
be done only with nonstatic member functions.
Conversion functions that are operator type()
t y p e ( )
must also be done only with nonstatic member
functions.
– 5. A union may have constructors and
destructors, but not virtual functions.
– 6. Access modification is possible, but using it
with public inheritance destroys the subtype
relationship.
10.10 Pragmatics
• Dem on page 350: acc_mod.cpp
Summary
• 1. Inheritance is the mechanism of deriving
a new class from old ones.
• 2. A class can be derived from an existing
class using the form:
• 3. The keywords publ i c, private, and
protected are available as visibility
modifiers for class members.
• 4. The derived class has its own
constructors, which will invoke the base
class constructor.
Summary
• 5. A
publicly derived class is a subtype of its base
class.
• 6. A reference to the derived class may be
implicitly converted to a reference to the public
base class.
• 7.
The keyword virtual
is a function specifier that
provides a mechanism to dynamically select at run
time the appropriate member function from among
base and derived class functions.
• 8. Inheritance provides for code reuse.
• 9. A pure virtual function is a virtual member
function whose body is normally undefined. 
原创粉丝点击